Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including rx-helper with all npm packages installed. Try it out:

const { interval } = require("rxjs") const { Agent, after } = require("rx-helper") const agent = new Agent() const log = (...args) => console.log(...args) // 1. Filters - Synchronous functions driven by events agent.filter(/.*/, ({ event }) => { console.log("Saw: " + JSON.stringify(event).replace(/"/g, "'")) }) // 2. Handlers - Do some work, often async; Return an Observable that the agent can subscribe to. agent.on( "hello", () => after(50, () => { log("- Hello World!") return "Zzz" }), { type: "fallAsleep", name: "sleep" } ) // 3. You can query all the agent sees, using getNextEvent and allEventsOfType, // which return Observable<Event> and Promise<Event>, respectively.. agent.getNextEvent("fallAsleep").then(event => { console.log("- Current status: " + event.payload) }) // 4. Tell the agent things to do (events) by // calling agent.process(event) or agent.trigger(type, payload) const result = agent.process({ type: "hello" }) // 5. The result can get a Promise for all handlers that run on account of this event. result.completed.sleep.then(sound => log("- Good night." + sound)) // 6. If you have an Observable (such as mouse clicks, or ticks // of time, its values can be processed in events of a given type. const sub = agent.subscribe(interval(1000), { type: "tick" }) sub.add(() => { console.log("Wake up!") }) await after(3500, () => sub.unsubscribe()).toPromise() /* Extended Comments 1. Filters run first - You can set up a filter to show all events the agent sees, before they are further processed. If they throw an exception, no handlers are run. 2. Handlers are functions which fire upon events of a certain type, in this case 'hello'. Its return value, the return value of `after`, will be an Observable of 'Zzz' after 50 milliseconds. The agent will subscribe to this, and further process these value in the payload of event(s) of type 'snore' 5. result.completed is an aggregate Promise: result.completed.then(({ sleep }) => ).then(r => log(`Good night. ${sleep}`)) while each handler is available under their name: result.completed.sleep.then(...) 6. If you have an Observable, and you want to process an event for each notification, simply give it to agent.subscribe, and specify a type argument. End the subscription by calling unsubscribe() */

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

rx-helper v2.1.5

Structure your applications around events. Focus on causes ⟺ effects.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free