1 /*******************************************************************************
4 * Reference implementation of the OpenAjax Hub, as specified by OpenAjax Alliance.
5 * Specification is under development at:
7 * http://www.openajax.org/member/wiki/OpenAjax_Hub_Specification
9 * Copyright 2006-2007 OpenAjax Alliance
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
12 * use this file except in compliance with the License. You may obtain a copy
13 * of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless
14 * required by applicable law or agreed to in writing, software distributed
15 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
16 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations under the License.
19 ******************************************************************************/
21 // prevent re-definition of the OpenAjax object
22 if(!window["OpenAjax"]){
23 OpenAjax = new function(){
24 // summary: the OpenAjax hub
25 // description: see http://www.openajax.org/member/wiki/OpenAjax_Hub_Specification
31 var ooh = "org.openajax.hub.";
35 h.implementer = "http://openajax.org";
36 h.implVersion = "0.6";
37 h.specVersion = "0.6";
42 h.registerLibrary = function(prefix, nsURL, version, extra){
49 this.publish(ooh+"registerLibrary", libs[prefix]);
51 h.unregisterLibrary = function(prefix){
52 this.publish(ooh+"unregisterLibrary", libs[prefix]);
56 h._subscriptions = { c:{}, s:[] };
61 h.subscribe = function(name, callback, scope, subscriberData, filter){
65 var handle = name + "." + this._subIndex;
66 var sub = { scope: scope, cb: callback, fcb: filter, data: subscriberData, sid: this._subIndex++, hdl: handle };
67 var path = name.split(".");
68 this._subscribe(this._subscriptions, path, 0, sub);
72 h.publish = function(name, message){
73 var path = name.split(".");
75 this._publish(this._subscriptions, path, 0, name, message);
77 if((this._cleanup.length > 0) && (this._pubDepth == 0)){
78 for(var i = 0; i < this._cleanup.length; i++){
79 this.unsubscribe(this._cleanup[i].hdl);
81 delete(this._cleanup);
86 h.unsubscribe = function(sub){
87 var path = sub.split(".");
89 this._unsubscribe(this._subscriptions, path, 0, sid);
92 h._subscribe = function(tree, path, index, sub){
93 var token = path[index];
94 if(index == path.length){
97 if(typeof tree.c == "undefined"){
100 if(typeof tree.c[token] == "undefined"){
101 tree.c[token] = { c: {}, s: [] };
102 this._subscribe(tree.c[token], path, index + 1, sub);
104 this._subscribe( tree.c[token], path, index + 1, sub);
109 h._publish = function(tree, path, index, name, msg){
110 if(typeof tree != "undefined"){
112 if(index == path.length) {
115 this._publish(tree.c[path[index]], path, index + 1, name, msg);
116 this._publish(tree.c["*"], path, index + 1, name, msg);
119 if(typeof node != "undefined"){
120 var callbacks = node.s;
121 var max = callbacks.length;
122 for(var i = 0; i < max; i++){
124 var sc = callbacks[i].scope;
125 var cb = callbacks[i].cb;
126 var fcb = callbacks[i].fcb;
127 var d = callbacks[i].data;
128 if(typeof cb == "string"){
129 // get a function object
132 if(typeof fcb == "string"){
133 // get a function object
137 (fcb.call(sc, name, msg, d))) {
138 cb.call(sc, name, msg, d);
146 h._unsubscribe = function(tree, path, index, sid) {
147 if(typeof tree != "undefined") {
148 if(index < path.length) {
149 var childNode = tree.c[path[index]];
150 this._unsubscribe(childNode, path, index + 1, sid);
151 if(childNode.s.length == 0) {
152 for(var x in childNode.c)
154 delete tree.c[path[index]];
159 var callbacks = tree.s;
160 var max = callbacks.length;
161 for(var i = 0; i < max; i++)
162 if(sid == callbacks[i].sid) {
163 if(this._pubDepth > 0) {
164 callbacks[i].cb = null;
165 this._cleanup.push(callbacks[i]);
168 callbacks.splice(i, 1);
174 // The following function is provided for automatic testing purposes.
175 // It is not expected to be deployed in run-time OpenAjax Hub implementations.
176 h.reinit = function()
178 for (var lib in OpenAjax.hub.libraries) {
179 delete OpenAjax.hub.libraries[lib];
181 OpenAjax.hub.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});
183 delete OpenAjax._subscriptions;
184 OpenAjax._subscriptions = {c:{},s:[]};
185 delete OpenAjax._cleanup;
186 OpenAjax._cleanup = [];
187 OpenAjax._subIndex = 0;
188 OpenAjax._pubDepth = 0;
191 // Register the OpenAjax Hub itself as a library.
192 OpenAjax.hub.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});