oja-pubsub

This module is a subset of eBay Oja framework.
The module defines generic actions that can be used to organize actions into publishers and subscribers that are completely disconnected from each others.
Comparing to Oja v1.0 this one is pub/sub on steroids - it defines pub/sub pattern using dependency injection.
Installation
$ npm install @ebay/oja-pubsub --save
Actions
- action('oja/subscribe', 'topic', listener) - subscribe to the event.
- action('oja/unsubscribe', 'topic', listener): Promise - unsubscribe from the event, returns true if subscriber was found and removed.
- action('oja/dispatch', 'topic', ...args): Promise<[subscriberResponse]> - dispatch event/args to the subscribers.
- action('oja/route', 'topic', ...args): Promise - dispatch event/args to only one subscriber in round robin mode.
Listener
function (eventType, ...eventData) {}
Usage
- Subscribing to the event:
const { createContext } = require('@ebay/oja-action');
const context = await createContext();
const listener = await context.action('oja/subscribe', 'topic', (eventType, eventData) => {
console.log(eventData);
});
await context.action('oja/dispatch', 'topic', eventData);
- Routing an event to only one subscriber:
const { createContext } = require('@ebay/oja-action');
const context = await createContext();
const listener1 = await context.action('oja/subscribe', 'topic', (eventType, eventData) => {
console.log('sub1', eventData);
return 'ok1';
});
const listener2 = await context.action('oja/subscribe', 'topic', (eventType, eventData) => {
console.log('sub2', eventData);
return 'ok2'
});
await context.action('oja/route', 'topic', 'one');
await context.action('oja/route', 'topic', 'two');
await context.action('oja/route', 'topic', 'three');
await context.action('oja/route', 'topic', 'four');
- Unsubscribing from the event:
await context.action('oja/unsubscribe', 'topic', listener);
Subscribing as part of other action
context => {
const listener = context.action(
'oja/subscribe', 'topic', (eventType, eventData) => {
console.log(eventData);
}
);
}
To trigger subscription, one needs to trigger the respective action.
Oja reset event
In some cases it may be required to reset any cache related to oja.
@ebay/oja-action makes an optional action call 'oja/extension/context/reset' that if extended can be used to reset respective caches. The only problem is it allows a single extenstion while we may need to reset more then one module.
That's where pub/sub pattern comes handy - the module extends reset action and dispatches the event that can be listened by many subscribers.
await context.action('oja/subscribe', 'oja:reset:event', (eventType, eventData) => {
console.log('sub1:', eventType);
console.log(eventData);
});
await context.action('oja/subscribe', 'oja:reset:event', (eventType, eventData) => {
console.log('sub2:', eventType);
console.log(eventData);
});
await context.action('oja/reset');