Experiment with async callbacks from js

This commit is contained in:
Corey Johnson & Nathan Sobo
2011-12-30 18:02:11 -06:00
parent fe0c6f9a61
commit e327dcc8e5
4 changed files with 42 additions and 0 deletions

View File

@@ -20,4 +20,7 @@ struct JSGlobalContextRef;
- (JSValueRefAndContextRef)jsWindow;
- (void)performActionForMenuItemPath:(NSString *)menuItemPath;
- (void)contentsOfDirectoryAtPath:(NSString *)path onComplete:(JSValueRefAndContextRef)jsFunction;
@end

View File

@@ -3,6 +3,7 @@
#import "JSCocoa.h"
#import <WebKit/WebKit.h>
#import <dispatch/dispatch.h>
@interface AtomController ()
@property (nonatomic, retain) JSCocoa *jscocoa;
@@ -108,6 +109,7 @@
}
- (void)performActionForMenuItemPath:(NSString *)menuItemPath {
NSString *jsCode = [NSString stringWithFormat:@"window.performActionForMenuItemPath('%@')", menuItemPath];
[self.jscocoa evalJSString:jsCode];
}
@@ -118,6 +120,34 @@
return windowWithContext;
}
- (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]);
dispatch_async(backgroundQueue, ^{
NSLog(@"back %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
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];
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"]];
});
});
}
#pragma mark NSWindowDelegate
- (BOOL)windowShouldClose:(id)sender {
[self close];

View File

@@ -21,6 +21,10 @@ class RootView extends Template
viewProperties:
initialize: ->
fs.async.list '/foo', (result) ->
console.log(result.valueOf())
@bindKey 'meta+s', => @editor.save()
@bindKey 'meta+w', => window.close()
@bindKey 'meta+t', => @toggleFileFinder()

View File

@@ -95,3 +95,8 @@ module.exports =
# Return the path name of the current working directory.
workingDirectory: ->
OSX.NSFileManager.defaultManager.currentDirectoryPath.toString()
async:
list: (path, callback) ->
$atomController.contentsOfDirectoryAtPath_onComplete(path, callback)