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-flow

v0.0.8

Published

Event Flow: Functional JavaScript event plumbing

Downloads

5

Readme

Event Flow

npm install --save event-flow

Functional JavaScript event plumbing

Event Flow is a declarative syntax for expressing how data moves through functions.

Quick overview

  • Define an event as a function that 1) is called with other functions, and 2) calls those other functions in the future. Let's call those other functions "delegates". This model is consistent with how Redux's store.subscribe function behaves.
  • Delegates come in two flavors. 1) One is defined by .call( ... ) which simply calls the given function, and 2) the other is defined by .pipe( ... ).to( ... ), which calls the pipe function with the to function. The advantage of approach #2 is that you can generalize how data flows to the to function. The pipe may choose to call it once, never, many times, after a Promise is resolved, etc... depending on the event arguments. And, the logic of how data flows to the to function is decoupled from the function itself. For example, you could call to with Redux store.dispatch if you're piping actions.

API

createEvent(event)

  • event is a function of a function, e.g. Redux store.subscribe.
  • Returns a new instance of the EventFlow class.
  • (Note: All methods of EventFlow are composable, meaning that they all return this.)

EventFlow.pass(source)

  • source is a function called at event time. Its return value is passed as an argument to each event delegate. If pass is called multiple times, then multiple arguments will be passed in the order of the calls.
  • Returns this.

EventFlow.call(delegate)

  • delegate is a function called at event time with arguments.
  • Returns this.

EventFlow.pipe(delegate)

  • delegate is a function called at event time with a "yield" function, plus arguments. delegate may asynchronously call the "yield" function, which is defined by the next call to EventFlow.to.
  • Returns this.

EventFlow.to(yield_function)

  • Defines the yield function passed to a pipe delegate, which is defined by the previous call to EventFlow.pipe.
  • Returns this.

Simple example with Redux

import { createEvent } from 'event-flow';
import { createStore } from 'redux';

const store = createStore((state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'RESET':
      return 0;
    default:
      return state;
  }
});

const actions = (push, state) => {
  if (state === 10) {
    push({ type: 'RESET' });
    setTimeout(() => push({ type: 'RESET' }), 500);
  }
};

// Create a simple event flow that dispatches actions after a change to state (and logs the state).
createEvent(store.subscribe)
  .pass(store.getState)
  .call(state => console.log(`state = ${state}`))
  .pipe(actions).to(store.dispatch);

// Dispatch INCREMENT actions every 100ms.
setInterval(() => store.dispatch({ type: 'INCREMENT' }), 100);


Console output:

state = 1
state = 2
state = 3
state = 4
state = 5
state = 6
state = 7
state = 8
state = 9
state = 10
state = 0
state = 1
state = 2
state = 3
state = 4
state = 0
state = 1
state = 2
state = 3
state = 4
state = 5
state = 6
state = 7
state = 8
state = 9
state = 10
state = 0
state = 1
state = 2
state = 3
state = 4
state = 0
state = 1
state = 2
state = 3
state = 4
state = 5
state = 6
state = 7
state = 8
state = 9
state = 10
state = 0

etc...