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

state-dispatch

v0.0.1

Published

simple state management

Downloads

5

Readme

state-dispatch

Send messages to change state.

npm travis standard conduct

Install

npm install --save state-dispatch

Work in progress

Currently this is just a sketch of an idea. The module works, but is it good? I guess we'll find out.

Usage example

var stateDispatch = require('state-dispatch')
var xhr = require('xhr')

var dispatcher = stateDispatch({
  state: { count: 0 },
  initialize: function (send, done) {
    setInterval(function () {
      send('getAmount')
    }, 1000)
    done()
  },
  actions: {
    getAmount: function (state, data, send, done) {
      xhr({ url: '/data.json', json: true }, function (err, res, body) {
        send('increase', body.amount, done)
      })
    }
  },
  reducers: {
    increase: function (state, data) {
      state.count += data
      return state
    }
  },
  update: function (state, prev, send) {
    console.log('state', state)
    console.log('prev', prev)
  }
})

dispatcher.start()

About

Taking inspiration from redux, barracks, send-action, elm, and others, state-dispatch is a variant on unidirectional state management.

The goal is to be simpler than barracks but with better async & logging support than send-action.

With state-dispatch there are terms and concepts that are similar to approaches found in elm, redux, and choo. But things are a little bit different.

There are a few concepts to look at:

  • send – the send function sends messages to trigger state changes
  • messages – a message has a name and optionally some data and is sent with the send function. a message can trigger a reducer or action
  • state – the global state object is only modified using reducers
  • reducers – perform state changes. must be synchronous.
  • actions – perform async work inside actions that then use send to make state changes with reducers
  • initialize – the initialize function is called when dispatcher.start is called and has access to the send function
  • update – the update function is where we use state changes to rerender the UI of the app
  • hooks – the global state has hooks available for logging and storage purposes.

Messages & send

Messages are more of a concept in state-dispatch than a specific piece of code. A message is similar to an event in an event emitter.

Messages are sent using the send function. Sending a message triggers a reducer or and action.

A message has a name and optionally some kind of data.

The name of a message is a string, and refers to the name of a reducer or action.

The second argument is the data, and can be a string, number, array, or object. The data argument is optional.

The send function is accessible in three places:

1. The return value of dispatcher.start():

var send = dispatcher.start()
send('example' { example: true })

2. As the first argument of the initialize function:

stateDispatch({
  state: {}
  initialize: function (send, done) {
    send('example', { example: true }, done)
  }
})

3. As the third argument of an action:

stateDispatch({
  state: {}
  actions: {
    apiGet: function (state, data, send, done) {
      api.get('data', function (err, data) {
        if (err) return send('error', err)
        send('data', data, done)
      })
    }
  }
}) 

state

The state object can be any arbitrary data.

Reducers

Reducers are similar to reducers in redux & choo.

A reducer is a function with two arguments: state & data.

state

The state argument is the current internal state of the dispatcher. This is a copy of the dispatcher's internal state, so changing this object does not update the state directly. It's possible to make changes to this object and return it to effectively update the state. The object returned by a reducer is then used to extend the internal state object of the dispatcher.

data

The data argument is the incoming data of the message used to update the state.

Actions

In state-dispatch actions are for performing asynchronous tasks like fetching data from an API server.

An action is a function with four arguments: state, data, send, done.

state

The state argument is the current internal state of the dispatcher. This is a copy of the internal state object, so changing this object does not update the state directly.

data

The data argument is the incoming data sent by the message that triggered the action.

send

The send argument is a function that you can use to send additional messages to trigger other actions. Using the send function is optional.

done

Call the done function when the asynchronous task is complete.

actions example

Here's an example making an API request:

function getExample (state, data, save, send) {
  apiClient.get('/example', function (err, res, body) {
    if (err) return send('error', err)
    send('exampleResponse', body, done)
  })
}

Note that done is passed in as the last argument to send. This is a convenient shorthand for calling the done function.

These two usages are equivalent:

send('exampleResponse', body, done)
send('exampleResponse', body)
done()

Initialize

The initialize function is called once after calling state.start(). The function is two arguments the send function, and a done function.

Similar to actions, the done function is used to indicate that any async tasks have completed, and can be passed in as the third argument to send.

This lets us create ongoing processes like websockets, make an initial call to an API, or set up an event listener to handle events like clicks or keypresses.

Here's an example based on setting up an event listener:

stateDispatch({
  state: {
    list: [],
    clicks: 0
  },
  initialize: function (send, done) {
    document.body.addEventListener('click', function (e) {
      send('click', done)
    })
  },
  reducers: {
    click: function (state, data, save) {
      state.clicks++
      return state
    }
  }
})

Update

The update function is where we listen for changes to state, and rerender the UI of the app.

Here's an example showing usage with yo-yo:

Hooks

These hooks are available:

  • beforeStart
  • afterStart
  • beforeSend
  • beforeAction
  • afterAction
  • beforeStateChange
  • afterStateChange

An example with all hooks:

var hooks = {
  beforeStart: function (state) {
    console.log(state)
  },
  afterStart: function (state) {
    console.log(state)
  },
  beforeSend: function (name, data, state) {
    console.log(name, data, state)
  },
  beforeAction: function (name, data, state) {
    console.log(name, data, state)
  },
  afterAction: function (name, data, state, prev) {
    console.log(name, data, state, prev)
  },
  beforeStateChange: function (name, data, state) {
    console.log(name, data, state)
  },
  afterStateChange: function (name, data, state, prev) {
    console.log(name, data, state, prev)
  }
}

var dispatcher = stateDispatch({
  hooks: hooks,
  state: {},
  reducers: {},
  actions: {},
  initialize: {},
  update: function (state, prev, send) {}
})

Contributing

Contributions are welcome! Please read the contributing guidelines first.

Conduct

It is important that this project contributes to a friendly, safe, and welcoming environment for all. Read this project's code of conduct

Changelog

Read about the changes to this project in CHANGELOG.md. The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Contact

License

ISC