EVT
Search…
Evt.from<T>(...)
Creates an Evt that post events of a specific type coming from other API that emmits events.

Returns

Evt<T> will post every time the emitter emits

Parameters

Ctx Optional, Allows detaching the handlers attached to the source emitter.
emitter: Any of the following,
    DOM EventTarget
    Node.js EventEmitter
    JQuery-like event target
    RxJS Subject
    An Array, NodeList or HTMLCollection of many of these.
    A promise
Depending of the API the type argument will be inferred or not.
name: The event name of interest, being emitted by the target.

Example

From EventEmitter

1
import { Evt } from "evt";
2
import { EventEmitter } from "events";
3
4
const ctx= Evt.newCtx();
5
6
const ee= new EventEmitter();
7
const evtText= Evt.from<string>(ctx, ee, "text");
8
evtText.attach(text=> console.log(text));
9
10
evtText.post("Foo bar");//Prints "Foo bar";
11
12
ctx.done();
13
14
console.log(ee.listenerCount("text"));//Prints "0"
Copied!

With RxJS Subject

1
import { Evt } from "evt";
2
import { Subject } from "rxjs";
3
4
const ctx= Evt.newCtx();
5
6
const subject = new Subject<string>();
7
8
const evtText = Evt.from(ctx, subject); //The type argument is inferred.
9
10
evtText.attach(text=> console.log(text));
11
12
subject.next("Foo bar"); //Prints "Foo bar"
13
14
ctx.done();
15
16
subject.next("Foo bar"); //Prints nothing
Copied!

With DOM EventTarget

1
import { Evt } from "evt";
2
3
Evt.from(document, "click").attach(()=> console.log("Clicked!"));
Copied!

With JQuery-like event target

1
import { Evt } from "evt";
2
3
Evt.from([
4
$("#btnA"),
5
$("#btnB"),
6
$("#btnC")
7
], "click").attach(()=> console.log("Clicked!"));
Copied!
Last modified 1yr ago