-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery-hub.js
164 lines (138 loc) · 5.13 KB
/
jquery-hub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*global OpenAjax*/
// questionable if should be plugin https://github.com/cowboy/talks/blob/master/jquery-plugin-authoring.js
(function( $ ) {
var hubOptions = {},
readylist = null;
$.hub = function( options ) {
if ( options.ready === false ) {
// http://api.jquery.com/category/deferred-object/
readylist = $._Deferred();
hubOptions.ready = false;
}
if ( options.ready ) {
if ( readylist ) {
// execute any callbacks that were added
readylist.resolve();
}
}
if ( options.debug ) {
hubOptions.debug = ( options.debug === true );
}
return hubOptions;
};
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
$.hub.guid = function() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace( /[xy]/g, function( c ) {
var r = Math.random() * 16 | 0,
v = ( c === "x" ? r : ( r & 0x3 | 0x8 ) );
return v.toString( 16 );
} );
};
$.hub.message = function( body, options ) {
var message = {},
messageOptions = options || {};
message.body = body;
message.timestamp = ( new Date() ).getTime();
message.messageId = $.hub.guid();
message.formatVersion = messageOptions.formatVersion || "0";
return message;
};
$.hub.subscribe = function( channel, callback, scope, subscriberData, filter ) {
var callbackProxy = callback,
filterProxy = filter;
if ( scope ) {
callbackProxy = $.proxy( callback, scope );
}
if ( filter && hubOptions.stringify ) {
filterProxy = function( channel, message ) {
return filter( channel, JSON.parse( message ) );
};
}
return OpenAjax.hub.subscribe( channel /*name*/,
function( channel, message ) { callbackProxy( channel, ( hubOptions.stringify ? JSON.parse( message ) : message ) ); },
null, /* null scope because of $.proxy */
subscriberData,
filterProxy );
};
$.hub.unsubscribe = function( subscription ) {
OpenAjax.hub.unsubscribe( subscription );
};
var _publish = function( channel, message ) {
try {
if ( hubOptions.stringify ) {
message = JSON.stringify( message );
}
OpenAjax.hub.publish( channel /*name*/, message /*data*/ );
} catch ( e ) {
if ( hubOptions.debug ) {
debugger;
}
}
};
$.hub.publish = function( channel, message ) {
if ( readylist ) {
// add a callback to be executed when the deferred is resolved
readylist.done( function() {
_publish( channel, message );
} );
} else {
_publish( channel, message );
}
};
$.hub.createVersionFilter = function( version ) {
var major, minor;
version = /(\d+)\.?(\d+)?\.?(\d+)?/.exec( version );
major = parseInt( version[1], 10 ) || 0;
minor = parseInt( version[2], 10 ) || 0;
var filter = function( channel, message ) {
var messageVersion = /(\d+)\.?(\d+)?\.?(\d+)?/.exec( ( message ? message.formatVersion : "" ) ),
messageMajor = parseInt( messageVersion[1], 10 ) || 0,
messageMinor = parseInt( messageVersion[2], 10 ) || 0;
return ( major === messageMajor && ( minor ? minor === messageMinor : true ) );
};
return filter;
};
$.hub.createIdempotentFilter = function() {
var messageIds = {};
var filter = function( channel, message ) {
if ( message && message.messageId ) {
if ( messageIds[message.messageId] ) {
return false;
} else {
messageIds[message.messageId] = message.timestamp;
}
}
return true;
};
return filter;
};
$.hub.reply = function( message, replyMessage ) {
if ( message && replyMessage && message.replyTo ) {
replyMessage.correlationId = message.messageId;
$.hub.publish( message.replyTo, replyMessage );
return true;
}
return false;
};
$.hub.requestReply = function( channel, message, callback ) {
var replyChannel = $.hub.guid(),
subscription = $.hub.subscribe( replyChannel, function( channel, message ) {
callback( channel, message );
$.hub.unsubscribe( subscription );
} );
message.replyTo = replyChannel;
$.hub.publish( channel, message );
};
// exposed for automated testing purposes
$.hub._reset = function() {
hubOptions.ready = true;
hubOptions.debug = false;
hubOptions.stringify = true;
readylist = null;
if ( OpenAjax.hub.reinit ) {
OpenAjax.hub.reinit();
}
};
// before returning, initialize the hubOptions
$.hub._reset();
} )( jQuery );