Ctx
helps detach all Handler
s that were attached in the goal of acompishing a certain task once the said task is done or aborted.CtxVoid
and Ctx<void>
is that ctxVoid.done()
can be called without argument when ctx<void>.done(result)
must be called with an argument (null
or undefined
).ctx.done(result?)
Evt
instances they are attached to, all Handlers bound to the context.Evt
returned by ctx.getEvtDone()
to be posted.{ "DETACH": ctx }
, ctx.done()
is invoked.{ "DETACH": ctx, "res": result }
, ctx.done(result)
is invoked.ctx.getEvtDone().postCount !== 0
ReturnType<ctx.getHandlers()>
All the Handlers that were bound to the context. They are now detached, calling ctx.getHandler()
just after ctx.done()
returns an empty array.T
for Ctx<T>
VoidCtx
ctx.abort(error)
ctx.done()
to use when the task did not go through.{ "DETACH": ctx, "err": error }
, ctx.abort(error)
is invoked.ReturnType<ctx.done()>
(cf ctx.done
)Error
an error that describes what went wrong.ctx.evtDoneOrAborted
ctx.waitFor([timeout])
{ handlers: Handler.WithEvt[] }
when ctx.done()
is called.{ error: Error, handlers: Handler.WithEvt[] }
when ctx.abort(error)
is called.Ctx<T>
, an Evt
that post:{ result: Result; handlers: Handler.WithEvt[]; }
when ctx.done(result)
is called.{ error: Error, handlers: Handlers.WithEvt[]; }
when ctx.abort(error)
is called.Handler.WithEvt<T>
is just a type alias for an object that wraps a handler and the Evt
it is attached to: { handler: Handler<T, any>, evt: Evt<T> }
ctx.waitFor([timeout])
ctx.done()
or ctx.abort()
is invoked.Promise<T>
(T
is the type argument of Ctx<T>
) A promise that resolve when ctx.done([result]) is invoked.ctx.abort(error)
is invoked before ctx.done()
the promise rejects with error
.ctx.done()
was not invoked within timeout
milliseconds. If it happens ctx.abort(timeoutError)
is internally invoked timeoutError
being an instance of EvtError.Timeout
.number
Optional, number of milliseconds before the promise reject if it hasn't fulfilled within this delay.ctx.getHandlers()
Handler.WithEvt[]
The Handler
s that are bound to the context alongside with the Evt
instance each one is attached to. The Handlers that are bound to the context but no longer attached to an Evt are not listed ( they are usually freed from memory anyway as there should be nor reference left of them as soon as they are detached ).ctx.evtAttach
Evt<Handler.WithEvt<any>>
An Evt that posts every time a new handler bound to the context is attached.ctx.evtDetach
ctx.getEvtAttach()
but post when handlers are detached. Note that a handler being detached does not mean that it has been explicitly detached. One-time handlers and handlers that have timed out are automatically detached.Ctx
. The task is to download a file, we know the size of the file to download, we have an Evt<Uint8Array>
that emits chunks of data, we want to accumulate them until we reach the expected file size. Multiple things can go wrong during the download:Promise<Uint8Array>
that resolves with the downloaded file or reject if anything went wrong.Ctx<Uint8Array>
:Ctx
enforce that there is no left over handlers on the Evt
passed as input once the download attempt has completed.