A Noticeboard is just the Pub-Sub pattern with a twist: most-recently published values are cached and accessible by future subscribers.
npm install cjs-noticeboard
var Noticeboard = require('cjs-noticeboard'),
board = new Noticeboard();
var notice = 'new-user',
watcher = 'greet-new-users';
board.watch( notice, watcher, function( message ){
console.log( "Hello " + message.notice.username + "!" );
});
board.notify( 'new-user', { username: 'akamaozu' });
board.ignore( 'new-user', 'greet-new-users' )
board.once( 'new-user', 'identify-first-user-only', function( message ){
console.log( "First User: " + message.notice.username );
});
board.watch( 'window-size', 'log-window-size', function( message ){
console.log( "w: " + message.notice.width " | h: " + message.notice.height );
}, { useCache: true });
var Noticeboard = require('cjs-noticeboard'),
board = new Noticeboard({ logging: true });
// or
board.settings.logging = true;
Note: Noticeboard must have settings logging
set to true
var Noticeboard = require('cjs-noticeboard'),
board = new Noticeboard({ logOps: true });
// or
board.settings.logOps = true;
board.watch( 'log-entry', 'process-board-logs', function( message ){
var entry = message.notice;
console.log.apply( console, entry );
});
Create a new Noticeboard Instance. Behavior can be configured with settings.
- Is a Function
- Arguments
- settings
- type: Object
{}
- required: false
- props:
- logging
- type: Boolean (
true
orfalse
) - required: false
- desc: Determines if the Noticeboard will notify subscribers of
log-entry
.
- type: Boolean (
- logOps
- type: Boolean (
true
orfalse
) - required: false
- desc: Determines if the Noticeboard will publish notices about its internal operations to
log-entry
watchers.
- type: Boolean (
- logging
- type: Object
- settings
Triggers the callback in every watcher of the specified notice. If there is a non-null message, the notification will be cached. The source is for attribution / debugging purposes.
- Is a Function
- Arguments
- notice
- type: String
- required: true
- message
- type: Any
- required: false
- gotchas:
- notifications with a message will automatically be cached.
- notifications without a message will not be cached.
- notifications with the message object
null
will not be cached.
- source
- type: String
- required: false
- notice
Adds a watcher to the list of callbacks to execute when a notice is sent out. The execution context and parameters of the callback can be modified via options.
- Is a Function
- Arguments
- notice
- type: String
- required: true
- watcher
- type: String
- required: true
- callback
- type: Function
- required: true
- arguments passed: Object
{}
- arguments props:
- notice
- description: message passed from
Noticeboard.notify
- description: message passed from
- watcher
- description: message passed from
Noticeboard.watch
- description: message passed from
- notice
- options
-
type: Object
{}
-
required: false
-
props:
-
message
- type: Any
- required: false
- description: Passed to callback on execution. accessible inside callback as arguments[0].watcher
-
useCache
- type: Boolean (
true
orfalse
) - required: false
- description: Set to true if its okay to autofire the callback if the notification has been previously cached
- type: Boolean (
-
once
- type: Boolean (
true
orfalse
) - required: false
- description: Set to true if you want this watcher to autoignore the notice immediately after its callback.
- type: Boolean (
-
- notice
Remove a watcher from the list of callbacks to execute when this notice is sent out.
- Is a Function
- Arguments
- notice
- type: String
- required: true
- watcher
- type: String
- required: true
- notice
A simple wrapper around Noticeboard.watch
that calls Noticeboard.ignore
as soon as its callback is executed. See Noticeboard.watch
for details this function's parameters.
Designed to simulate the browser's console.log
, this function will notify all watchers of log-entry
and pass its arguments object as the message.
For instance, this is equivalent to calling console.log
, with the added benefit of not crashing if the browser doesn't have console. That and your log can be piped to other parts of your app.
Noticeboard.watch('log-entry', 'browser-console', function(msg){
if(!console || typeof console.log !== "function"){ return; }
console.log.apply(console, msg.notice);
}
With the above watcher, the snippet below is now a superior version of console.log
.
Noticeboard.log("testing", {isTest: true}, [1,2,3], function(){})
- Pull Requests
- Email: uzo@designbymobius.ca
- Twitter: @akamaozu
- Carrier Pigeons
Hop on the Noticeboard Train!
You're waiting for a train
A train that will take you far away
You know where you hope this train will take you
But you don't know for sure
Yet it doesn't matter
Because we'll be together
-- Mal (Inception, 2010)