co-mediator
co mediator
Last updated 2 years ago by paio .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install co-mediator 
SYNC missed versions from official npm registry.

co-mediator NPM version Build Status Dependency Status Coverage percentage

A mediator with co wrapped subscriber.

Install

$ npm install --save co-mediator

Usage

let CoMediator = require('co-mediator');
let cm = new CoMediator();
let testData1 = 'a string 1';
let testData2 = 'a string 2';

//a normal subscriber.
let subscriberSymbol1 = cm.subscribe('test', function (publishedData1, publishedData2) {
  console.log(publishedData1 + ' and ' + publishedData2);
});

//a generator subscriber. it will be co wrapped.
let subscriberSymbol2 = cm.subscribe('test', function* (publishedData1, publishedData2) {
  console.log(publishedData1 + ' and ' + publishedData2);
  yield Promise.resolve(true);
});

//error handler
let subscriberSymbol3 = cm.subscribe('test', function* (publishedData1, publishedData2) {
  throw 'an error from the subscriber';
}, function (e) {
  console.log('thrown error ' + e);
});

//publish. specipying channel & arguments. you can pass 0 or more arguments
cm.publish('test');
cm.publish('test', testData1);
cm.publish('test', testData1, testData2);

//unsubscribe
cm.unsubscribe(subscriberSymbol1);
cm.unsubscribe(subscriberSymbol2);
cm.unsubscribe(subscriberSymbol3);
cm.publish('test', testData1, testData2);

//subscribe once
let subscriberSymbol3 = cm.subscribeOnce('test', function* (publishedData1, publishedData2) {
  console.log('this code will be called one time');
}, function (e) {
  console.log('thrown error ' + e);
});
cm.publish('test', testData1, testData2);

//subscribe procedure
cm.subscribeProcedure('ch', function* (param) {
  console.log('passed param is ' + param);
  return yield Promise.resolve('result');
});

//call procedure
cm.procedure('ch', 'param')
  .then(function (val) {
    console.log('result is ' + val);
  })
  .catch(function (e) {
    console.log('this line prints if called proc throws error');
  });

License

MIT © PAIO co.,ltd.

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 9
Dependencies (1)
  • co ^4.6.0
Dev Dependencies (12)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |