ccgus / CocoaScript

JavaScript + the Cocoa frameworks, and then ObjC brackets show up to party as well.
Other
618 stars 58 forks source link

Keep track of background tasks #52

Closed mathieudutour closed 6 years ago

mathieudutour commented 6 years ago

As of today, the only way to keep a script alive while doing some async work is to use coScript.shouldKeepAround. The issue is that there is no (easy) way to schedule more than one background tasks without closing the others when one is finished.

Each background task should create its own "fiber" and shouldKeepRunning returns whether some fibers are still active or not.

ccgus commented 6 years ago

Do you have any sample Cocoa Script examples for this? Or is this only running inside a host app? Is this already being used in Sketch for example?

mathieudutour commented 6 years ago

That will be in the next Sketch release yes.

I wrote some doc for the Sketch API here (just replace sketch.createFiber() by coscript.createFiber()): https://github.com/BohemianCoding/SketchAPI/pull/75/files#diff-fdaccba03814731006f44d16097c7422

The fetch polyfill for Sketch already makes use of it (if it's present): https://github.com/skpm/sketch-polyfill-fetch/blob/master/lib/index.js#L68

I do have pure CocoaScript examples, only examples running in Sketch. To integrate the new API into another host app, you would need to change the checks for coscript.shouldKeepAround to coscript. shouldKeepRunning. I kept coscript.shouldKeepAround for backward compatibility reasons.

ccgus commented 6 years ago

All done, thanks for the patch.