0.0.10 • Published 6 years ago

@unic/composite-observer v0.0.10

Weekly downloads
44
License
Apache-2.0
Repository
github
Last release
6 years ago

Composite - Observer

Small and simple observer pattern as a composite for your factories

Installation

$ npm install @unic/composite-observer

Importing

// ES6 Module
import observer from '@unic/composite-observer';

// CommomJS
const observer = require('@unic/composite-observer').default;

Usage

A composite is a function or an object which can be used as is or to merged with another object. These composites are normally used in the factory/composition pattern.

Helpful Ressources:

Important: In further examples and the API will just infer that you've already generated your new object with the composite applied on it and will not give any more examples on how to do that.

Examples

// Applying the composite to a new object literal
const obj = Object.assign({}, observer());

// Equivalent with lodash.merge
const obj = _.merge({}, observer());

// Just use it as a
const obj = observer();

API

on(event, callback, once = false)

Subscribe to an event

Returns: Integer - Returns an identifyer to unsubscribe

ParamTypeDefaultDescription
eventStringEventname to subscribe to
callbackfunctionCallback function to execute when the event is triggered
oncebooleanfalseWhen true, will unsubscribe automatically after first execution

Example

// Subscribe to the 'eventName' event
obj.on('eventName', () => {
  console.log('eventName was called');
});

// Subscribe to the 'eventName' event but unsubscribe automatically after first call
obj.on('eventName', () => {
  console.log('eventName was called');
  console.log('This handler unsubscribes automatically');
}, true);

off(identifier)

Unsubscribe a single handler by the identifier returned by .on() or unsubscribe a whole event group by providing an eventname you want to unsubscribe all listeners from

Returns: String/Number - For now... lets jsut say the return doesn't matter

ParamTypeDescription
identifierString/NumberEither the eventname or the identifiers returned by .on()

Example

// Subscribe to the 'eventName' event
const uid = obj.on('eventName', () => {
  console.log('eventName was called');
});

// Unsubscribe by uid
obj.off(uid);

// Unsubscribe by eventname, this unsubscribes all listeners for this event
obj.off('eventName');

trigger(event, params...)

Trigger all listeners by eventname

Returns: undefined

ParamTypeDescription
eventStringEventname to trigger
params...AnyPass any number of arguments you want to receive in the listener

Example

// Subscribe to the 'eventName' event
obj.on('eventName', () => {
  console.log('eventName was called');
});

// Trigger the event 'eventName'
obj.trigger('eventName');

// Subscribe and output all the params you get in the callback
obj.on('eventName', (param1, param2, ...rest) => {
  console.log(param1, param2, rest);
});

// Trigger the event 'eventName' and add custom parameters for this trigger
obj.trigger('eventName', 'Hello', 'World', '!!!');

MIT © Christian Sany

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago