Skip to content

Judahh/journaly

Repository files navigation

Journaly

Publish npm version npm downloads

A simple message-broker/Pub-sub library

const function1 = async (object0, string0): Promise<string> => {
  await timeout(100);
  return new Promise((resolve) => resolve({string0: string0 + ' executed!', object0}));
};

const journaly = Journaly.newJournaly<string>({
      multiple: true, // setting to use pub-sub pattern
      hasTopic: true,
    }) as PublisherSubscriber<string>;

const subscribe1 = journaly.subscribe(function1, 'test');// Connects function1 to subject test

const subscribes = await Promise.all([subscribe1]);

// Publishes to subject test args: { someObject: 'something' }, 'test 0'
const publish1 = await journaly.publish('test', { someObject: 'something' }, 'test 0');
// Prints all responses to publish 1 from functions which subscribe to subject test
// Each response is an element of the returned array
console.log(publish1);
const function1 = async (object0, string0): Promise<string> => {
  await timeout(100);
  return new Promise((resolve) => resolve({string0: string0 + ' executed!', object0}));
};

const journaly = Journaly.newJournaly<string>({
      multiple: true, // setting to use observer pattern
    }) as ObserverSubject<string>;

const subscribe1 = journaly.subscribe(function1, 'test');// Connects function1 to subject test

const subscribes = await subscribe1;

// Publishes to subject test args: { someObject: 'something' }, 'test 0'
const publish1 = await journaly.publish('test', { someObject: 'something' }, 'test 0');
// Prints the response to publish 1 from function 1 which subscribes to subject test
console.log(publish1);

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js.

If this is a brand new project, make sure to create a package.json first with the npm init command or yarn init command.

Installation is done using the npm install command or yarn add command:

$ npm install journaly

or

$ yarn add journaly

Features

  • Ready to use Pub-sub design pattern
  • Promises oriented
  • Simple implementation

Object Example

class ObjectClass {
  public async method1(object0, string0): Promise<string> {
    await timeout(100);
    return new Promise((resolve) => resolve({string0: string0 + ' executed!', object0}));
  }
}

const object = new ObjectClass();

const journaly = Journaly.newJournaly<string>({
      multiple: true,// setting to use pub-sub pattern
      hasTopic: true,
    }) as PublisherSubscriber<string>;

const subscribe1 = journaly.subscribe(object.method1.bind(object), 'test');// Connects method1 to subject test

const subscribes = await Promise.all([subscribe1]);

// Publishes to subject test args: { someObject: 'something' }, 'test 0'
const publish1 = await journaly.publish('test', { someObject: 'something' }, 'test 0');
// Prints all responses to publish 1 from functions which subscribe to subject test
console.log(publish1);

Settings

const journaly = Journaly.newJournaly<string>({
      multiple: true,// setting to use pub-sub pattern
      hasTopic: true,
    }) as PublisherSubscriber<string>;

const journaly = Journaly.newJournaly<string>({
      multiple: true,// setting to use observer pattern
    }) as ObserverSubject<string>;

const journaly = Journaly.newJournaly<string>({// setting to use sender-receiver pattern
    }) as SenderReceiver<string>;

const journaly = Journaly.newJournaly<string>({
      multiple: true,// setting to use pub-sub pattern
      hasTopic: true,
      hasMemory: true,// setting to store every event,
      // to send all received events to new subscribers
    }) as PublisherSubscriberWithMemory<string>;

const journaly = Journaly.newJournaly<string>({
      multiple: true,// setting to use observer pattern
      hasMemory: true,// setting to store every event,
      // to send all received events to new subscribers
    }) as ObserverSubjectWithMemory<string>;

const journaly = Journaly.newJournaly<string>({// setting to use sender-receiver pattern
      hasMemory: true,// setting to store every event,
      // to send all received events to new subscribers
    }) as SenderReceiverWithMemory<string>;

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test

or

$ yarn
$ yarn test

Methods

// Returns an array with current topics
getTopics(): string[];

// Subscribe a function to a topic (if applicable) and returns an array
// with a result corresponding with all the data received before
subscribe(
    subscriber: SubjectPromise<Result>,
    topic?: string
  ): Promise<Result[]>;

// Unsubscribe a function of a topic (if applicable) and returns an boolean
// corresponding if it's succedd
unsubscribe(subscriber: SubjectPromise<Result>, topic?: string): boolean;

// Publish to a topic (if applicable) and returns all the results of
// subscribbed functions
publish(topic?: string, ...params: any[]): Promise<Result[] | Result>;

People

The original author of Journaly is Judah Lima

List of all contributors