Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

barracks

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

barracks

An event dispatcher for the flux architecture

  • 5.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20
increased by33.33%
Maintainers
1
Weekly downloads
 
Created
Source

barracks

NPM version build status Test coverage Downloads

Event dispatcher for the flux architecture. Provides event composition through this.waitFor() and checks for circular dependencies with a small interface of only 3 functions.

╔═════╗        ╔════════════╗       ╔════════╗       ╔═════════════════╗
║ API ║<──────>║ Middleware ║──────>║ Stores ║──────>║ View Components ║
╚═════╝        ╚════════════╝       ╚════════╝       ╚═════════════════╝
                     ^                                        │
                     │                                        │
               ╔════════════╗                                 │
               ║ Dispatcher ║                                 │
               ╚════════════╝                                 │
                     ^                                        │
                     └────────────────────────────────────────┘

Installation

npm install barracks

Overview

var barracks = require('barracks');

// Initialize dispatcher.

var dispatcher = barracks({
  users: {
    add: function(next) {
      console.log(user + ' got added');
      next();
    }
  },
  courses: {
    get: function(next) {
      console.log('Get ' + this.payload);
      next();
    },
    set: function(next) {
      console.log('Set ' + this.payload);
      next();
    }
  }
});

// Dispatch an event.

dispatcher('users_add', 'Loki');
// => 'Loki got added'

API

dispatcher = barracks(actions)

Initialize a new barracks instance. Returns a function.

// Initialize without namespaces.

var dispatcher = barracks({
  user: function() {},
  group: function() {}
});

// Initialize with namespaces.

var dispatcher = barracks({
  users: {
    add: function() {},
    remove: function() {}
  },
  courses: {
    get: function() {},
    put: function() {}
  }
});
dispatcher(action, data)

barracks() returns a dispatcher function which can be called to dispatch an action. By dispatching an action you call the corresponding function from the dispatcher and pass it data. You can think of it as just calling a function.

In order to access namespaced functions you can delimit your string with underscores. So to access courses.get you'd dispatch the string courses_get.

// Call a non-namespaced action.
dispatcher('group', [123, 'hello']);

// Call a namespaced action.
dispatcher('users_add', {foo: 'bar'});
ctx.waitFor(action)

Execute another function within the dispatcher before proceeding. Registered callbacks are always bound to the scope of the dispatcher, so you can just call this.waitFor to access the function from within a registered callback.

var dispatcher = barracks({
  init: function(next) {
    console.log('1');
    this.waitFor(['add', 'listen'], function() {
      console.log('4');
      next();
    });
  },
  add: function(next) {
    setTimeout(function() {
      console.log('2');
      done();
    }, 10);
  },
  listen: function(next) {
    console.log('3');
    next();
  }
});

dispatcher('init');
// => 1 2 3
ctx.payload

this.payload contains the data provided by dispatcher().

var dispatcher = barracks({
  init: function(next) {
    console.log(this.payload);
  }
});

dispatcher('init', 'fooBar');
// => 'fooBar'
ctx.locals=

this.locals is shared between all (delegated) function calls and acts as the location to share data between function calls. For example when you retrieve a token from a store and want to make it available to all subsequent functions.

The payload provided by dispatcher() is available under this.locals.payload.

var dispatcher = barracks({
  add: function(next) {
      this.locals.token = 'asdf12345';
      next();
    });
  },
  fetch: function(next) {
    this.waitFor(['add'], function() {
      console.log(this.locals.token);
      next();
    });
  }
});

dispatcher('fetch');
// => 'asdf12345'

License

MIT

Keywords

FAQs

Package last updated on 14 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc