Skip to content

Support observing promises in tasks #65

@TwitchBronBron

Description

@TwitchBronBron

We should add support for observing promises in task threads. This will allow tasks to much more nicely handle async flows without needing to manually write the logic. This also allows apps to align better with the other conventions in the app.

Implementation

Since tasks run a while loop, we need to include a helper function to handle promise events. Sometihng like this would work best, because promises could properly handle the promise events, and then pass along other events to the task.

promises.runEventLoop(m.messagePort, sub(message)
    'your while loop logic goes here, after promises has filtered through promise events
end function)

Then later in your task, you'd register promise callbacks like this

promises.onThen(promiseVar, callbackFunction) 

The magic for this is that, promises establishes its own internal port during promises.runEventLoop. Under the hood, promises onThen and other methods would likely just check for the promises port to determine which logic to implement (observing with callback function or with a port).

if m.__promises__PromiseTaskPort then 
    'task logic
else
    'render thread logic
end if

Advanced usage

If you need to subscribe to promises BEFORE entering the event loop, you can preemptively activate "task mode" by calling
promises.initTaskFunctionality() (name TBD). Like this:

sub runTaskThread() 
    promises.initTaskFunctionality()
    promises.observePromise(somePromise, someCallback)

    promises.runEventLoop(m.messagePort, sub(message)
        'your while loop logic goes here, after promises has filtered through promise events
    end function)
end sub

Areas of consideration:

  • new timer concept in tasks since standard timers cause rendevous (or, maybe the message port concept is enough that we don't need timers)
  • promises.isPromise will probably trigger rendezvous because of field or name checks (need to mitigate that)
  • maybe use a separate internal port for promises to send all promise observer events on so we can differentiate

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions