EVT
HomeGitHub
v1
v1
  • Why EVT ?
  • Overview
  • API Documentation
    • Evt<T>
      • evt.attach*(...)
      • evt.post*(data)
      • evt.waitFor(...)
      • evt.evt[Attach|Detach]
      • evt.pipe(...)
      • evt.getHandlers()
      • evt.isHandled(data)
      • evt.detach(ctx?)
      • evt.enableTrace(...)
      • evt.setMaxHandlers(n)
      • toStateful(initialState)
      • evt.getStatelessOp(op)
      • Evt.create(initalState?)
      • Evt.newCtx<T>()
      • Evt.getCtx(object)
      • Evt.from<T>(...)
      • Evt.merge([ evt1, evt2, ... ])
      • Evt.loosenType(evt)
      • Evt.factorize(evt)
      • Evt.asPostable(evt)
      • Evt.asNonPostable(evt)
      • Evt.setDefaultMaxHandlers(n)
    • Ctx<T>
    • Operator<T, U> (type)
    • StatefulEvt<T>
    • Helper types
    • Handler<T, U> (type)
  • React hooks
  • Extending Evt
Powered by GitBook
On this page
  • ToNonPostableEvt<E>
  • ToPostableEvt<E>
  • UnpackEvt<E>
  • Example:
  • SwapEvtType<E, T>
  • FactorizeEvt<E>
  1. API Documentation

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

import { ToNonPostableEvt } from "evt";

ToNonPostableEvt<Evt<T>>         → NonPostableEvt<T>
ToNonPostableEvt<SatefulEvt<T>>  → StatefulReadonlyEvt<T>
ToNonPostable<VoidEvt>           → NonPostable<Void>
ToNonPostable<NonpostableEvt<T>> → NonPostableEvt<T>

ToNonPostableEvt<{ 
    evtText: Evt<string>; 
    evtCount: StatefulEvt<number>; 
    type: "FOO" 
}> 
 → 
{ 
    evtText: NonPostableEvt<string>; 
    evtCount: StatefulNonpostableEvt<number>; 
    type: "FOO"
}

Example use of the NonPostableEvt interface:

import { Evt, NonPostableEvt } from "evt";

const evtText= new Evt<string>();

//Api to expose.
export const api:{ evtText: NonPostableEvt<string>; } = { evtText };

//evtText exposed by the api cannot be posted…
api.evtText.post //<=== TS error 
api.evtText.postOnceMatched //<===== TS error

//…but we can post internally.
evtText.post("good");

ToPostableEvt<E>

Invert of ToNonPostableEvt

import { 
    ToPostableEvt, 
    NonPostableEvt, 
    StatefulReadonlyEvt
} from "evt";

ToPostableEvt<NonPostableEvt<T>>         → Evt<T>
ToPostableEvt<StatefulReadonlyEvt<T>>    → StatefulEvt<T>
ToPostable<NonPostable<void>>            → VoidEvt
ToPostable<Evt<T>>                       → Evt<T>

ToPostableEvt<{ 
    evtText: NonPostableEvt<string>; 
    evtCount: StatefulReadonlyEvt<number>; 
    type: "FOO" 
}> 
 → 
{ 
    evtText: Evt<string>; 
    evtCount: StatefulEvt<number>; 
    type: "FOO"
}

UnpackEvt<E>

Extract the type argument of an Evt

import { UnpackEvt } from "evt";

UnpackEvt<Evt<number>>            → number
UnpackEvt<StatefulEvt<number>>    → number
UnpackEvt<NonpostableEvt<number>> → number

UnpackEvt<{ 
    evtText: Evt<string>; 
    evtCount: StatefulEvt<number>; 
    type: "FOO" 
}> 
 → 
{ 
    evtText: string; 
    evtCount: number; 
    type: "FOO"
}

Example:

UnpackEvt is a helper type to infer the type argument of an Evt instance.

import { Evt, UnpackEvt } from "evt";

const evtHuman = new Evt<{
    name: string;
    age: number;
    gender: "MALE" | "FEMALE"
}>();


type Human = UnpackEvt<typeof evtHuman>;

const human: Human = {
    "name": "bob",
    "age": 89,
    "gender": "MALE"
};

evtHuman.post(human);

SwapEvtType<E, T>

import { SwapEvtType } from "evt";

SwapEvtType<Evt<string>, number>          → Evt<number>
SwapEvtType<SatefulEvt<string>, number>   → SatefulEvt<number>
SwapEvtType<Evt<number>, void>            → VoidEvt
SwapEvtType<StatefulEvt<number>, void>    → VoidEvt

FactorizeEvt<E>

import { FactorizeEvt } from "evt";

FactorizeEvt<Evt<string> | Evt<number>>     → Evt<string | number>
//...Work as well with StatefulEvt, NonPostable ect
PreviousStatefulEvt<T>NextHandler<T, U> (type)

Last updated 2 years ago

Run the example
Run the example