]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/OpenAjax.js
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(){
27 // see http://www.openajax.org/member/wiki/OpenAjax_Hub_Specification
30 var ooh
= "org.openajax.hub.";
34 h
.implementer
= "http://openajax.org";
35 h
.implVersion
= "0.6";
36 h
.specVersion
= "0.6";
40 h
.registerLibrary = function(prefix
, nsURL
, version
, extra
){
47 this.publish(ooh
+"registerLibrary", libs
[prefix
]);
49 h
.unregisterLibrary = function(prefix
){
50 this.publish(ooh
+"unregisterLibrary", libs
[prefix
]);
54 h
._subscriptions
= { c
:{}, s
:[] };
59 h
.subscribe = function(name
, callback
, scope
, subscriberData
, filter
){
63 var handle
= name
+ "." + this._subIndex
;
64 var sub
= { scope
: scope
, cb
: callback
, fcb
: filter
, data
: subscriberData
, sid
: this._subIndex
++, hdl
: handle
};
65 var path
= name
.split(".");
66 this._subscribe(this._subscriptions
, path
, 0, sub
);
70 h
.publish = function(name
, message
){
71 var path
= name
.split(".");
73 this._publish(this._subscriptions
, path
, 0, name
, message
);
75 if((this._cleanup
.length
> 0) && (this._pubDepth
== 0)){
76 for(var i
= 0; i
< this._cleanup
.length
; i
++){
77 this.unsubscribe(this._cleanup
[i
].hdl
);
79 delete(this._cleanup
);
84 h
.unsubscribe = function(sub
){
85 var path
= sub
.split(".");
87 this._unsubscribe(this._subscriptions
, path
, 0, sid
);
90 h
._subscribe = function(tree
, path
, index
, sub
){
91 var token
= path
[index
];
92 if(index
== path
.length
){
95 if(typeof tree
.c
== "undefined"){
98 if(typeof tree
.c
[token
] == "undefined"){
99 tree
.c
[token
] = { c
: {}, s
: [] };
101 this._subscribe(tree
.c
[token
], path
, index
+ 1, sub
);
105 h
._publish = function(tree
, path
, index
, name
, msg
){
106 if(typeof tree
!= "undefined"){
108 if(index
== path
.length
){
111 this._publish(tree
.c
[path
[index
]], path
, index
+ 1, name
, msg
);
112 this._publish(tree
.c
["*"], path
, index
+ 1, name
, msg
);
115 if(typeof node
!= "undefined"){
116 var callbacks
= node
.s
;
117 var max
= callbacks
.length
;
118 for(var i
= 0; i
< max
; i
++){
120 var sc
= callbacks
[i
].scope
;
121 var cb
= callbacks
[i
].cb
;
122 var fcb
= callbacks
[i
].fcb
;
123 var d
= callbacks
[i
].data
;
124 if(typeof cb
== "string"){
125 // get a function object
128 if(typeof fcb
== "string"){
129 // get a function object
133 (fcb
.call(sc
, name
, msg
, d
))){
134 cb
.call(sc
, name
, msg
, d
);
142 h
._unsubscribe = function(tree
, path
, index
, sid
){
143 if(typeof tree
!= "undefined"){
144 if(index
< path
.length
){
145 var childNode
= tree
.c
[path
[index
]];
146 this._unsubscribe(childNode
, path
, index
+ 1, sid
);
147 if(childNode
.s
.length
== 0){
148 for(var x
in childNode
.c
)
150 delete tree
.c
[path
[index
]];
155 var callbacks
= tree
.s
;
156 var max
= callbacks
.length
;
157 for(var i
= 0; i
< max
; i
++){
158 if(sid
== callbacks
[i
].sid
){
159 if(this._pubDepth
> 0){
160 callbacks
[i
].cb
= null;
161 this._cleanup
.push(callbacks
[i
]);
164 callbacks
.splice(i
, 1);
172 // The following function is provided for automatic testing purposes.
173 // It is not expected to be deployed in run-time OpenAjax Hub implementations.
174 h
.reinit = function(){
175 for (var lib
in OpenAjax
.hub
.libraries
){
176 delete OpenAjax
.hub
.libraries
[lib
];
178 OpenAjax
.hub
.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});
180 delete OpenAjax
._subscriptions
;
181 OpenAjax
._subscriptions
= {c
:{},s
:[]};
182 delete OpenAjax
._cleanup
;
183 OpenAjax
._cleanup
= [];
184 OpenAjax
._subIndex
= 0;
185 OpenAjax
._pubDepth
= 0;
189 // Register the OpenAjax Hub itself as a library.
190 OpenAjax
.hub
.registerLibrary("OpenAjax", "http://openajax.org/hub", "0.6", {});