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

harmony-conductor

v0.6.2

Published

HarmonyConductor: A lightweight and type-safe event bus for seamless event handling and inter-component communication in JavaScript and TypeScript applications.

Downloads

99

Readme

HarmonyConductor

Harmony: Conductor is a lightweight, easy-to-use event bus for managing application-level events. This simple yet powerful tool facilitates communication between different parts of your application without tight coupling.

Features

Installation

Install HarmonyConductor via npm:

npm install harmony-conductor

Or using Yarn:

yarn add harmony-conductor

Usage

Importing

First, import the HarmonyConductor library into your project:

import { Conductor } from 'harmony-conductor';

Event Subscriptions

To subscribe to an event, use the Conductor.Subscribe method. You can provide an event name and a handler function.

Conductor.Subscribe('myEvent', payload => {
    console.log('Event payload:', payload);
});

For events without a payload:

Conductor.Subscribe('simpleEvent', () => {
    console.log('Simple event triggered');
});

Event Publishing

To trigger an event, use Conductor.Publish. You can provide the event name and an optional payload.

Conductor.Publish('myEvent', { key: 'value' });

For events without a payload:

Conductor.Publish('simpleEvent');

Unsubscribing from Events

When you no longer need to listen to an event, you can unsubscribe from it. Use the UnSubscribe method available on the subscription object returned when subscribing:

const subscription = Conductor.Subscribe('myEvent', payload => {
    console.log('Event payload:', payload);
});

// Later, when you want to unsubscribe
subscription.UnSubscribe();

API

Subscribe to an event.

Conductor.Subscribe<TEvent>(eventName: string, handler: (payload: TEvent) => void, thisArg?: object): ISubscription<TEvent>

  • eventName: The name of the event to subscribe to.
  • handler: The function to call when the event is published.
  • thisArg (optional): An object to bind as this when calling the handler.

Publish an event.

Conductor.Publish<TEvent>(eventName: string, payload?: TEvent): void

  • eventName: The name of the event to publish.
  • payload (optional): Data to pass to the event handler.

Manage a subscription.

The ISubscription<TEvent> interface represents a subscription to an event.

  • Subscriber: A function that will be called when the event is published, with an optional payload of type TEvent.
  • EventName: The name of the event to which the subscription is associated.
  • UnSubscribe: A parameterless method to unsubscribe from the event, removing the subscription.

You can use the UnSubscribe method to remove a subscription when it's no longer needed, preventing further callbacks to the Subscriber function.

Advanced Usage

Using Typed Events

You can create a typed event class to encapsulate the data you want to pass with the event. Define a typed event class:

class UserCreatedEvent {
    constructor(userName?: string, email?: string) {
        this.UserName = userName;
        this.Email = email;
    }
    public UserName?: string
    public Email?: string
}

And an Enum to represent event names:

enum AppEvents {
    UserCreated = "UserCreated"
}

Subscribing with a Class Method

Subscribe to an event using a method of a TypeScript class, ensuring proper binding of the this context:

class AppComponent {
    constructor() {
        Conductor.Subscribe(AppEvents.UserCreated, this.onUserCreated, this);
    }
    private onUserCreated(event: UserCreatedEvent): void {
        console.log(`New user created: ${event.UserName}, Email: ${event.Email}`);
    }
}

Publishing Typed Events

Publishing typed events is straightforward. Pass the event name and the event object:

const userCreatedEvent = new UserCreatedEvent('johndoe', '[email protected]');
Conductor.Publish(AppEvents.UserCreated, userCreatedEvent);

This example shows how Conductor can be used with TypeScript classes and typed events, offering a clean and type-safe way to handle events in your application.

Running Tests

To ensure Conductor functions as expected, there is a comprehensive test suite. After installing the necessary dependencies, run these tests using the following command:

npm test

Contributing

Contributions to Harmony: Conductor are welcome! Please contact the author to get started.

License

This project is licensed under the MIT License.