You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@erboladaiorg/harum-ipsa

Package Overview
Dependencies
30
Maintainers
0
Versions
43
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@erboladaiorg/harum-ipsa

[![Subscriber](https://github.com/erboladaiorg/harum-ipsa/actions/workflows/github-workflows.yml/badge.svg)](https://github.com/erboladaiorg/harum-ipsa/actions/workflows/github-workflows.yml)


Version published
Maintainers
0
Created

Readme

Source

SUBSCRIBER

Subscriber

size: 1.3Kb (gzipped)

This tiny library let's you coordinate and dispatch events using channels, namespaces and events.

You create the subscriber once. And add subscriptions to it. You dispatch from the subscriber passing first channel name, event name and an optional data:

 const 
    subscriber = new Subscriber(),
    subscription = subscriber.subscribe("my-channel");
subscription.on("my-event@my-namespace", () => console.log("Hello World!"));
subscriber.dispatch("my-channel", "my-event"); // Hello World!

You can create multiple channels:

const subscription$1 = subscriber.subscribe("my-channel");
const subscription$2 = subscriber.subscribe("my-channel");
const subscription$3 = subscriber.subscribe("my-other-channel");

Add events to subscriptions using event name, only namespace or both. There must be @ between event and namespace. Event comes first:

subscription$1.on("my-event", () => void(0))
subscription$1.on("@my-namespace", () => void(0))
subscription$1.on("my-event@my-namespace", () => void(0))

Pass optional data if you want:

const 
    subscriber = new Subscriber(),
    subscription$1 = subscriber.subscribe("my-channel"),
    subscription$2 = subscriber.subscribe("my-channel");
subscription$1.on("my-event@my-namespace", (data) => data.firstname);
subscription$1.on("my-other-event@my-namespace", (data) => data.lastname);
subscriber.dispatchReturn("my-channel", "@my-namespace", {firstname: "john", lastname: "doe"})
/*
    [
        {channel: "my-channel", subscription: subscription$1, namespace: "my-namespace", event: "my-event", value: "john"},
        {channel: "my-channel", subscription: subscription$2, namespace: "my-namespace", event: "my-other-event", value: "doe"}
    ]
*/

When creating subscriptions, you can pass an optional garbage collection function argument (by default it is () => false, so never removed), which if returns truthy, than that subscriptions will be destroyed and will no longer respond to dispatches.

const subscription$1 = subscriber.subscribe("my-channel", () => !someElement.parentNode) //if someElement is detached from the documentElement, this subscriber wil be removed and won't respond to dispatch calls. Eventually it should be garbage collected.

Methods

MethodExampleExplanation
subscriber.clear()Clears all subscriptions in the channel's Set
subscriber.off(channelstr)subscriber.off("my-channel")Removes the channel, recreated automatically if needed later
subscriber.removeChannel(channelstr)subscriber.off("my-channel")Alias for subscriber.off
subscriber.dispatch(channelstr, eventstr [,data])subscriber.dispatch("my-channel", "my-event", {hello: "world"})Dispatches events in the channel
subscriber.dispatchReturn(channelstr, eventstr [,data])subscriber.dispatchReturn("my-channel", "my-event", {hello: "world"})Dispatches events but instead of returning the subscriber, it returns the callback results as an array with the signature {channel, subscription, namespace, event, value}
subscriber.subscribe(channelstr[,gc])subscriber.subscribe("my-channel")Creates a subscription to a channel
subscription.destroy()subscription.destroy()Removes the subscription from internal channel Set. Dispatches no longer trigger this subscription.
subscription.on(eventstr, callback)subscription.on("my-event@my-namespace", () => "hi")Adds a listener to the subscription
subscription.addEventListener(eventstr, callback)subscription.addEventListener("my-event@my-namespace", () => "hi")Alias for subscription.on
subscription.off(eventstr[,callback])subscription.off("@my-namespace")Removes an eventlistener. The eventstr can be my-event or @my-namespace or my-event@my-namespace. If eventstr is a function, then it will be removed from all namespaces and events including that function for that subscription
subscription.removeEventListener(eventstr[,callback])subscription.removeEventListener("@my-namespace")Alias for subscription.off
subscription.ondispatch = callbacksubscription.ondispatch = () => console.log("dispatched is called from subscriber!")A callback that is triggered whenever the subscriper dispatches this subscription. Only called if ondispatch is set to a function

Keywords

FAQs

Package last updated on 20 Jul 2024

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc