GithubHelp home page GithubHelp logo

barracks-promisify-plugin's Introduction

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) => ({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'))

What about choo

This plugin works perfectly fine with choo as well, since choo is just an opinionated wrapper around barracks.

Checkout the example folder for a full choo app using this plugin.

Tests

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

See test.js and try out npm t

License

MIT โ†’ See LICENSE file.

barracks-promisify-plugin's People

Contributors

myobie avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.