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

@mwx47/teleport

v1.2.4

Published

A library for managing and communicating events in your application through a singleton pattern. This pattern ensures that there is a single instance of the event manager, making it easy to coordinate and handle events across different parts of your codeb

Downloads

44

Readme

Teleport

npm version NPM Downloads NPM License GitHub Workflow Status GitHub 仓库

Teleport is a lightweight and versatile event handling library crafted for TypeScript, drawing inspiration from RxJS. It empowers you to effectively manage and communicate events within your application using a singleton pattern. This approach guarantees a single, centralized instance of the event manager, simplifying event coordination and handling across various sections of your codebase.

Key Features

  • Singleton Design: Implements a singleton pattern, providing a unified and singular instance for streamlined event management.
  • Event Queues: When a message is emited, and there are no subscribers at the moment, the message will be temporarily queued until a subscription is established. Simultaneously, the task queue optimizes the execution of identical tasks.
  • Effortless Event Emission: Easily emits events with associated data and optional callback functions.
  • Event Registration: Simple registration of handlers for specific events, making it easy to respond to diverse scenarios in your application.
  • Maintenance: Offers methods to remove specific event handlers, clear all handlers, and reset the entire event manager.

This solution stands independently, devoid of any dependencies on RxJS or external libraries.

Installation

Install the Teleport library using npm:

npm install @mwx47/teleport

Example

First Example

import { Teleport } from '@mwx47/teleport';

const teleport = new Teleport();

teleport.receive('eventName', (data) => {
    console.log('Event data:', data); // ✅ Event data: hello world! 
});

// send data
teleport.emit('eventName', "hello world!");

Examples with delayed subscription

import { Teleport } from '@mwx47/teleport';

const teleport = new Teleport();
// send data
teleport.emit('eventName', "hello world!");

// Although it is delayed by 1000ms, the data can still be obtained
setTimeout(() => {
    const handler = teleport.receive('eventName', (data) => {
        console.log('Event data:', data); // ✅ Event data: hello world! 
        // remove handler
        handler.clear();
    });
}, 1000);

Examples with multiple subscriptions

import { Teleport } from '@mwx47/teleport';

const teleport = new Teleport();
// send data
teleport.emit('eventName1', "Tokyo!");
teleport.emit('eventName2', "China!");
teleport.emit('eventName3', "London!");

const subscriptions = ['eventName1', 'eventName2', 'eventName3'];
const handler = teleport.receive(subscriptions, (arg1:string, arg2:string, arg3:string) => {
    console.log('Hello', arg1); // ✅ Hello Tokyo!
    console.log('Hello', arg1); // ✅ Hello China!
    console.log('Hello', arg1); // ✅ Hello London!
    // remove handler
    handler.clear();
});

Usage

Importing

import { Teleport } from '@mwx47/teleport';

Getting or Creating the Singleton Instance

const teleport = new Teleport();

Emitting an Event

teleport.emit('eventName', eventData, () => {
    // Optional callback function
    console.log('Event emitted successfully!');
});

Receiving and Handling an Event

teleport.receive('eventName', (data) => {
    // Handle the event data
    console.log('Event data:', data);
});

Receiving and Handling Events

teleport.receive(['eventName1', 'eventName2'], (data1:any, data2:any) => {
    // Handle the event data
    console.log('Events data:', data1, data2);
});
// or
teleport.multiReceive(['eventName1', 'eventName2'], (data1:any, data2:any) => {
    // Handle the event data
    console.log('Events data:', data1, data2);
});

Removing a Specific Event Handler

teleport.removeHandle('eventName');

Removing Specific Multiple Events Handler

teleport.removeHandle(['eventName1', 'eventName2']);
// or
teleport.removeMultiHandle(['eventName1', 'eventName2']);

Removing All Event Handlers

teleport.removeAllHandlers();

Clearing the Event Manager

teleport.clear();

Contribution

Contributions are welcome! Feel free to open issues, submit pull requests, or provide suggestions to improve the Teleport library.

License

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