npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@unic/composite-observer

v0.0.10

Published

Simple observer composite to use for your factories

Downloads

18

Readme

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

| Param | Type | Default | Description | | --- | --- | --- | --- | | event | String | | Eventname to subscribe to | | callback | function | | Callback function to execute when the event is triggered | | [once] | boolean | false | When 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

| Param | Type | Description | | --- | --- | --- | | identifier | String/Number | Either 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

| Param | Type | Description | | --- | --- | --- | | event | String | Eventname to trigger | | [params...] | Any | Pass 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