Class: Dendrite

Defined in: src/dendrite.coffee

Overview

dendrite - An extended Observer pattern implementation, worked at any JavaScript environment.

GitHub repository dendrite

Thanks to Joe Zim for original Publish/Subscribe plugin for jQuery

Author:

Copyright:

Version:

Constant Summary

DEBUG =

Verbose levels constants

3
WARNING =
2
ERROR =
1
SILENT =
0

Instance Method Summary

Constructor Details

- (void) constructor()
- (void) constructor(options)

Construct a new Dendrite.

Examples:

dendrite_obj = new Dendrite verbose : 'warning'

Overloads:

- (void) constructor()

Construct new Dendrite with default options

- (void) constructor(options)

Constrict new Dendrite with settings

Parameters:

  • options (Object)

Options Hash: (options):

  • verbose (String) verbose level, may be [ 'debug' | 'warning' | 'error' | 'silent' ]

Instance Method Details

- (Object) subscribe(topics, callback)
- (Object) subscribe(topics, callback, context)

Note: The 'callback' function receives 'topic' [String] as first argument and 'data' [Any] as any data that the publisher sent

Subscribe to topic(s).

Examples:

handler = dendrite_obj.subscribe 'foo', (topic, data...) -> console.log data, topic

Returns:

  • (Object) handler { topics: topics, callback: callback, watchdog: undefined, context: context } or throw exception on invalid arguments

Overloads:

- (Object) subscribe(topics, callback)

Subscribe to topic(s) without context

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to subscribe to
  • callback (Function) function to be called when the given topic(s) is published to

Returns:

  • (Object)

- (Object) subscribe(topics, callback, context)

Subscribe to topic(s) with context

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to subscribe to
  • callback (Function) function to be called when the given topic(s) is published to
  • context (Object) an object to call the function on

Returns:

  • (Object)

- (Object) subscribeGuarded(topics, callback, watchdog)
- (Object) subscribeGuarded(topics, callback, watchdog, context)

Note: The 'callback' function receives 'topic' [String] as first argument and 'data' [Any] as any data that the publisher sent
Note: The 'watchdog' function receives two arguments: 'err' [Error] and 'options' [Object] as all 'callback' properties

Subscribe to topic(s) with 'watchdog' function to handle errors here, in subscriber.

Examples:

context_object = 
  name : 'Context Object'
  callback : (topic, data) -> throw Error "Die at #{topic}"
  watchdog : (err, options) -> 
    console.log "Error in | #{@name} |"
    console.log "Error string: | #{err} |"
    console.log "Error detail", options
    null  

handler = dendrite_obj.subscribeGuarded 'foo', context_object.callback, context_object.watchdog, context_object

Returns:

  • (Object) handler { topics: topics, callback: callback, watchdog: watchdog, context: context } or throw exception on invalid arguments

See also:

Overloads:

- (Object) subscribeGuarded(topics, callback, watchdog)

Subscribe with 'watchdog' without context

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to subscribe to
  • callback (Function) function to be called when the given topic(s) is published to
  • watchdog (Function) function to be called when callback under publishing topic rise exception

Returns:

  • (Object)

- (Object) subscribeGuarded(topics, callback, watchdog, context)

Subscribe with 'watchdog' with context

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to subscribe to
  • callback (Function) function to be called when the given topic(s) is published to
  • watchdog (Function) function to be called when callback under publishing topic rise exception
  • context (Object) an object to call the function on

Returns:

  • (Object)

- (Object) unsubscribe(topics)
- (Object) unsubscribe(topics, callback)
- (Object) unsubscribe(topics, callback, context)
- (Object) unsubscribe(handler)

Note: Unsubscriptions may be placed to queue if Dendrite do some publish tasks and restarted to unsubscribe when all publish tasks is done.

Unsubscribe from topic(s) or remove all subscribers from topic(s).

Examples:

dendrite_obj.unsubscribe 'foo bar', callback_reference, obj
dendrite_obj.unsubscribe 'bar baz'

Returns:

  • (Object) *this* for chaining

Overloads:

- (Object) unsubscribe(topics)

Remove all subscriptions from topic(s)

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to unsubscribe from

Returns:

  • (Object)

- (Object) unsubscribe(topics, callback)

Remove subscriptions for callback from topic(s) if no context used in the #subscribe() call

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to unsubscribe from
  • callback (Function) function to be removed from the topics subscription list

Returns:

  • (Object)

- (Object) unsubscribe(topics, callback, context)

Remove subscriptions for callback and given context object from topic(s)

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to unsubscribe from
  • callback (Function) function to be removed from the topics subscription list
  • context (Object) object that was used as the context in the #subscribe() call

Returns:

  • (Object)

- (Object) unsubscribe(handler)

Remove subscriptions with handler object. May be usefully if subscription created with anonymous 'callback'

Parameters:

  • handler (Object) subscription handler, returned by #subscribeX() method

Options Hash: (handler):

  • topics (String) 1 or more topic names, separated by a space, to unsubscribe from
  • callback (Function) function to be removed from the topics subscription list
  • context (Object) object that was used as the context in the #subscribe() call

Returns:

  • (Object)

- (Object) publish(topics)
- (Object) publish(topics, data...)

Synchronously publish any data to topic(s).

Examples:

dendrite_obj.publish 'foo bar', 'This is some data'

Returns:

  • (Object) *this* for chaining

Overloads:

- (Object) publish(topics)

Do publish to topics without any data

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to publish to

Returns:

  • (Object)

- (Object) publish(topics, data...)

Do publish with some data to topics

Parameters:

  • topics (String) 1 or more topic names, separated by a space, to publish to
  • data (Any) any kind of data(s) you wish to give to the subscribers

Returns:

  • (Object)

- (Object) publishSync(topics, data...)

Alias for #publish

Returns:

  • (Object) *this* for chaining

- (Object) publishAsync(topics, data...)

Note: Used exactly as #publish, but this method puts task to queue and will returns immediately

Asynchronously publish any data to topic(s).

See #publish for all info

Examples:

dendrite_obj.publishAsync 'foo bar', 'This is some data'

Returns:

  • (Object) *this* for chaining