However, the traditional approach that consists of gathering all the events in a single bus is also an option.
Note: Due to a current TypeScript limitation the .attach()methods need to be prefixed with $ when used with fλ ( to in this case) operators but evt.$attach*() are actually just aliases to the corresponding evt.attach*() methods.
import { Evt, to } from "evt";
const evt = Evt.create<
[ "text", string ] |
[ "time", number ]
>();
evt.$attach(to("text"), text => console.log(text));
evt.$attachOnce(to("time"), time => console.log(time));
evt.post(["text", "hi!"]);
evt.post(["time", 123]);
evt.post(["time", 1234]);
Unlike RxJS operators that return Observable EVT operators are function build using native language features, no by composing other pre-existing operators or instantiating any particular class.
Consider that we have an emitter for this data type:
type Data = {
type: "TEXT";
text: string;
} | {
type: "AGE";
age: number;
};
We want to get a Promise<string> that resolves with the next text event.
import { Subject } from "rxjs";
import { filter, first, map } from "rxjs/operators";
const subject = new Subject<Data>();
const prText = subject
.pipe(
filter(
(data): data is Extract<Data, { type: "TEXT" }> =>
data.type === "TEXT"
),
first(),
map(data => data.text)
)
.toPromise()
;
/* ---------------------------------------------------------------- */
import { Evt } from "evt";
const evt = new Evt<Data>();
const prText = evt.waitFor(
data => data.type !== "TEXT" ?
null : [data.text]
);