EVT
Search…
Evt.asPostable(evt)
Cast the passed event as portable.
Evt.asNonPostable() is the identity function with special type annotation
Use this method only onEvt you instantiated yourself. Not as a hack to trigger events on Evt that have been exposed as non-postable by an API.
To invoke post() on a NonPostableEvt or a StatefullReadonlyEvt.

Usecase:

Without this method this would be the way for a class to expose Evt that are posted internally and exposed to be listened.
1
import { Evt } from "evt";
2
3
class Socket2 {
4
5
private readonly _evtIsConnected= Evt.create(false);
6
private readonly _evtMessage= Evt.create<Uint8Array>();
7
8
readonly evtIsConnected= Evt.asNonPostable(this._evtIsConnected);
9
readonly evtMessage= Evt.asNonPostable(this._evtMessage);
10
11
/*
12
OR, more explicit but require to repeat the types and to
13
import type { StatefulReadonlyEvt, NonPostableEvt } from "evt";
14
15
readonly evtIsConnected: StatefulReadonlyEvt<boolean>= this._evtIsConnected;
16
readonly evtMessage: NonPostableEvt<Uint8Array> = this._evtMessage;
17
*/
18
19
constructor(){
20
21
this._evtIsConnected.state = true;
22
this._evtMessage.post(new Uint8Array(111));
23
24
}
25
26
}
Copied!
Now it can be frustrating to have to store a private property only to call post on a object that we know is postable. Here is were this method come in handy:
1
class Socket {
2
3
readonly evtIsConnected= Evt.asNonPostable(Evt.create(false));
4
readonly evtMessage= Evt.asNonPostable(Evt.create<Uint8Array>());
5
6
constructor(){
7
8
Evt.asPostable(this.evtIsConnected).state = true;
9
Evt.asPostable(this.evtMessage).post(new Uint8Array(111));
10
11
}
12
13
14
}
Copied!
Last modified 1yr ago
Copy link
Contents
Usecase: