barracks-promisify-plugin

0.0.1 • Public • Published

Promisify Barracks

What is barracks?

From https://github.com/yoshuawuyts/barracks:

Action dispatcher for unidirectional data flows. Creates tiny models of data that can be accessed with actions through a small API.

This plugin makes subscriptions and effects work with promises. It also wraps send inside subscriptions and effects to return a promise which is resolved when it has completely finished. Basically done is called automatically when the promise is resolved or rejected.

Usage

const promisifyPlugin = require('barracks-promisify-plugin')
const barracks = require('barracks')
 
function wait (timeout) {
  return new Promise(resolve => setTimeout(() => resolve(), timeout))
}
 
const store = barracks()
store.use(promisifyPlugin())
store.model({
  state: { stars: null },
  reducers: {
    updateStargazersCount: (state, data) => {
      return { stars: data }
    }
  },
  effects: {
    fetchStargazersCount: async (state, data, send) => {
      const response = await fetch("https://api.github.com/repos/yarnpkg/yarn")
      const json = await response.json()
      const count = json.stargazers_count
      await send('updateStargazersCount', count)
    }
  },
  subscriptions: {
    regularlyFetchStargazersCount: async (send) => {
      while (true) {
        await send('fetchStargazersCount')
        await wait(60000)
      }
    }
  }
})
const createSend = store.start()
const send = promisifyPlugin.promisifySend(createSend('send'))
send('updateStargazersCount', 1).then(() => console.log('up and running'))

Tests

I'm using blue-tape to write simple tests.

See test.js and try out npm t

License

MIT → See LICENSE file.

Readme

Keywords

Package Sidebar

Install

npm i barracks-promisify-plugin

Weekly Downloads

0

Version

0.0.1

License

MIT

Last publish

Collaborators

  • myobie