doors

A NodeJS module taking the lock and door pattern to inception extremes

npm install doors
13 downloads in the last week
25 downloads in the last month

doors

A NodeJS module taking the lock and door pattern to inception extremes

Doors

What the heck is this?

Design patterns are very trendy. There are two design patterns that we think are really interesting:

  • State Machine - Basically, it allows you to change your application from one state to another (this is called a transition) when initiated by a triggering an event or a condition. You can define an action to execute asynchronously for each transition. You will find a great State machine here.

  • Promises - To use the words of Wikipedia : it represents the value returned from the single completion of an operation. To make it simple, that's a state machine with two immutable transitions : unfulfilled to fulfilled (or unresolve to resolve) and unfulfilled to failed. You will find a great Promises implementation here

Both have something missing. A transition occurs when one condition is triggered. Things are not so simple in real life. You will probably have more than one condition to do something, but one condition is sufficient to not do it. Think about a door with multiple locks: you can't open the door until all locks are unlocked.

Additionally, things can change and are not necessarily immutable. A door which is unlocked is not open until you actually open it and a door which is opened can be closed and locked again.

Getting started

  1. Install the module via npm:
npm install doors
  1. Use doors with NodeJS
var Door = require("doors");

var door = new Door("doors", ["is", "awesome"]);

door.on("open", function() {
  // do something when door is opened
});

door.unlock("is");
door.unlock("awesome");

API

Toggle locks


// unlock by default
door.toggle("awesome");

// lock when false
door.toggle("awesome", false);

// unlock when true
door.toggle("awesome", true);

Pass and BreakIn


// unlock 'is' and 'awesome' locks
door.pass(["awesome", "is"]);

// unlock all locks and open door
door.breakIn();

// unlock all locks but don't open door even if automatic is true
door.breakIn(false);

Options

  • automatic (boolean) - Allow doors to be opened automatically when all locks are unlocked (default true).
  • event (string) - A custom event to fire when door is open (default 'open').
var door = new Door("door", ["lock1", "lock2"], {
  automatic : false
});

door.on("open", function() {
  // do something now that the door is unlocked & open
});

door.unlock("lock1");
door.unlock("lock2");
door.open();

Door piping

This is door inception!

var Door = require("doors");

var child1 = new Door("child1", ["lock1", "lock2"], {
  automatic: false,
  event: 'foo'
});

var child2 = new Door("child2", ["lock3", "lock4"]);

var parent = new Door("parent", ["lock5", child1, child2]);

parent.on("open", function() {
  // the parent door is unlocked
});

child1.on("foo", function() {
  // the child1 door is unlocked
});

child1.unlock("lock1"); // unlock lock1 in the child1 door
parent.unlock("lock5"); // unlock lock5 in parent door
child1.unlock("lock2"); // unlock lock1 in the child1 door
child1.open();
child2.breakIn();

TODO

  • change unlock to accept truthy and falsy argument instead of just boolean
  • make module available client side

License

Copyright 2013 PetroFeed Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you