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

notiflyer

v1.1.0

Published

A reactive RxJS-based library for managing notifications with customizable message.

Downloads

129

Readme

notiflyer Library Documentation

notiflyer is a lightweight library built with RxJS for managing notifications in a reactive and type-safe manner.
It supports multiple notification statuses (SUCCESS, FAILURE, IN_PROGRESS, and IDLE) and allows you to easily integrate, handle, and evaluate notifications in your application.
Additionally, it includes support for custom states with a flexible and extensible API.


Features

  • Reactive Design: Built on RxJS for flexible and reactive notifications.
  • Type-Safe Notifications: Strongly typed notification messages for robust development.
  • Custom Notifiers: Define and manage custom notification statuses with ease.
  • Utility Functions: Simplified creation and evaluation of notifications.

Installation

Install the library via npm:

npm install notiflyer

Usage

1. Creating and Using a Notifier

Example: Single Notifier

import { createReactiveNotifier, NotifyFactory } from 'notiflyer';

export interface MessageNotification {
  message: string;
}

const notifier = createReactiveNotifier<MessageNotification>();

notifier
  .observeNotifications({
    success: (msg) => console.log('Success:', msg),
  })
  .subscribe();

notifier.notify(NotifyFactory.success({ message: 'Operation completed successfully!' }));

2. Creating and Managing Multiple Notifiers

Example: Notifier Group

import { createReactiveNotifierGroup, NotifyFactory, NotifyMessage } from 'notiflyer';

interface AppNotifications {
  taskNotifier: NotifyMessage<{ taskId: number }>;
  userNotifier: NotifyMessage<{ user: number }>;
}

const notifiers = createReactiveNotifierGroup<AppNotifications>(['taskNotifier', 'userNotifier']);

notifiers.taskNotifier
  .observeNotifications({
    inProgress: (msg) => console.log('Task in progress:', msg),
  })
  .subscribe();

notifiers.taskNotifier.notify(NotifyFactory.inProgress({ taskId: '123' }));

3. Creating and Using Custom Notifiers

notiflyer supports custom states, allowing you to define custom notification statuses tailored to your application.

Example: Custom Single Notifier

import { createCustomReactiveNotifier } from 'notiflyer';

type CustomStates = 'LOADING' | 'ERROR' | 'COMPLETED';
const customStates: CustomStates[] = ['LOADING', 'ERROR', 'COMPLETED'];

export interface MessageNotification extends Record<string, unknown> {
  message: string;
}

const customNotifier = createCustomReactiveNotifier<MessageNotification, CustomStates>(customStates);

customNotifier
  .observeNotifications({
    LOADING: (msg) => console.log('Loading:', msg.message),
    ERROR: (msg) => console.error('Error:', msg.message),
    COMPLETED: (msg) => console.log('Completed:', msg.message),
  })
  .subscribe();

customNotifier.notify({ status: 'LOADING', message: 'Fetching data...' });
customNotifier.notify({ status: 'COMPLETED', message: 'Data fetched successfully!' });
customNotifier.notify({ status: 'ERROR', message: 'Failed to fetch data.' });

Example: Custom Notifier Group

import { createCustomReactiveNotifierGroup, CustomNotifyMessage } from 'notiflyer';

type CustomStates = 'LOADING' | 'ERROR' | 'COMPLETED';
const customStates: CustomStates[] = ['LOADING', 'ERROR', 'COMPLETED'];

interface CustomAppNotifications {
  taskNotifier: CustomNotifyMessage<{ taskId: number }, CustomStates>;
  userNotifier: CustomNotifyMessage<{ userId: number }, CustomStates>;
}

const customNotifiers = createCustomReactiveNotifierGroup<CustomAppNotifications, CustomStates>(
  ['taskNotifier', 'userNotifier'],
  customStates,
);

customNotifiers.taskNotifier
  .observeNotifications({
    LOADING: (msg) => console.log('Loading Task:', msg),
    ERROR: (msg) => console.error('Error in Task:', msg),
    COMPLETED: (msg) => console.log('Task Completed:', msg),
  })
  .subscribe();

customNotifiers.taskNotifier.notify({ status: 'LOADING', taskId: 1 });
customNotifiers.taskNotifier.notify({ status: 'COMPLETED', taskId: 1 });
customNotifiers.taskNotifier.notify({ status: 'ERROR', taskId: 1 });

4. Utility Functions

notiflyer provides utility functions for creating and evaluating notification messages:

Message Factories

  • NotifyFactory.success<T>(data: T): NotifyMessage<T> – Creates a success notification.
  • NotifyFactory.failure<T>(data: T): NotifyMessage<T> – Creates a failure notification.
  • NotifyFactory.inProgress<T>(data: T): NotifyMessage<T> – Creates an in-progress notification.
  • NotifyFactory.idle<T>(data: T): NotifyMessage<T> – Creates an idle notification.

State Checkers

  • NotifyState.isSuccess<T>(message: NotifyMessage<T>): boolean – Checks if a message is a success notification.
  • NotifyState.isFailure<T>(message: NotifyMessage<T>): boolean – Checks if a message is a failure notification.
  • NotifyState.isInProgress<T>(message: NotifyMessage<T>): boolean – Checks if a message is in progress.
  • NotifyState.isIdle<T>(message: NotifyMessage<T>): boolean – Checks if a message is idle.

Example:

import { NotifyFactory, NotifyState } from 'notiflyer';

const message = NotifyFactory.failure({ error: 'Operation failed!' });

if (NotifyState.isSuccess(message)) {
  console.log('The operation was successful!');
} else {
  console.error('The operation failed:', message);
}

API Reference

ReactiveNotifier

A generic class to manage a single stream of notifications.

  • Methods:
    • notify(message: T): void – Emits a notification to all subscribers.
    • observeNotifications(callback: NotifyCallback<T>): Observable<T> – Sets up callbacks for handling notification statuses.

CustomReactiveNotifier<T, S>

A generic class for managing a single stream of notifications with custom states.

  • Methods:
    • notify(message: CustomNotifyMessage<T, S>): void – Emits a notification to all subscribers.
    • observeNotifications(callback: CustomNotifyCallback<T, S>): Observable<CustomNotifyMessage<T, S>> – Sets up callbacks for handling custom states.

Factory Functions

createReactiveNotifier<T>()

Creates a new reactive notifier for managing a single notification stream.

  • Returns: ReactiveNotifier<T> – A notifier instance.

createReactiveNotifierGroup<T>()

Creates a group of reactive notifiers for managing multiple notification streams.

  • Parameters:

    • keys: (keyof T)[] – List of notifier keys.
  • Returns: { [K in keyof T]: ReactiveNotifier<T[K]> } – A group of notifier instances.


createCustomReactiveNotifier<T, S>()

Creates a new reactive notifier for managing notifications with custom states.

  • Parameters:

    • states: S[] – List of custom states.
  • Returns: CustomReactiveNotifier<T, S> – A notifier instance with custom states.


createCustomReactiveNotifierGroup<T, S>()

Creates a group of reactive notifiers for managing multiple notification streams with custom states.

  • Parameters:

    • keys: (keyof T)[] – List of notifier keys.
    • states: S[] – List of custom states.
  • Returns: { [K in keyof T]: CustomReactiveNotifier<T[K], S> } – A group of notifier instances.


Example Project

This repository contains an example TypeScript project.

Prerequisites

  • Node.js: Make sure you have Node.js installed. You can download it from Node.js official website.
  • npm: Comes with Node.js. Verify installation with:
    node -v
    npm -v

Installation

  1. Clone the repository and navigate to the project directory:

    git clone https://github.com/malsienk/notiflyer.git
    cd notiflyer
  2. Install the required dependencies:

    npm install

Running the Example

To run the example script, use the following command:

./node_modules/.bin/ts-node examples/example.ts

License

This project is licensed under the MIT License. See the LICENSE file for details.