Skip to content

MagicSketch/SketchAsync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SketchAsync

Example: Global Event Emission

Execute "Plugin > SketchAsync > runObjcAsync"

var onRunObjcAsync = function(context) {
    context.document.showMessage("running async for 3 seconds...");
    var async = SketchAsync.alloc().init();
    async.runSomethingInBackgroundForSeconds(3);
};
@implementation SketchAsync

- (void)runSomethingInBackgroundForSeconds:(NSTimeInterval)seconds {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSString *actionID = @"SketchAsync.finish";
        NSObject *appController = [NSClassFromString(@"AppController") valueForKeyPath:@"sharedInstance"];
        NSObject *pluginManager = [appController valueForKeyPath:@"pluginManager"];
        NSDictionary *pluginContext = [appController valueForKeyPath:@"pluginContext"];


        NSMutableDictionary *context = [NSMutableDictionary dictionaryWithDictionary:pluginContext];

        context[@"SketchAsyncDuration"] = @(seconds);

        [pluginManager performSelector:NSSelectorFromString(@"sendToInterestedCommandsActionWithID:context:") withObject:actionID withObject:context];

    });

    // At the end of dispatch_after, `SketchAsync.finish` event will be emitted
}

@end

Registering for SketchAsync.finish

in manifest.json, similar to other actions API, register a specific event of what defined in the native side

    {
        "script" : "script.cocoascript",
        "handlers" : {
            "actions" : {
                "SketchAsync.finish" : "onSketchAsyncFinish",
            }
        }
    }

in script.cocoscript, implement onSketchAsyncFinish

var onSketchAsyncFinish = function(context) {
    log("js context: " + context);
    log("js onSketchAsyncFinish");
    var duration = context.actionContext.SketchAsyncDuration;
    context.actionContext.document.showMessage("onSketchAsyncFinish: " + duration + " seconds");
};

About

Different ways of performing background operations in Sketch plugins and catch it from Javascript side

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published