EVT
Search…
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.
The prefered way of detaching handler in TS-EVT is via Ctx<T> .
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.
To chery pick the handlers to detach use evt.getHandlers() or ctx.getHandlers()``

Returns

Handler<T,any>[] array of Handler that have been detached.

Parameters

ctx?: Ctx If Ctx is provided only Handler bound to the given context will be removed.

Examples

To detach all handlers at once:
1
const evtText = new Evt<string>();
2
//detach with no argument will detach all handlers (attach, attachOnce, waitFor... )
3
evtText.detach();
Copied!
Using a context argument
1
import { Evt } from "evt";
2
3
const evtText = new Evt<string>();
4
5
evtText.attachOnce(text=> console.log(`Hello ${text}`));
6
7
const ctx = Evt.newCtx();
8
9
evtText.attach(
10
ctx,
11
_text => console.assert(false,"never")
12
);
13
14
evtText.attachOnce(
15
ctx,
16
_text => console.assert(false,"never")
17
);
18
19
evtText.detach(ctx);
20
21
//"Hello World" will be printed
22
evtText.post("World");
Copied!
Last modified 1yr ago
Copy link