Ctx<T>
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.The only difference between
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
).Detach, from the
Evt
instances they are attached to, all Handlers bound to the context.Once you have called ctx.done() the ctx can't be re-used. If you attach another handler using this ctx, it will be immediately detached.
Calling this method causes the
Evt
returned by ctx.getEvtDone()
to be posted.To test if ctx.done() have been invoked already you can use:
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
forCtx<T>
- none for
VoidCtx
Equivalent of
ctx.done()
to use when the task did not go through.When a fλ operator returns
{ "DETACH": ctx, "err": error }
, ctx.abort(error)
is invoked.ReturnType<ctx.done()>
(cf ctx.done
)Error
an error that describes what went wrong.Tracks when ctx.done or ctx.abort are invoked.
For most use cases, it is more convenient to use
ctx.waitFor([timeout])
- For VoidCtx an Evt that posts:
{ handlers: Handler.WithEvt[] }
whenctx.done()
is called.{ error: Error, handlers: Handler.WithEvt[] }
whenctx.abort(error)
is called.
- For
Ctx<T>
, anEvt
that post:{ result: Result; handlers: Handler.WithEvt[]; }
whenctx.done(result)
is called.{ error: Error, handlers: Handlers.WithEvt[]; }
whenctx.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> }
import { Evt } from "evt";
import { EventEmitter } from "events";
const ctx= Evt.newCtx();
const evtText = new Evt<string>();
const evtTime = new Evt<number>();
evtText.$attach(
text=> [ text.length ],
ctx,
count => console.log("1: " + count)
);
evtTime.waitFor(
time => time < 0,
ctx,
).then(time=> console.log("2: " + time));
evtText
.pipe(ctx)
.pipe(text => [text.toUpperCase()])
.attach(upperCaseText=> console.log("3: " + upperCaseText))
;
Evt.merge(ctx, [ evtText, evtTime ])
.attach(textOrTime => console.log("4: " + textOrTime))
;
const ee= new EventEmitter();
Evt.from<string>(ctx, ee, "text")
.attach(text=> console.log("5: " + text))
;
evtText.post("foo"); //Prints "1: 3" "3: FOO" "4: foo"
ee.emit("text", "bar"); //Prints "5: bar"
console.log(evtText.getHandlers().length); //Prints "3"
console.log(evtTime.getHandlers().length); //Prints "2"
console.log(ee.listenerCount("text")); //Print "1"
ctx.evtDoneOrAborted.attachOnce(
({handlers})=> {
console.log(
handlers.filter(({ evt })=> evt === evtText).length +
" handlers detached from evtText"
);
console.log(
handlers.filter(({ evt })=> evt === evtTime).length +
" handlers detached from evtTime"
);
console.log(
handlers.length + " handlers detached total"
);
}
);
//Prints:
//"3 handlers detached from evtText"
//"2 handlers detached from evtTime"
//"5 handlers detached total"
ctx.done();
console.log(evtText.getHandlers().length); //Prints "0"
console.log(evtTime.getHandlers().length); //Prints "0"
console.log(ee.listenerCount("text")); //Print "0"
evtText.post("foo"); //Prints nothing
ee.emit("text", "bar"); //Prints nothing