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

@fedeghe/channeljs

v1.2.10

Published

multi channel retroactivatable observer utility

Downloads

35

Readme

Coverage Status

Channeljs

Channeljs aims to offer a isomorphic easy-to-use, small and flexible Observer pattern implementation.

install it

> yarn add @fedeghe/channeljs

run the tests / coverage

> yarn test
> yarn cover 

Create a Channel, or get it if already exists.


import Channel from '@fedeghe/channeljs'
// import Channel from '@fedeghe/channeljs';

// The Channel.get function lets You create (or retrieve if elsewhere created)
// an instance of a channel.

// Somewhere, subscribe to the "mult"
// topic of the "math" channel
Channel.get('math').sub('mult',
    (...args) => args.reduce((el, acc) => acc * el, 1)
);

then elsewhere

import Channel from '@fedeghe/channeljs'
// ...
const mult = Channel.get('math').pub('mult', [2, 3, 5]);
console.log(mult) // 30

so the flow seems to be

  1. subscribe to a topic passing as second parameter a function that will receive all parameters passed when publishing, the function could as side effect consume the published data or/and return a result.
  2. get that result calling publish to a topic passing all parameters needed for the subscriber to be able to calculate and return the result.

...there is something more:

  1. since there could be more subscribers to the same topic, in that case publish will return an array of results, each one corresponding to the result returned from the relative subscriber (subscription order). There is not realy way to be sure about the result/subscriber correspondance thus it is not a bad idea to include this information in the returned result (as a referrer information).

  2. it's possible to do the opposite, allowing for example a `late` subscriber to be informed at subscription time of a past publishing on a topic.

  3. couple of more functions: once (sub), enable, disable, unsub (one, more, or all topics) and reset


Raw Api

Once the script has been imported in the browser, or required in Your script (I assume required as Channeljs), there are two static functions available:

Channeljs.getAllChannels

const myChannels  = Channeljs.getAllChannels({enabledStatus <boolean>});

Returns an object literal containing all existing Channels, optionally can be passed a boolean value which enables a filter, when true it will return only all enabled Channels, when false only disabled ones.

Channeljs.get([name <string>])

const uxEvents = Channeljs.get('ux');

Creates, if not already existing, an instance of Channel indexed as ux and returns it. Now we can use it.


@instance methods

pub(topic <string>, parameters <array> | single parameter)

uxEvents.pub('cartUpdated', articles)

Publishes parameters on one or more topics. If an array is passed then the subscribing function will receive the content of the array as parameters:

ch.sub('topicx', (a, b, c) => { ... })
/* ----> */ 
ch.pub('topicx', [1, 2 ,3])

if instead the second agrument used to invoke the pub is not an array then the subscriber will receive only that as first arguments, all other will not be passed:

ch.sub('topicx', (the_obj) => { ... })
/* ----> */ 
ch.pub('topicx', { name: 'widgzard' })

sub(topic <string>, subscriber <function> {, retroActive <boolean>})

uxEvents.sub('cartUpdated', function () {
    var articles = [].slice.call(arguments, 0);
})

Registers a listener to a particular topic on the channel; the subscribing function will receive all parameters passed in an array as second parameter at pub call:

ch.pub('bePolite', ['Hello', 'World', '!'])
...
ch.sub('bePolite', function (a, b, c) {
    console.log(`${a} ${b} ${c}`) // Hello World !
}, true) // true here enables retroActivity

The retroActive parameter allows the subcriber to be executed immediately for all relevant past published events.

unsub(topic <string>, subscriber <function>)

Removes the subscriber from the topic

once(topic <string>, subscriber <function> {, retroActive <boolean>}

Exactly as sub but once, retroactivable.

enable()

Enables a channel

disable()

Disables a channel