proposal-eventual-send
proposal-eventual-send copied to clipboard
expand explanation of presenceHandler
I'm trying to get my head around resolveWithPresence
, and the only description of it I could find (around line 263 in the README) says:
if (presenceHandler) {
// presence is a freshly-created Object.create(null) whose handler
// is presenceHandler. The targetP below will be resolved to this
// presence.
const presence = resolveWithPresence(presenceHandler);
presence.toString = () => 'My Special Presence';
The README does an excellent job of explaining what it means for a Promise to have a handler, but it's not clear what it means for a non-Promise (Object.create(null)
) to have a handler.
What I currently believe is:
- the presence object is created as described
- a new handled Promise is created, with something like
p2 = Promise.resolve(presence)
- that Promise is created in the resolved state
- that Promise is given a handler
presenceHandler
, so any calls toeventualSend
/etc will be routed topresenceHandler
in some future turn
And maybe the equivalent creation code would look like:
const presence = Object.create(null);
const p2 = Promise.delegate((resolve, _rej, _resWithPres) => resolve(presence),
presenceHandler);
return presence;
Furthermore, the reason p2
is interesting, despite being discarded before the caller of resolveWithPresence()
gets to see it, is that subsequent Promise.resolve(presence)
calls will always return this same p2
, and therefore things like Promise.eventualSend(presence, prop, args)
will invoke presenceHandler.eventualSend(presence, prop, args)
, allowing presence
to be used as the same sort of remote proxy target as the original Promise.
Is that accurate?
Does that last presenceHandler.eventualSend()
really get presence
as its first argument? The use of t
in the handler descriptions makes sense in the "Default Behavior" part, where t
is the value to which the Promise has resolved, but I don't immediately understand what t
is supposed to be for the pre-resolution handler methods. In the analogy with Proxy
, I know proxies are given a special "target" object, but I don't see where Promise.delegate
takes a corresponding object.