Untangle
An event aggregator for using the Publish/Subscribe pattern, also known as Pub/Sub. This version also have a Respond/Request feature.
Used together these two features allow you to create truly decoupled code.
Installation
npm install untangle
Usage Example
Abstract
Untangle is an EventAggregator, or Pub/Sub, library. Untangle is meant to be used in a specific way. It is a library meant to create highly uncoupled code, meaning that classes and object has NO knowledge of each other. The addition, modification, or deletion of a class cannot affect any other class. To achieve this you must use Untangle in a specific way. There are a couple of rules:
- No classes can know about the existence of other classes.
- A class must be completely self-contained.
- All interchange of information must go through the Untangle system.
- All interchanged data must be "primitives", aka. boolean, number, string, or a hash or array consiting of the former datatypes.
Untangle has a concept not normally used in the Pub/Sub pattern, namely Respond/Request. This is in a way the opposite of Pub/Sub. In Untangle you can register that you respond
to a certain message type. Whenever code request
this massage later, then the callback which respond
to this will return data to the requestee.
How to do the basics:
Subscribing
Untangle = {console}UntangleUntangle "data"UntangleUntangle no output
Responding
Untangle = {data + " returned"}Untangleresult = Untangleconsole "data returned" Untangleresult = Untangleconsole null //Note: It returns null, not undefined.
Conveniency methods
By calling Untangle.helpers();
you will gain access to new methods on all string objects. These wraps the Untagle methods for conveniency. The available methods are:
Untangle; //Will activate prototypes on the String class: "MessageType""MessageType""MessageType" "MessageType""MessageType""MessageType" "MessageType""MessageType"
More methods
.subscribeAll + .unSubscribeAll
{console}Untangle //Will receive every published message created. Great for logging all activity in the system."whatever" "Got message of type whatever, with data some data" Untangle "whatever" no output
.reroute + .unReroute
{console}UntangleUntangleUntangleUntangle "test" "data" "data modified" UntangleUntangle"test" no output
.resetAll
Untangle
This will remove everything inside Untangle, all listeners, all responders everything.
For this method to work you must supply the value "HARD"
as input parameter.
Using Untangle on a system scale
Untangle = Untangle //Simulate a class similar to how CoffeeScript does itUser = { var name; { thisname = name; "logData"; "getUserName"; "tick" } Userprototype { return console; }; Userprototype { return thisname; }; Userprototype { if"isThereFish" "I ate some fish" }; return User;}; Fish = { { "isThereFish" "I ate some fish" } Fishprototype { return "getTick" > 10; }; Fishprototype { console; }; return Fish;}; Ticker = { var timer; var tick; Tickerprototype { return thistick; }; { var self = this; "getTick"; thistimer = ; } return Ticker;}; var aUser = "The Users Name";var fish = ;var ticker = ; //... var username = "getUserName"
License
The MIT License (MIT)
Copyright 2015 Stephan Nordnes Eriksen
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.
Thanks to
- Cray-Cray Design for logo.