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. 👍
npm install channy --save
Or with Bower (use dist/channy.pkg.js
in browserland),
bower install channy --save
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 up an empty channel. open
takes a channel name and returns void
.
chan.open "a:channel"
Close a channel. close
takes a channel name and returns void
.
chan.close "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. 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
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"
MIT © Ezekiel Gabrielse