evt.detach(ctx?)
Similar to EventEmitter.prototype.removeListener()
Detach all handlers from the Evt or all Evt's handler that are bound to a given context.
Calling this method without passing a context argument is almost never a good idea. An Evt instance should be sharable by modules that are isolated one another. If a module take the liberty to call evt.detach() it can brek the code elswhere.
Handler<T,any>[]
array of Handler that have been detached.To detach all handlers at once:
const evtText = new Evt<string>();
//detach with no argument will detach all handlers (attach, attachOnce, waitFor... )
evtText.detach();
Using a context argument
import { Evt } from "evt";
const evtText = new Evt<string>();
evtText.attachOnce(text=> console.log(`Hello ${text}`));
const ctx = Evt.newCtx();
evtText.attach(
ctx,
_text => console.assert(false,"never")
);
evtText.attachOnce(
ctx,
_text => console.assert(false,"never")
);
evtText.detach(ctx);
//"Hello World" will be printed
evtText.post("World");
Last modified 8mo ago