Code Monkey home page Code Monkey logo

belfry.js's Introduction

Belfry.js

A simple publish subscribe library returning curried functions to work with cumin.

Codeship Status for CrowdHailer/belfry.js

Code Climate

Code Climate

Installation

Belfry relies on cumin.js, as currently I use cumin for all my functional requirements.

  1. install cumin.js and belfry.js, The easiest way to do this is using bower bower install cumin belfry.
  2. Include using script tags.
<script type="text/javascript" src="path/to/cumin.js"></script>
<script type="text/javascript" src="path/to/belfry.js"></script>

Usage

Belfry provides a singlton instance from the method getTower.

var tower = Belfry.getTower();

Instance methods are publish, subscribe and unsubscribe.

// create a function to subscribe to topics
var logger = function( data, topic ){
    console.log( topic, data );
};

// subscribe to topic 'channel1'
// Keep returned id to unsubscribe
var subscriberId = tower.subscribe('channel1')(logger);

// publish to 'channel1'
// returns true if any listerns otherwise returns false
tower.publish('channel1')({message: 'Hello, World!'});

// Unsubscribe from topic
// requires both channel and subscriberId
tower.unsubscribe('channel1')(subscriberId);

Tips

Use curried function to create custom listeners

var ListenToResizeEvents = tower.subscribe('resize-events');

// Later
ListenToResizeEvents(logger);

Unsubscribe all listeners from a topic by not passing a subscriberId

ListenToResizeEvents(logger);
ListenToResizeEvents(otherFunc);

var clearResizeListeners = tower.unsubscribe('resize-events');
clearResizeListeners();

Debug Code

Useful code snippet to listen to all messages passing through a channel using cumin.

var logChannel = _.compose(_.invoke(_.log), tower.subscribe);

logChannel('thisChannel');

_.each(logChannel)('listen', 'to', 'all', 'these', 'channels');

belfry.js's People

Contributors

crowdhailer avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.