npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

event-saga

v4.0.0

Published

Saga storage for EventStream

Downloads

3

Readme

EventSaga

NPM

Simple saga system listening to events from an EventEmitter

Installation

npm install --save event-saga

Usage

// create a normal EventEmitter
var emitter = new EventEmitter();

// create the saga that listens to the EventEmitter
var saga = new EventSaga(emitter, saga => {

  //Create a new saga whenever the 'logon' event is emitted
  saga.createOn('logon', function(data){
    //a new saga has been made
    //the data has the data emitted by the EventEmitter
    //this.data is the saga storage
    this.data.shoppingCart = [];
    this.data.username = data.username;

    //call this event in 60 seconds
    this.setTimeout('emptyCart', 60*1000);
  });

  //Only handle this event if the saga exists (if logon has happened already)
  //You can also use fat arrow functions, and use the second parameter (actor)
  //instead of using `this`.
  saga.on('itemPlacedInShoppingCart', (data, actor) => {
    actor.data.shoppingCart.push(data.item);

    //this replaces the previous timeout with the same name
    actor.setTimeout('emptyCart', {}, 60*1000);
  });

  saga.on('emptyCart', (_, actor) => {
    //finalize the saga, clearing away all data and timeouts
    actor.done();
  })

  saga.on('checkout', function(data){
    //trigger a side-effect
    buyItems(this.username, this.data.shoppingCart);
    //finalize the saga.
    this.done();
  });
});

API

new EventSaga(eventEmitter, saga => { /* initialize here */ })

Creates a new saga that will react to events from the eventEmitter.

EventSaga uses the revealing constructor pattern, where the second argument to the constructor is the executor function. This is a function that will receive one parameter, the saga object.

saga.createOn(event, reaction)

Listens to events from the event emitter, creates a new saga if one doesn't already exist for the id and reacts to the event.

The event data should have an id field that will be used to map the event to the correct saga instance.

If a saga instance doesn't exist for the given id, one will be created. It can be accessed using this.data or actor.data inside the reaction.

saga.createOn('start', function(data, actor){
  this.data.value = data.value;
  assert(this === actor);
});

emitter.emit('start', {id:1, value:15});
emitter.emit('start', {id:2, value:-7});

saga.on(event, reaction)

Listens to events from the event emitter and reacts to the event, but only if a saga instance with the id provided already exists.

The event data should have an id field that will be used to map the event to the correct saga instance.

The saga instance can be accessed using this.data or actor.data inside the reaction.

saga.on('change', function(data, actor){
  this.data.value = data.value;
  assert(this === actor);
});

emitter.emit('change', {id:1, value:12});
emitter.emit('change', {id:2, value:-9});
emitter.emit('change', {id:3, value:0}); // nothing will happen, since there is no saga for id:3

actor.data

The saga instance object. Store data on this object. It can be anything.

actor.id

The saga id. Readonly

actor.done()

Destroys the actor instance.

saga.on('stop', (data, actor) => {
  console.log(actor.data.value);
  actor.done();
});

emitter.emit('start', {id:1, value:12});
emitter.emit('change', {id:1, value:-9});
emitter.emit('stop', {id:1}); // will console.log -9
emitter.emit('change', {id:1, value:0}); // nothing will happen, since there is no saga anymore for id:1

actor.setTimeout(event, data, milliseconds)

Schedules an event in the future. If a timeout with the same event name has been scheduled already, it will be replaced.

actor.clearTimeout(event)

Unschedules an event scheduled using actor.setTimeout().

actor.emit(event, data)

Emits an event on the EventEmitter