Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rsed

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rsed

Event Dispatcher

  • 0.1.6
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

RSED - Event Dispatcher

RSED is simple event dispatcher. It dispatches events in sync mode. There's no async/await, all events are non-blocking.

Getting Started

Build your custom application event

export default class CurrentUserSaved {
  public user: User;
  constructor(user: User) {
    this.user = user;
  }
}

Build event listeners

You have to build class that implements IListenerProvider.

import { AppEventHandler, IListenerProvider } from "rsed";

export default class CurrentUserEventListener implements IListenerProvider {
  getListenersForEvent(event: object): AppEventHandler[] {
    if (event instanceof CurrentUserSaved) {
      return [this.setAccess];
    }
    return [];
  }

  setAccess = (event: CurrentUserSaved) => {
    console.log("New user authenticated", event.user);
  };
}

Build event dispatcher

import EventDispatcher, { IEventDispatcher } from "rsed";

const eventDispatcher = new EventDispatcher();
eventDispatcher.addListenerProvider(new CurrentUserEventListener());

Dispatch event in your application

eventDispatcher.dispatch(new CurrentUserSaved(currentUser));

Test async event handlers

// add tester helper
export const flushPromises = () => {
  return new Promise((resolve) => setImmediate(resolve));
};

export class MyAsyncListener {
  public async isSatisfied(newValue: number) {
    // await someAction()
  }
}

// test

eventDispatcher.dispatch(new MyEvent());
await flushPromises();

expect(MyAsyncListener.isSatisfied).toHaveBeenCalled();

Keywords

FAQs

Package last updated on 31 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc