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
  • Returns
  • Parameters
  • Example
  • From EventEmitter
  • With RxJS Subject
  • With DOM EventTarget
  • With JQuery-like event target
  1. API Documentation
  2. Evt<T>

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

import { Evt } from "evt";
import { EventEmitter } from "events";

const ctx= Evt.newCtx();

const ee= new EventEmitter();
const evtText= Evt.from<string>(ctx, ee, "text");
evtText.attach(text=> console.log(text));

evtText.post("Foo bar");//Prints "Foo bar";

ctx.done();

console.log(ee.listenerCount("text"));//Prints "0"

With RxJS Subject

import { Evt } from "evt";
import { Subject } from "rxjs";

const ctx= Evt.newCtx();

const subject = new Subject<string>();

const evtText = Evt.from(ctx, subject); //The type argument is inferred.

evtText.attach(text=> console.log(text));

subject.next("Foo bar"); //Prints "Foo bar"

ctx.done();

subject.next("Foo bar"); //Prints nothing

With DOM EventTarget

import { Evt } from "evt";

Evt.from(document, "click").attach(()=> console.log("Clicked!"));

With JQuery-like event target

import { Evt } from "evt";

Evt.from([
    $("#btnA"),
    $("#btnB"),
    $("#btnC")
], "click").attach(()=> console.log("Clicked!"));
PreviousEvt.getCtx(object)NextEvt.merge([ evt1, evt2, ... ])

Last updated 2 years ago

********

********

********

Run the example
Run the example
Run the example