npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

untangle

v0.0.6

Published

Untangle is a library meant to create highly uncoupled code

Downloads

49

Readme

Untangle

Untangle

Dependency status devDependency Status Build Status

NPM

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:

  1. No classes can know about the existence of other classes.
  2. A class must be completely self-contained.
  3. All interchange of information must go through the Untangle system.
  4. 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 = require("untangle")

callback = function(data){console.log(data)}
Untangle.subscribe("MessageType", callback)
Untangle.publish("MessageType", "data")
=> "data"
Untangle.unSubscribe("MessageType", callback)
Untangle.publish("MessageType", "data")
=> no output

Responding

Untangle = require("untangle")

callback = function(data){data + " returned"}
Untangle.respond("MessageType", callback)
result = Untangle.request("MessageType", "data")
console.log(result)
=> "data returned"

Untangle.unRespond("MessageType", callback)
result = Untangle.request("MessageType", "data")
console.log(result)
=> 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.helpers(); //Will activate prototypes on the String class:

"MessageType".subscribe(callback)
"MessageType".unSubscribe(callback)
"MessageType".publish("data")

"MessageType".respond(callback)
"MessageType".unRespond(callback)
"MessageType".request("data")

"MessageType".reroute("MessageType2")
"MessageType".unReroute("MessageType2")

More methods

.subscribeAll + .unSubscribeAll

callback = function(messageType, data){console.log("Got message of type " + messageType + ", with data " + data)}
Untangle.subscribeAll(callback) //Will receive every published message created. Great for logging all activity in the system.
"whatever".publish("some data")
=> "Got message of type whatever, with data some data"

Untangle.unSubscribeAll(callback) 
"whatever".publish("some data")
=> no output

.reroute + .unReroute

callback = function( data){console.log(data)}
Untangle.subscribe("test2", callback)
Untangle.subscribe("test3", callback)
Untangle.reroute("test", "test2")
Untangle.reroute("test", "test3", function(data){data + " modified"})

"test".publish("data")

=> "data"
=> "data modified"

Untangle.unReroute("test", "test2")
Untangle.unReroute("test", "test3")
"test".publish("data")

=> no output

.resetAll

	Untangle.resetAll(data)

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 = require("untangle")
Untangle.helpers()

//Simulate a class similar to how CoffeeScript does it
User = (function() {
  var name;
  function User(name) {
    this.name = name;
    "logData".subscribe(this.loggerMethod);
    "getUserName".respond(this.getName.bind(this));
    "tick".subscribe(this.eatIfFish)
  }
  User.prototype.loggerMethod = function(data) {
    return console.log(data);
  };
  User.prototype.getName = function() {
    return this.name;
  };
  User.prototype.eatIfFish = function(tick) {
    if("isThereFish".request()){
      "I ate some fish".publish("and it was good")
    }
  };

  return User;
})();

Fish = (function() {
  function Fish() {
    "isThereFish".respond(this.isFish)
    "I ate some fish".subscribe(this.eatFish)
  }
  Fish.prototype.isFish = function() {
    return "getTick".request() > 10;
  };
  Fish.prototype.eatFish = function(data) {
    console.log("Omg! so horrible! And you even said " + data);
  };

  return Fish;
})();

Ticker = (function() {
  var timer;
  var tick;
  Ticker.prototype.getTick = function() {
    return this.tick;
  };
  function Ticker() {
    var self = this;
    "getTick".respond(this.getTick.bind(this));
    this.timer = setInterval(function() {
      if(!self.tick){self.tick = 0}
      self.tick += 1;
      "tick".publish(self.tick);
    }, 500);
  }
  
  return Ticker;
})();

var aUser = new User("The Users Name");
var fish = new Fish();
var ticker = new Ticker();

//...

var username = "getUserName".request()

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.