Skip to content
/ channy Public

channy is a synchronous channel-like observer that allows easy orchestration of events

License

Notifications You must be signed in to change notification settings

ezekg/channy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

channy

Travis NPM Bower

channy is a Go channel-like observer that allows easy orchestration of arbitrary events. It lets you to get rid of binding events to DOM elements (because those aren't very reusable across an application), and instead, lets channy message the members of a channel, executing all of the callbacks that have joined.

As stated above, this is loosely based on the flow associated with Go's channels. I use this library for most of my projects, so I figured I should stop copying and pasting it and start keeping it nice and versioned. 👍

Install

npm install channy --save

Or with Bower (use dist/channy.pkg.js in browserland),

bower install channy --save

Usage

channy is a static class, so you don't need to instantiate it with new. It is responsible for managing global channels. A channel name should be namespaced (for example, "namespace:channel"). See inline documentation for detailed breakdowns on what each method does, otherwise see below for some basic usage.

chan = require "channy"

chan.join "chan:channy", (x) ->
  console.log "hello, #{x}"

chan.message "chan:channy", "channy"
# console.log => "hello, channy"

Open a channel

Open up an empty channel. open takes a channel name and returns void.

chan.open "a:channel"

Close a channel

Close a channel. close takes a channel name and returns void.

chan.close "a:channel"

Join a channel

Join a channel. This will automatically create the channel if it doesn't already exist. join takes a channel name and callback function and returns the callback.

chan.join "a:channel", (args...) -> # ...

Leave a channel

Leave a channel. This will automatically close the channel if no subscribers currently exist for it. leave takes a channel name and callback function and returns the callback.

callback = -> # ...

chan.join "a:channel", callback

chan.leave "a:channel", callback

Message a channel

Send a message to a channel, executing all callbacks currently subscribed. message takes a channel name and an infinite number of arguments to be passed to the callback function, and returns void.

chan.join "a:channel", (x) -> console.log "hello, #{x}"

chan.message "a:channel", "channy"
# console.log => "hello, channy"

chan.message "a:channel", "manny"
# console.log => "hello, manny"

License

MIT © Ezekiel Gabrielse

About

channy is a synchronous channel-like observer that allows easy orchestration of events

Resources

License

Stars

Watchers

Forks

Packages

No packages published