From d076b1a48ccbd7409f9800eb105a5581ebf598d2 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 3 Jan 2012 11:11:11 -0800 Subject: [PATCH] Async file finding works with with blocks --- Atom/Classes/AtomController.m | 30 ++++++++++++------------------ src/atom/root-view.coffee | 4 ++-- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Atom/Classes/AtomController.m b/Atom/Classes/AtomController.m index 6118749f0..9edeaed5e 100644 --- a/Atom/Classes/AtomController.m +++ b/Atom/Classes/AtomController.m @@ -123,28 +123,22 @@ - (void)contentsOfDirectoryAtPath:(NSString *)path onComplete:(JSValueRefAndContextRef)jsFunction { dispatch_queue_t backgroundQueue = dispatch_get_global_queue(0, 0); dispatch_queue_t mainQueue = dispatch_get_main_queue(); - - JSValueProtect(jsFunction.ctx, jsFunction.value); - NSLog(@"start %@", [NSThread currentThread]); + + JSValueProtect(self.jscocoa.ctx, jsFunction.value); dispatch_async(backgroundQueue, ^{ - NSLog(@"back %@", [NSThread currentThread]); - [NSThread sleepForTimeInterval:2]; + NSFileManager *fm = [NSFileManager defaultManager]; + NSDirectoryEnumerator *enumerator = [fm enumeratorAtPath:path]; + NSMutableArray *files = [NSMutableArray array]; - dispatch_sync(mainQueue, ^{ - NSLog(@"main %@", [NSThread currentThread]); - NSLog(@"ran the block on main %p", jsFunction.value); - [self.jscocoa callJSFunction:jsFunction.value withArguments:[NSArray arrayWithObject:@"testing"]]; -// JSValueUnprotect(jsFunction.ctx, jsFunction.value); - - }); - - [NSThread sleepForTimeInterval:2]; + NSString *filePath; + while (filePath = [enumerator nextObject]) { + [files addObject:filePath]; + } dispatch_sync(mainQueue, ^{ - NSLog(@"main %@", [NSThread currentThread]); - NSLog(@"ran the block on main %p", jsFunction.value); - [self.jscocoa callJSFunction:jsFunction.value withArguments:[NSArray arrayWithObject:@"testing 2"]]; - }); + [self.jscocoa callJSFunction:jsFunction.value withArguments:[NSArray arrayWithObject:files]]; + JSValueUnprotect(self.jscocoa.ctx, jsFunction.value); + }); }); } diff --git a/src/atom/root-view.coffee b/src/atom/root-view.coffee index 3ca21e4e2..28466474c 100644 --- a/src/atom/root-view.coffee +++ b/src/atom/root-view.coffee @@ -22,8 +22,8 @@ class RootView extends Template viewProperties: initialize: -> - fs.async.list '/foo', (result) -> - console.log(result.valueOf()) + fs.async.list '/Users/corey/code/mine/Atom', (result) -> + console.log "Got #{result.length} results." @bindKey 'meta+s', => @editor.save() @bindKey 'meta+w', => window.close()