The main challenge with once
and pausable
is that they both need to reference both the listener function and the returned handle.
once
:
- Needs to wrap listener to remove handle when run
- Needs handle to be referenced within wrapped listener
pausable
:
- Needs to wrap listener to check a flag
- Needs to add pause/resume functions to handle
Dojo 1 of course tackles this by implementing once
and pausable
as APIs with the same signature as on
itself, and they both call on
internally. This would preclude easily reusing them for other callback/handler-based APIs (e.g. aspect
).
These could ostensibly be written as Dojo 1 style extension events, but that would still not allow them to be used outside of on
, and we've pretty much killed extension events for the time being otherwise.
Here are a few ideas:
- Pass the listener API to the function, e.g.
once(on, node, 'click', handler)
- Terrible because there'd be no way to type-check
once
andpausable
- Terrible because there'd be no way to type-check
- Make APIs for each thing that should be once-able / pausable-able(-able-able...)
- Terrible because Oprah syndrome (and YOU get once and pausable APIs! And YOU get once and pausable APIs! ...)
- I guess this could be maybe not terrible if we limited it, e.g. to
on.once
,on.pausable
, andaspect.once
(presumably a once version ofaspect.on
, though maybeaspect.after
is more useful). - For that matter, if we're thinking of replacing sinon with our own utilities, we could work one in for
aspect.
whatever and then just worry abouton.once
andon.pausable
in core itself. Though then we're basically back to Dojo 1's way.
- Make APIs capable of wrapping the original function and returning a once/pausable version?
- I've started trying to throw together an example of this for
once
and it looks hairier than it's worth. Meanwhile I'm not sure it's feasible forpausable
because the new function needs to return a different type of handle than the old function (with pause/resume).
- I've started trying to throw together an example of this for
I think line 23 should be an array as well.