Skip to content

otociulis/iris

Repository files navigation

Travis CI Coverage Status

Overview

Join the chat at https://gitter.im/otociulis/iris

Iris is a simple message dispatch framework intendent to support message driven development in Javascript. Main idea behind message driven development is that application components communicate together via well-defined, immutable messages instead of calling methods on each other.

For example, sample application (see HelloTime sample) wishes to display current date and time in HTML web page. In this case we can identify two components:

  • date publishing component sending new date and time in regular intervals
  • rendering component displaying date and time in user format

Date publishing component (datetimeserver.js):

  var iris = require("iris");
    
  window.setInterval(function() {
      iris.send("dateTimeUpdated", new Date());
  }, 1000);

Rendering component (timecontrol.js):

  var iris = require("iris");
  
  iris.register("dateTimeUpdated", function(date) {
    document.getElementById("dateTime").innerText = date.toString();
  });

This approach allows very loosely tied component development which automatically allow good unit testing since there are essentially no ties between components; furthermore it's very easy to remove or add new components without affecting the functionality of the remaining components.

API

At core API consist of three methods:

  • register - registers for message
  • send - sends the message
  • unregister - unregisters receiver of message or message type

All methods returns iris object allowing to chain requests; usefull when multiple register or send calls are needed:

  iris.register("MyMessage", OnMyMessage)
      .register("MyDerivedMessage", OnMyMessageOrDerived);

Function register

  function register(type, callback);

Parameters:

  • type: string -name of the message to register for
  • callback: Function - callback method to be invoked when message arrives. Callback method will receive one parameter with arguments sent by send function
  function register(options, callback);

Parameters:

  • options - object allowing to fine-tune the behavior:
    • type: string - name of the message to register for
    • registerForSubclasses: boolean - indicates if callback should receive also messages derived from type
    • thisArg: any - used to bind the call to callback to other object. Usefull when defining callback inside other class (see below for example)
  • callback: Function - callback method to be invoked when message arrives. Callback method will receive one parameter with arguments sent by send function

To bind callback in other object use thisArg parameter:

var Target = (function () {
    function Target() {
        iris.register({ type: MyMessage.name, thisArg: this }, this.OnMyMessage);
    }
    
    function OnMyMessage(msg) {
        // this will be correct context - instance of class Target
    }
}

Function send

  function send(type, body);

Parameters:

  • type: string - name of the message to send
  • body: any - object to be sent
  function send(options);

Parameters:

  • options - object allowing to fine-tune the behavior:
    • type: string - name of the message to send
    • isLogging: boolean - indicates if message should be logged; usefull for performance improvements when particular message is sent many times
    • description: string - additional information to be logged (e.g. context information)

Function unregister

  function unregister(type);

Parameters:

  • type: string - name of the message to unregister from all receivers
  function unregister(obj);

This call works only for messages registered using thisArg option.

Parameters:

  • obj: any - instance of object from which to unregister all messages.
  function unregister();

Unregisters all messages and all receivers.

Hierarchy of messages

As application grows bigger it's possible to identify messages that share some data or the behavior can be specialized from same base class; for example

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •