-
Notifications
You must be signed in to change notification settings - Fork 6
Description
A problem with PromiseContext.nowOr(_:)
is there's no easy way to tell in the callback whether it's being executed synchronously. If we register the callback from the main thread and use .nowOr(.main)
we could use a local variable that we set to false
after registering the callback, but this is a little awkward and requires knowing what thread you're registering the callback from.
The thought is maybe we could use TLS to store whether the context is executing synchronously or asynchronously and then expose this value in a property like PromiseContext.isExecutingNow
. All cases where a promise callback is invoked should be directly nested inside a call to context.execute
so there should be no risk of returning stale info from this property, but this should be audited.
Question: In order to implement .nowOr
we piped an isSynchronous
flag through the seal enqueuing and context execution. Could we instead use TLS that's set when the seal runs its enqueued callbacks and query it from the context? I didn't implement it that way to begin with because I wasn't sure if there was any way to end up with stale data at the context.execute
call (e.g. if there are any context.execute
s that aren't direct children of a seal.enqueue
), but it may be safe to represent it that way.