2011-03-04 17:02:28 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
* OpenAjax.js
|
|
|
|
*
|
|
|
|
* Reference implementation of the OpenAjax Hub, as specified by OpenAjax Alliance.
|
2011-11-08 17:40:44 +01:00
|
|
|
* Specification is under development at:
|
2011-03-04 17:02:28 +01:00
|
|
|
*
|
|
|
|
* http://www.openajax.org/member/wiki/OpenAjax_Hub_Specification
|
|
|
|
*
|
|
|
|
* Copyright 2006-2007 OpenAjax Alliance
|
|
|
|
*
|
2011-11-08 17:40:44 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless
|
|
|
|
* required by applicable law or agreed to in writing, software distributed
|
|
|
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
2011-03-04 17:02:28 +01:00
|
|
|
* specific language governing permissions and limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
// prevent re-definition of the OpenAjax object
|
2010-11-15 08:39:52 +01:00
|
|
|
if(!window["OpenAjax"]){
|
2011-03-04 17:02:28 +01:00
|
|
|
OpenAjax = new function(){
|
2013-03-18 07:26:24 +01:00
|
|
|
// summary:
|
|
|
|
// the OpenAjax hub
|
|
|
|
// description:
|
|
|
|
// see http://www.openajax.org/member/wiki/OpenAjax_Hub_Specification
|
2011-03-04 17:02:28 +01:00
|
|
|
|
2013-03-18 07:26:24 +01:00
|
|
|
var libs = {};
|
2011-03-04 17:02:28 +01:00
|
|
|
var ooh = "org.openajax.hub.";
|
|
|
|
|
|
|
|
var h = {};
|
|
|
|
this.hub = h;
|
|
|
|
h.implementer = "http://openajax.org";
|
|
|
|
h.implVersion = "0.6";
|
|
|
|
h.specVersion = "0.6";
|
|
|
|
h.implExtraData = {};
|
|
|
|
h.libraries = libs;
|
|
|
|
|
|
|
|
h.registerLibrary = function(prefix, nsURL, version, extra){
|
|
|
|
libs[prefix] = {
|
|
|
|
prefix: prefix,
|
|
|
|
namespaceURI: nsURL,
|
|
|
|
version: version,
|
2011-11-08 17:40:44 +01:00
|
|
|
extraData: extra
|
2011-03-04 17:02:28 +01:00
|
|
|
};
|
|
|
|
this.publish(ooh+"registerLibrary", libs[prefix]);
|
2012-08-14 16:59:10 +02:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
h.unregisterLibrary = function(prefix){
|
|
|
|
this.publish(ooh+"unregisterLibrary", libs[prefix]);
|
|
|
|
delete libs[prefix];
|
2012-08-14 16:59:10 +02:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
|
|
|
h._subscriptions = { c:{}, s:[] };
|
|
|
|
h._cleanup = [];
|
|
|
|
h._subIndex = 0;
|
|
|
|
h._pubDepth = 0;
|
|
|
|
|
|
|
|
h.subscribe = function(name, callback, scope, subscriberData, filter){
|
|
|
|
if(!scope){
|
|
|
|
scope = window;
|
|
|
|
}
|
|
|
|
var handle = name + "." + this._subIndex;
|
|
|
|
var sub = { scope: scope, cb: callback, fcb: filter, data: subscriberData, sid: this._subIndex++, hdl: handle };
|
|
|
|
var path = name.split(".");
|
|
|
|
this._subscribe(this._subscriptions, path, 0, sub);
|
|
|
|
return handle;
|
2012-08-14 16:59:10 +02:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
|
|
|
h.publish = function(name, message){
|
|
|
|
var path = name.split(".");
|
|
|
|
this._pubDepth++;
|
|
|
|
this._publish(this._subscriptions, path, 0, name, message);
|
|
|
|
this._pubDepth--;
|
|
|
|
if((this._cleanup.length > 0) && (this._pubDepth == 0)){
|
2011-11-08 17:40:44 +01:00
|
|
|
for(var i = 0; i < this._cleanup.length; i++){
|
2011-03-04 17:02:28 +01:00
|
|
|
this.unsubscribe(this._cleanup[i].hdl);
|
|
|
|
}
|
|
|
|
delete(this._cleanup);
|
|
|
|
this._cleanup = [];
|
|
|
|
}
|
2012-08-14 16:59:10 +02:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
|
|
|
h.unsubscribe = function(sub){
|
|
|
|
var path = sub.split(".");
|
|
|
|
var sid = path.pop();
|
|
|
|
this._unsubscribe(this._subscriptions, path, 0, sid);
|
2012-08-14 16:59:10 +02:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
|
|
|
h._subscribe = function(tree, path, index, sub){
|
|
|
|
var token = path[index];
|
|
|
|
if(index == path.length){
|
|
|
|
tree.s.push(sub);
|
2011-11-08 17:40:44 +01:00
|
|
|
}else{
|
2011-03-04 17:02:28 +01:00
|
|
|
if(typeof tree.c == "undefined"){
|
2012-08-14 16:59:10 +02:00
|
|
|
tree.c = {};
|
2011-03-04 17:02:28 +01:00
|
|
|
}
|
|
|
|
if(typeof tree.c[token] == "undefined"){
|
2011-11-08 17:40:44 +01:00
|
|
|
tree.c[token] = { c: {}, s: [] };
|
2011-03-04 17:02:28 +01:00
|
|
|
}
|
2013-03-18 07:26:24 +01:00
|
|
|
this._subscribe(tree.c[token], path, index + 1, sub);
|
2011-03-04 17:02:28 +01:00
|
|
|
}
|
2012-08-14 16:59:10 +02:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
|
|
|
h._publish = function(tree, path, index, name, msg){
|
|
|
|
if(typeof tree != "undefined"){
|
|
|
|
var node;
|
2012-08-14 16:59:10 +02:00
|
|
|
if(index == path.length){
|
2011-03-04 17:02:28 +01:00
|
|
|
node = tree;
|
|
|
|
}else{
|
|
|
|
this._publish(tree.c[path[index]], path, index + 1, name, msg);
|
2011-11-08 17:40:44 +01:00
|
|
|
this._publish(tree.c["*"], path, index + 1, name, msg);
|
2011-03-04 17:02:28 +01:00
|
|
|
node = tree.c["**"];
|
|
|
|
}
|
|
|
|
if(typeof node != "undefined"){
|
|
|
|
var callbacks = node.s;
|
|
|
|
var max = callbacks.length;
|
|
|
|
for(var i = 0; i < max; i++){
|
|
|
|
if(callbacks[i].cb){
|
|
|
|
var sc = callbacks[i].scope;
|
|
|
|
var cb = callbacks[i].cb;
|
|
|
|
var fcb = callbacks[i].fcb;
|
|
|
|
var d = callbacks[i].data;
|
|
|
|
if(typeof cb == "string"){
|
|
|
|
// get a function object
|
|
|
|
cb = sc[cb];
|
|
|
|
}
|
|
|
|
if(typeof fcb == "string"){
|
|
|
|
// get a function object
|
|
|
|
fcb = sc[fcb];
|
|
|
|
}
|
2011-11-08 17:40:44 +01:00
|
|
|
if((!fcb) ||
|
2012-08-14 16:59:10 +02:00
|
|
|
(fcb.call(sc, name, msg, d))){
|
2011-03-04 17:02:28 +01:00
|
|
|
cb.call(sc, name, msg, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-14 16:59:10 +02:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
2012-08-14 16:59:10 +02:00
|
|
|
h._unsubscribe = function(tree, path, index, sid){
|
|
|
|
if(typeof tree != "undefined"){
|
|
|
|
if(index < path.length){
|
2011-03-04 17:02:28 +01:00
|
|
|
var childNode = tree.c[path[index]];
|
|
|
|
this._unsubscribe(childNode, path, index + 1, sid);
|
2012-08-14 16:59:10 +02:00
|
|
|
if(childNode.s.length == 0){
|
2011-11-08 17:40:44 +01:00
|
|
|
for(var x in childNode.c)
|
2012-08-14 16:59:10 +02:00
|
|
|
return;
|
2011-11-08 17:40:44 +01:00
|
|
|
delete tree.c[path[index]];
|
2011-03-04 17:02:28 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-08-14 16:59:10 +02:00
|
|
|
else{
|
2011-03-04 17:02:28 +01:00
|
|
|
var callbacks = tree.s;
|
|
|
|
var max = callbacks.length;
|
2013-03-18 07:26:24 +01:00
|
|
|
for(var i = 0; i < max; i++){
|
2012-08-14 16:59:10 +02:00
|
|
|
if(sid == callbacks[i].sid){
|
|
|
|
if(this._pubDepth > 0){
|
2011-11-08 17:40:44 +01:00
|
|
|
callbacks[i].cb = null;
|
|
|
|
this._cleanup.push(callbacks[i]);
|
2011-03-04 17:02:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
callbacks.splice(i, 1);
|
2011-11-08 17:40:44 +01:00
|
|
|
return;
|
2011-03-04 17:02:28 +01:00
|
|
|
}
|
2013-03-18 07:26:24 +01:00
|
|
|
}
|
2011-03-04 17:02:28 +01:00
|
|
|
}
|
|
|
|
}
|
2012-08-14 16:59:10 +02:00
|
|
|
};
|
2013-03-18 07:26:24 +01:00
|
|
|
|
2011-03-04 17:02:28 +01:00
|
|
|
// The following function is provided for automatic testing purposes.
|
|
|
|
// It is not expected to be deployed in run-time OpenAjax Hub implementations.
|
2013-03-18 07:26:24 +01:00
|
|
|
h.reinit = function(){
|
|
|
|
for (var lib in OpenAjax.hub.libraries){
|
2011-03-04 17:02:28 +01:00
|
|
|
delete OpenAjax.hub.libraries[lib];
|
|
|
|
}
|
|
|
|
OpenAjax.hub.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});
|
|
|
|
|
|
|
|
delete OpenAjax._subscriptions;
|
|
|
|
OpenAjax._subscriptions = {c:{},s:[]};
|
|
|
|
delete OpenAjax._cleanup;
|
|
|
|
OpenAjax._cleanup = [];
|
|
|
|
OpenAjax._subIndex = 0;
|
|
|
|
OpenAjax._pubDepth = 0;
|
2013-03-18 07:26:24 +01:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
};
|
2013-03-18 07:26:24 +01:00
|
|
|
|
2011-03-04 17:02:28 +01:00
|
|
|
// Register the OpenAjax Hub itself as a library.
|
|
|
|
OpenAjax.hub.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});
|
|
|
|
|
2010-11-15 08:39:52 +01:00
|
|
|
}
|