ecc-messagebus
Eccenca Message Bus for inter-component and in-app communications.
Last updated 4 years ago by eccenca .
GPL-3.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ecc-messagebus 
SYNC missed versions from official npm registry.

Eccenca Message Bus for inter-component and in-app communications

ecc-messagebus exports a normal rxmq.js instance but with a set of additional functions. One of those functions is the createChannels helper. For convenience reasons it also exports Rx so that we can use a fixed Rx version in all components.

Using Rx

import {Rx} from 'ecc-messagebus';

var source = Rx.Observable.just(42);

var subscription = source.subscribe(
  function (x) {
    console.log(`Next: ${x}');
  },
  function (err) {
    console.log(`Error: ${err}`);
  },
  function () {
    console.log('Completed');
  });

// => Next: 42
// => Completed

Using request-response

Request-response pattern can be used like so:

import rxmq from 'ecc-messagebus';
// ...
// get channel
const channel = rxmq.channel('yourChannel');
// subscribe to topic
channel.subject('someTopic').subscribe(({data, replySubject}) => {
    // ...
    // use envelop.reply to send response
    replySubject.onNext({some: 'response'});
    replySubject.onCompleted();
});
// ...
// initiate request and handle response as a promise
channel.request({
    topic: 'someTopic',
    data: {test: 'test'},
    timeout: 2000
})
.subscribe((data) => {
    // work with data here
    // ...
},
(err) => {
    // catch and handle error here
    // ...
});

Creating Channel Definitions

The createChannels functions allows to define channels and subjects. Basically it allows to create a channelDefinition with private and public subjects. The Channel of Private Subjects can be changed. This may sound counter-intuitive, but is actually a nice wrapper around changing channels. This helper is predominantly used within the ecc-mixins to create an event-bus per component instance, so that there are no side effects (two components reacting on the same click handler)


// in a channelDefinition.js

import {createChannels, SubjectType} from 'ecc-messagebus'

const {privateChannel, publicChannel} = createChannels({
    name: 'component',
    subjects: {
        secretMessage: SubjectType.private,
        update: SubjectType.public,
    }
});

export {privateChannel, publicChannel};

// in Component A

import {publicChannel, privateChannel} from './channelDefinition.js';

// Handle public messages
publicChannel.update.getSubject().subscribe(() => {
    console.log('There was an update');
});

// Handle Secret Message from Component B
publicChannel.secretMessage.getSubject('secret-key-b').subscribe(() => {
    console.log('Uuuuuh, a secret message from Component B');
});

// Handle Secret Message from Component C
publicChannel.secretMessage.getSubject('secret-key-c').subscribe(() => {
    console.log('Uuuuuh, a secret message from Component C');
});

// in Component B

import {privateChannel} from './channelDefinition.js';

// Send to Component A on Secret Channel
publicChannel.secretMessage.getSubject('secret-key-b').onNext('pssst, component A');

// in Component C

import {privateChannel} from './channelDefinition.js';

// Send to Component A on Secret Channel
publicChannel.secretMessage.getSubject('secret-key-c').onNext('pssst, component A');

Current Tags

  • 3.6.0                                ...           latest (4 years ago)

4 Versions

  • 3.6.0                                ...           4 years ago
  • 4.0.0                                ...           4 years ago
  • 3.5.0                                ...           4 years ago
  • 3.4.0                                ...           4 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |