项目作者: LivePersonInc

项目描述 :
JS Channels (Events / Commands / Reqest-Response / Courier) Mechanism
高级语言: JavaScript
项目地址: git://github.com/LivePersonInc/chronosjs.git
创建时间: 2015-05-21T15:30:44Z
项目社区:https://github.com/LivePersonInc/chronosjs

开源协议:MIT License

下载


" class="reference-link">chronosjs

Built with Grunt
Build Status
Test Coverage
Code Climate
npm version
Dependency Status
devDependency Status
npm downloads
NPM

LivePerson’s Generic JS Channels Mechanism (Events/Commands/ReqRes)

Getting Started

Run npm install chronosjs

Overview

This library provides an ability to develop event driven applications using the included sub-modules of events, commands and request/response.

Together with Courier, one can integrate multiple applications into one, by allowing cross domain cross application event driven communication.

An application developer can integrate/embed a 3rd party application (provided the application uses courier as well) seamlessly and securely without worrying about cross domain issues.

Another use case is for building multi module application where each module can be it’s own application and a developer will want to mix and match between them.

Chronos.Events

An events channel for binding and triggering events.
Allows multiple listeners on a single event and wildcards ("*") support.

Chronos.Command

A command mechanism for complying and commanding and API call.
Allows a single complier per command.
Supports async commands with an options to call a callback when done.

Chronos.ReqRes

A request mechanism for replying and requesting and API call that returns a response.
Allows a single replier per request.
Supports async requests with an options to call a callback when done with a result.

Chronos.Channels

A Channel which includes all communication means (events, commands, reqres).
Implements the same API’s as all means it contains

Chronos.PostMessageCourier

A generic implementation of Channels over postMessage API.
Allows communication between cross domain IFRAMES “sharing” a Channels instance.

Package Contents

The package holds a few artifacts in the dist folder:

Minified compressed versions exist in the min* folder.

Usage examples

Events

  1. var events = new Chronos.Events();
  2. //Listen on the event only once
  3. events.once({
  4. appName: "Your App Name",
  5. eventName: "Your Event Name",
  6. func: _yourCallBackFunction
  7. });
  8. //Regular bind on the event
  9. events.bind({
  10. appName: "Your App Name",
  11. eventName: "Your Event Name",
  12. func: _yourCallBackFunction
  13. });
  14. //Unbind from the event
  15. events.unbind({
  16. appName: "Your App Name",
  17. eventName: "Your Event Name",
  18. func: _yourCallBackFunction
  19. });
  20. //Trigger the event
  21. events.trigger({
  22. appName: "Your App Name",
  23. eventName: "Your Event Name",
  24. data: {}
  25. });
  26. //Will return an array of fired events
  27. events.hasFired("Your App Name", "Your Event Name");

There is an option to pass "*" as event name and "*" as app name on all APIs which is an ALL indicator.

Commands

  1. var commands = new Chronos.Commands();
  2. function _yourCommandExecution(data, cb) {
  3. //Do something async with data and call cb when done.
  4. }
  5. //Comply to a command
  6. commands.comply({
  7. appName: "Your App Name",
  8. cmdName: "Your Command Name",
  9. func: _yourCommandExecution
  10. });
  11. //Stop complying to a command
  12. commands.stopComplying({
  13. appName: "Your App Name",
  14. cmdName: "Your Command Name",
  15. func: _yourCommandExecution
  16. });
  17. var cmd = {
  18. appName: "Your App Name",
  19. cmdName: "Your Event Name",
  20. data: {}
  21. }
  22. function notifyWhenDone(err) {
  23. if (!err) {
  24. console.log('Done executing command');
  25. }
  26. }
  27. //Issue the command
  28. commands.command(cmd, notifyWhenDone);
  29. //Will return an array of fired commands
  30. commands.hasFired("Your App Name", "Your Command Name");

The callback on the command is optional.

ReqRes

  1. var reqres = new Chronos.ReqRes();
  2. function _yourRequestExecution(data, cb) {
  3. //Do something async with data and call cb when done.
  4. return 1; //Whatever you want to return
  5. }
  6. //Reply to a request
  7. reqres.reply({
  8. appName: "Your App Name",
  9. reqName: "Your Request Name",
  10. func: _yourRequestExecution
  11. });
  12. //Stop replying to a request
  13. reqres.stopReplying({
  14. appName: "Your App Name",
  15. reqName: "Your Command Name",
  16. func: _yourRequestExecution
  17. });
  18. var req = {
  19. appName: "Your App Name",
  20. reqName: "Your Request Name",
  21. data: {}
  22. }
  23. function notifyWhenDoneWithResult(err, res) {
  24. if (!err) {
  25. console.log('Done executing request with result=' + JSON.stringify(res));
  26. }
  27. }
  28. //Issue the request
  29. var res = reqres.command(req, notifyWhenDoneWithResult);
  30. //Will return an array of fired requests
  31. reqres.hasFired("Your App Name", "Your Request Name");

The callback on the request is optional.

PostMessageCourier

  1. // Initialize a new Courier
  2. var courier = Chronos.PostMessageCourier({
  3. target: {
  4. url: "http://www.crossdomain.com/"
  5. }
  6. });
  7. ///// ---- BINDINGS ------ ////
  8. courier.bind({
  9. appName: "host",
  10. eventName: "multiply",
  11. func: multiply
  12. });
  13. courier.comply({
  14. appName: "host",
  15. cmdName: "square",
  16. func: square
  17. });
  18. courier.reply({
  19. appName: "host",
  20. reqName: "divide",
  21. func: divide
  22. });
  23. ///// ---- INVOCATION ------ ////
  24. courier.trigger({
  25. appName: "frame",
  26. eventName: "got_it",
  27. data: data * 2
  28. });
  29. courier.command({
  30. appName: "frame",
  31. cmdName: "expect",
  32. data: data
  33. }, function(err) {
  34. if (err) {
  35. console.log("Problem invoking command");
  36. }
  37. });
  38. courier.request({
  39. appName: "frame",
  40. reqName: "askBack",
  41. data: data
  42. }, function(err, data) {
  43. if (err) {
  44. console.log("Problem invoking request");
  45. return;
  46. }
  47. // Do Something with the data
  48. console.log(data);
  49. });

LIMITATIONS

  • Only supports browsers which implements postMessage API and have native JSON implementation (IE8+, Chrome, FF, Safari, Opera, IOS, Opera Mini, Android)
  • IE9-, FF & Opera Mini does not support MessageChannel and therefore we fallback to using basic postMessage. This makes the communication opened to any handler registered for messages on the same origin.
  • All passDataByRef flags (in Channels) are obviously ignored
  • In case the browser does not support passing object using postMessage (IE8+, Opera Mini), and no special serialize/deserialize methods are supplied to PostMessageCourier, All data is serialized using JSON.stringify/JSON.parse which means that Object data is limited to JSON which supports types like: strings, numbers, null, arrays, and objects (and does not allow circular references). Trying to serialize other types, will result in conversion to null (like Infinity or NaN) or to a string (Dates), that must be manually deserialized on the other side
  • When the IFRAME is managed outside of PostMessageCourier (passed by reference to the constructor), a targetOrigin option is expected to be passed to the constructor, and a query parameter with the name “lpHost” is expected on the IFRAME url (unless the PostMessageCourier at the IFRAME side, had also been initialized with a valid targetOrigin option)

Wrappers

License

MIT

Credits

Thanks to Danielle Dimenshtein for the logo

Session on this subject with code examples can be found here.

Demo using Angular and Chronos.