EVT
Search…
Helper types

ToNonPostableEvt<E>

NonPostableEvt<T> and StatefulReadonlyEvt<T> are interfaces implemented respectively by the classes Evt<T> and StatefulEvt<T>. They contains all the methods but the ones used to post events, namely: .post(), .postOnceHandled() and the .state setter for StatefulReadonlyEvt
1
import { ToNonPostableEvt } from "evt";
2
3
ToNonPostableEvt<Evt<T>> → NonPostableEvt<T>
4
ToNonPostableEvt<SatefulEvt<T>> → StatefulReadonlyEvt<T>
5
ToNonPostable<VoidEvt> → NonPostable<Void>
6
ToNonPostable<NonpostableEvt<T>> → NonPostableEvt<T>
7
8
ToNonPostableEvt<{
9
evtText: Evt<string>;
10
evtCount: StatefulEvt<number>;
11
type: "FOO"
12
}>
13
14
{
15
evtText: NonPostableEvt<string>;
16
evtCount: StatefulNonpostableEvt<number>;
17
type: "FOO"
18
}
Copied!
Example use of the NonPostableEvt interface:
1
import { Evt, NonPostableEvt } from "evt";
2
3
const evtText= new Evt<string>();
4
5
//Api to expose.
6
export const api:{ evtText: NonPostableEvt<string>; } = { evtText };
7
8
//evtText exposed by the api cannot be posted…
9
api.evtText.post //<=== TS error
10
api.evtText.postOnceMatched //<===== TS error
11
12
//…but we can post internally.
13
evtText.post("good");
Copied!

ToPostableEvt<E>

Invert of ToNonPostableEvt
1
import {
2
ToPostableEvt,
3
NonPostableEvt,
4
StatefulReadonlyEvt
5
} from "evt";
6
7
ToPostableEvt<NonPostableEvt<T>> → Evt<T>
8
ToPostableEvt<StatefulReadonlyEvt<T>> → StatefulEvt<T>
9
ToPostable<NonPostable<void>> → VoidEvt
10
ToPostable<Evt<T>> → Evt<T>
11
12
ToPostableEvt<{
13
evtText: NonPostableEvt<string>;
14
evtCount: StatefulReadonlyEvt<number>;
15
type: "FOO"
16
}>
17
18
{
19
evtText: Evt<string>;
20
evtCount: StatefulEvt<number>;
21
type: "FOO"
22
}
Copied!

UnpackEvt<E>

Extract the type argument of an Evt
1
import { UnpackEvt } from "evt";
2
3
UnpackEvt<Evt<number>>number
4
UnpackEvt<StatefulEvt<number>>number
5
UnpackEvt<NonpostableEvt<number>>number
6
7
UnpackEvt<{
8
evtText: Evt<string>;
9
evtCount: StatefulEvt<number>;
10
type: "FOO"
11
}>
12
13
{
14
evtText: string;
15
evtCount: number;
16
type: "FOO"
17
}
Copied!

Example:

UnpackEvt is a helper type to infer the type argument of an Evt instance.
1
import { Evt, UnpackEvt } from "evt";
2
3
const evtHuman = new Evt<{
4
name: string;
5
age: number;
6
gender: "MALE" | "FEMALE"
7
}>();
8
9
10
type Human = UnpackEvt<typeof evtHuman>;
11
12
const human: Human = {
13
"name": "bob",
14
"age": 89,
15
"gender": "MALE"
16
};
17
18
evtHuman.post(human);
Copied!

SwapEvtType<E, T>

1
import { SwapEvtType } from "evt";
2
3
SwapEvtType<Evt<string>, number> → Evt<number>
4
SwapEvtType<SatefulEvt<string>, number> → SatefulEvt<number>
5
SwapEvtType<Evt<number>, void> → VoidEvt
6
SwapEvtType<StatefulEvt<number>, void> → VoidEvt
Copied!

FactorizeEvt<E>

1
import { FactorizeEvt } from "evt";
2
3
FactorizeEvt<Evt<string> | Evt<number>> → Evt<string | number>
4
//...Work as well with StatefulEvt, NonPostable ect
Copied!