Door asynchronous pattern
A door represents the eventual result of an asynchronous operation. This operation is trigerred by an
openevent and depend on multiple conditions (
Install with component:
$ component install bredele/doors
Install with nodejs:
$ npm install bredele-doors
$ <script src="doors.js"></script>
You will find some examples here. Make sure you build the component first.
Design patterns are very trendy. There are two design patterns that 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.
Doors constructor. Initialize door with a key (mandatory) and an optional array of locks.
var Doors = require'doors';var door = 'github';
Return true if has lock.
Add string lock or child door.
Lock a previously added lock.
doorlock'olivier'; //lock 'olivier'doorlock'olivier' 'amy'; //lock 'olivier' and 'amy'doorlock; //lock all the locks
Unlock door's lock(s).
doorunlock'olivier'; //unlock 'olivier'doorunlock'olivier' 'amy'; //unlock 'olivier' and 'amy'doorunlock; //unlock all the locks
open event when all the locks are unlocked.
doortoggle'olivier'; //lockdoortoggle'olivier' true; //unlock
Open the door only if all locks are unlocked. and emit open event.
Listen door events.
The MIT License (MIT)
Copyright (c) 2014 Olivier Wietrich email@example.com
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.