Async file finding works with with blocks

This commit is contained in:
Corey Johnson
2012-01-03 11:11:11 -08:00
parent b3a7e1e097
commit d076b1a48c
2 changed files with 14 additions and 20 deletions

View File

@@ -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);
});
});
}

View File

@@ -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()