New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@unic/composite-observer

Package Overview
Dependencies
Maintainers
3
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@unic/composite-observer

Simple observer composite to use for your factories

  • 0.0.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by37.5%
Maintainers
3
Weekly downloads
 
Created
Source

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:

  • https://www.youtube.com/watch?v=ImwrezYhw4w
  • https://www.youtube.com/watch?v=wfMtDGfHWpA

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
[once]booleanfalseWhen 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

Keywords

FAQs

Package last updated on 01 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc