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

@pourianof/notifier

v1.2.2

Published

A simple notifier library to help develop event base services in well performance way. INSPIRED BY FLUTTER STREAM

Downloads

31

Readme

Intro

A simple library to utilize event-base programming in our app.

Installation

npm i @pourianof/nitifier

Changes

V1.1 Make event typing richer (may cause typescript code break if you extending classes)

V1.2.0 Add stopPropagation method to event object, visibility of notifyListener method of Notifier class got change from private to protected to make it possible to change or customize how notify listeners.

Usage

You can use a notifier like:

import Notifier from "notifier";

const notifier = new Notifier();

// Add listener to the notifier
notifier.addListener("meow", function () {
  console.log("someone meowing...");
});

// You can have multiple listeners
const listener = notifier.addListener("meow", function () {
  feedTheCat();
  // Cancel listening to event (No event will sent to this listener)
  listener.cancel();
});

// Trigger an event which cause notify the listeners
notifier.trigger("meow");

Or you can use it as a base class for your own classes to make them notifier.

Passing data

You can also pass data when you triggering events.

// Passing a data along with event
notifier.trigger("meow", { catName: "pinky" });

notifier.addListener("meow", function (data) {
  if (data.catName != "leo") {
    ignoreThatCat(); // its not mine
  }
});

Stop event propogation

You can stop propogation of event to other(later registered) listener via event object.

notifier.addListener("event-name", (eventObject) => {
  eventObject.stopPropogation();
});

// Not receiving event, because the first listener stopped the propagation
notifier.addListener("event-name", (eventObject) => {
  console.log(eventObject); // not running
});

Typescript

If you are using typescript, you can define your events as generic. For example our notifier only works with "meow" and "bark" events:

const notifier = new Notifier<"meow" | "bark">();

notifier.trigger("hi"); // Typescript error which refer to wrong event with what you defined when you construct

You can also define a type as map of key-values which keys are the event names and values are type of the data which that event carrying.

const notifier = new Notifier<{
  meow: {
    catName: string;
    age: number;
  };
  bark: {
    dogName: string;
    owner: string;
  };
}>();

In this example typescript force you to work with only two defined events("bark", "meow") and also whenever you trigger those events you must pass a data with the specified structure.

Sync-Async

This package support two type of event dispatching: 1- Sync: in this mode the event will dispatch at the moment it triggered using trigger method, synchronously. 2- Async: in this mode each event which triggered, scheduled to be dispatch asynchronously at later. After synchronous code has executed. (Default mode)

The default mode is async, if you wanna use synchronous mode, then you need pass boolean "false" value to constructor.

const asyncNotifier = new Notifier();
const syncNotifier = new Notifier(false);

Use sync mode carefully, because it may cause some code execution interference. Like when you trigger new event in your listener callback.