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

uniflow

v1.1.0

Published

A flux-inspired unidirectional data flow library.

Downloads

311

Readme

Uniflow

Uniflow is a flux-inspired unidirectional data flow library. It works great with React, but it could be used just as easily with any other view library. The primary goal of Uniflow is simplicity. The entire lib directory can be read and understood in minutes. Give it a try!

Features

Actions

  • An actions object is an EventEmitter (eventemitter3).
  • Action methods are auto-bound to the actions object. This is great for passing actions directly as callbacks to other functions.
  • Action methods have a partial method. It does what you would expect. For example: <button onClick={itemActions.deleteItem.partial(this.props.id)}>Delete</button>.
  • Action methods emit events using this.emit('event-name', payload).
  • Async code belongs here.

Stores

  • A store object is an EventEmitter. (Notice the pattern?)
  • The store.state property should only be mutated using store.setState() or store.replaceState()
  • Emits a 'change' event when the state changes. It uses shallow equality to test if state has changed similar to how PureRenderMixin works in React.
  • Works well with Immutable.js values as properties of state.
  • Should never contain async code.

Dispatcher

  • There is no dispatcher!

Installation

$ npm install uniflow --save

Usage

Example

var uniflow = require('uniflow')
var superagent = require('superagent')
var resourceUrl = '[some url]'


// define actions
var PersonActions = uniflow.createActions({
  changeName(first, last) {
    this.emit('name-change-pending', first, last)
    // async code always belongs in an action
    superagent
      .put(resourceUrl)
      .send({first, last})
      .end(this.changeNameResponse) // use other actions as callbacks
  },
  changeNameResponse(err, res) {
    if (err) {
      return this.emit('name-change-error', err)
    }
    this.emit('name-change-success', res.body.first, res.body.last)
  }
})


// define store
var PersonStore = uniflow.createStore({
  fullName() {
    return this.state.first + ' ' + this.state.last
  }
})


// stores subscribe to actions
PersonActions.on('name-change-pending', function(first, last) {
  PersonStore.setState({first, last, status: 'pending'})
})

PersonActions.on('name-change-success', function(first, last) {
  PersonStore.setState({first, last, status: 'saved'})
})

PersonActions.on('name-change-error', function(error) {
  PersonStore.setState({error, status: 'error'})
})


// views subscribe to stores
PersonStore.on('change', function() {
  if (PersonStore.state.status === 'error') {
    return console.error(PersonStore.state.error)
  }
  console.log(PersonStore.fullName())
})


// views initiate actions
PersonActions.changeName('Tobias', 'Funke')

API

Actions

actions = uniflow.createActions(proto)
  • proto object

Creates an Actions object with all of the properties of proto. Within the methods of proto be sure to call this.emit('<name of action>') for listening stores to update properly. Asynchronous tasks, like fetching data, should be performed in Actions.

actions.on, actions.once, actions.emit, etc.

See eventemitter3 and Node.js events documentation for details.

Store

store = uniflow.createStore(proto)
  • proto object

Creates a Store object with all of the properties of proto. A Store should listen to Actions and call this.setState(newState) to keep itself up to date. A change event will be emitted automatically when the Store has updated its state. Stores should be completely synchronous.

store.state

Holds the current values for the store. By default, the initial state is an empty object ({}). You can override the initial state by declaring a state property in proto.

store.setState(newState)
  • newState object

Merges newState with the current state. If any properties have changed, store emits a "change" event. This comparison is shallow, so see the following examples to ensure "change" occurs when you expect it to.

// don't ever do this
this.state.foo = 'updated';
this.setState(this.state);

// do this instead
this.setState({ foo:'updated' });


// don't do this either
var bar = this.state.bar;
bar.baz = 'updated';
this.setState({ bar:bar });

// do something like this instead
var bar = _.assign({}, this.state.bar, { baz:'updated' });
this.setState({ bar:bar });
store.on, store.once, store.emit, etc.

See eventemitter3 and Node.js events documentation for details.