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.
At core API consist of three methods:
register
- registers for messagesend
- sends the messageunregister
- 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(type, callback);
Parameters:
type
: string -name of the message to register forcallback
: Function - callback method to be invoked when message arrives. Callback method will receive one parameter with arguments sent bysend
function
function register(options, callback);
Parameters:
options
- object allowing to fine-tune the behavior:type
: string - name of the message to register forregisterForSubclasses
: boolean - indicates ifcallback
should receive also messages derived fromtype
thisArg
: any - used tobind
the call tocallback
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 bysend
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(type, body);
Parameters:
type
: string - name of the message to sendbody
: any - object to be sent
function send(options);
Parameters:
options
- object allowing to fine-tune the behavior:type
: string - name of the message to sendisLogging
: boolean - indicates if message should be logged; usefull for performance improvements when particular message is sent many timesdescription
: string - additional information to be logged (e.g. context information)
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.
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