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

@notifierjs/core

v1.1.5

Published

Zero dependencies platform and framework agnostic engine that helps you build your own notifications system.

Downloads

5

Readme

@notifierjs/core

Zero dependencies platform and framework agnostic engine that helps you build your own notifications system.

Table of contents

Installation

Using npm:

npm install @notifierjs/core

Using yarn:

yarn add @notifierjs/core

Usage

Creation

Library exports a single createNotifier factory function which return to you instance of Notifier interface.

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier();

Optionally you can pass some options to it(you can view list of all available options here):

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier({ autoRemoveTimeout: 7000 });

Subscription

Then you should subscribe to the two main notifier events: add and remove

const addListener = () => {
  // some `add` logic here
};

const removeListener = () => {
  // some `remove` logic here
};

notifier.subscribe('add', addListener);
notifier.subscribe('remove', removeListener);

This listeners will called whenever add or remove event happened.

If you want to cancel subscription subscribe method return disposer function.

const addListener = () => {
  // some `add` logic here
};

const dispose = notifier.subscribe('add', addListener);

// when you don't need `add` event listener anymore

dispose();

Add notification

To add notification you simply call add method and pass notification config:

notifier.add({ id: nanoid(), payload: { titile: 'Notification', body: 'Hello world!' } });

This will trigger add event and all its event listeners will be executed.

When you add notification it will appear on the notifications property with the folowing properties:

  • id - id that you passed to notification in add method
  • payload - payload that you passed to notification in add method
  • options - options that are specific to this particular notification
  • info - additional data about your notification, contains a countdown timer for removing your notification if the following options were passed to the factory function or the notification option in the add method: autoRemove: true and autoRemoveTimeout with any number.

Remove notification

Usually, notifications have the autoRemove option set to true as well as autoRemoveTimeout, but if you wanna manually remove some notification use remove method and pass notification id to it:

const notification = { id: nanoid(), payload: { titile: 'Notification', body: 'Hello world!' } };

notifier.add(notification);

// if you wanna to manually remove this notification

notifier.remove(notification.id);

Remove notification(automatically or manually) will trigger remove event.

Queue

If the number of added notifications exceeds the number that was specified in the option size when creating the instance, then they will be added to the queue. You can manage size of the displayed notifications by passed size option to factory function or setOptions method.

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier({ size: 3 });

notifier.add(notification1);
notifier.notifications.length; // 1

notifier.add(notification2);
notifier.notifications.length; // 2

notifier.add(notification3);
notifier.notifications.length; // 3

notifier.add(notification4);
// `notification4` will be added to queue
notifier.notifications.length; // 3

Or by calling setOptions method on the Notifier instance

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier();

notifier.setOptions({ size: 3 });

notifier.add(notification1);
notifier.notifications.length; // 1

notifier.add(notification2);
notifier.notifications.length; // 2

notifier.add(notification3);
notifier.notifications.length; // 3

notifier.add(notification4);
// `notification4` will be added to queue
notifier.notifications.length; // 3

Options

createNotifier factory function can take the following options:

autoRemove

Determines whether the added notification should be removed automatically.

| Type | Required | Default | | --------- | -------- | ------- | | boolean | no | true |

autoRemoveTimeout

Determines after what time(in milliseconds) the notification should be removed automatically.

| Type | Required | Default | | -------- | -------- | ------- | | number | no | 5000 |

size

Determines how many notifications can be displayed.

| Type | Required | Default | | -------- | -------- | ------- | | number | no | 5 |

API

notifications

type: LaunchedNotification<Payload>[]

Contains displayed notifications.

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier<string>();

notifier.add({ id: 1, payload: 'Notification 1' });
notifier.add({ id: 2, payload: 'Notification 2' });

console.log(notifier.notifications); // [LaunchedNotification<string>, LaunchedNotification<string>]

options

type: Options

Contains setted options. Read more in options section.

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier<string>();

console.log(notifier.options); // { autoRemove: true, autoRemoveTimeout: 5000, size: 5 }
import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier<string>({ autoRemoveTimeout: 7000 });

console.log(notifier.options); // { autoRemove: true, autoRemoveTimeout: 7000, size: 5 }

setOptions

type: (options: Partial<Options>) => void

Set options for the current instance.

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier();

notifier.setOptions({ size: 3 });

notifier.add(notification1);
notifier.notifications.length; // 1

notifier.add(notification2);
notifier.notifications.length; // 2

notifier.add(notification3);
notifier.notifications.length; // 3

notifier.add(notification4);
// `notification4` will be added to queue
notifier.notifications.length; // 3

add

type: (notification: PreparedNotification<Payload>) => void

Add notification.

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier<string>();

notifier.notifications.length; // 0

notifier.add({ id: 1, payload: 'Notification 1' });

notifier.notifications.length; // 1

remove

type: (id: string | number) => void

Remove notification.

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier<string>();

notifier.notifications.length; // 0

notifier.add({ id: 1, payload: 'Notification 1' });

notifier.notifications.length; // 1

notifier.remove(1);

notifier.notifications.length; // 0

subscribe

type: (event: NotificationEvent, handler: Handler) => Disposer

Subscribe to notifier events. Return disposer function to unsubscribe from event.

import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier<string>();

notifier.subscribe('add', () => {
  console.log('add event triggered!');
});

notifier.subscribe('remove', () => {
  console.log('remove event triggered!');
});

notifier.add({ id: 1, payload: 'Notification 1' });

// 'add event triggered!'

notifier.remove(1);

// 'remove event triggered!'
import { createNotifier } from '@notifierjs/core';

const notifier = createNotifier<string>();

const unsubscribe = notifier.subscribe('add', () => {
  console.log('add event triggered!');
});

notifier.add({ id: 1, payload: 'Notification 1' });

// 'add event triggered!'

unsubscribe();

notifier.add({ id: 2, payload: 'Notification 2' });

// nothing will be displayed in console

Maintaining

Scripts

List of available scripts to run:

  • build — build library
  • typecheck — run typescript to check types
  • test — run all tests
  • test:watch — run all tests in watch mode
  • test:coverage — run all tests with code coverage output