0.1.4 • Published 1 year ago

type-pubsub v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

type-pubsub

type-pubsub is TypeScript library implementing the Publish/Subscribe pattern for Node.js or web browser.

Installation

To start using type-pubsub install the npm package:

npm install type-pubsub

Also you need to configure your TypeScript to enable experimental decorators. Add this line to your tsconfig.json file under the compilerOptions key:

"experimentalDecorators": true,

Basic Usage

import { PubSub, Subscribe, Subscriber, Unsubscribe } from 'type-pubsub';

@Subscriber()
class SubscriberExample {
  @Subscribe('TEST_MESSAGE')
  foo(data: string, message: string): void {
    console.log(`Message received: ${message}, Data: ${data}`);
  }

  @Unsubscribe() // Calling the method marked with @Unsibscribe() will unregister all the subscriptions
  dispose(): void {}
}

var subscriber = new SubscriberExample();
PubSub.publish('TEST_MESSAGE', 'This message will be displayed');
subscriber.dispose(); // Unsubscribe
PubSub.publish('TEST_MESSAGE', "This message won't be displayed");

You can also create channels to publish messages manually and specify which one you want to observe:

const myChannel = new Channel<string>();

@Subscriber({ channel: myChannel })
class SubscriberExample {
  @Subscribe('TEST_MESSAGE')
  foo(data: string, message: string): void {
    console.log(`Message received: ${message}, Data: ${data}`);
  }
}

const data = {
  // Some data here
};
myChannel.publish('TEST_MESSAGE', data);

If needed, you can mark a method with multiple @Subscribe() decorators to observe different messages:

@Subscriber()
class SubscriberExample {
  @Subscribe('TEST_MESSAGE')
  @Subscribe('OTHER_MESSAGE')
  foo(data: string, message: string): void {
    console.log(`Message received: ${message}, Data: ${data}`);
  }
}

License

MIT - http://rem.mit-license.org