-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedConnector.js
250 lines (231 loc) · 7.93 KB
/
SharedConnector.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @class SharedConnector
* allows sharing of any kind of resource-hungry event emitter (e.g. long polling) between several tabs
* interface of such object should consist of 2 functions:
* @param {function(function(string))} start main [net]working function
* it gets callback as input and should call it with
* string whenever new message is received
* @param {function(string)} onMessage actual callback for message processing
*
*/
Ext.define('ElyGui.util.SharedConnector',
/*_* @lends SharedConnector prototype */ {
/**
* flag that this window/tab instance is master
* if it is == 1, we're the only tab
* if it is > 1, there're other tabs
* @field
* @type {?number}
*/
leading: null,
/**
* unique id, used to distinguish different tabs
* @field
* @type {?number}
*/
id: null,
/**
* list of all other tabs. Used to select another leader if current one is closed
* @field
* @type {Object.<number,number>}
*/
slaves: {},
/**
* value from SYNCLEAD bus, stored in local var
* @field
* @type {number}
*/
synclead: 0,
/**
* requesting interval (in ms), whether there's some tab alive
* @field
* @type {number}
*/
LS_SYNC_T: 30000,
/**
* localStorage bus name for leadership negotiations
* @field
* @type {string}
*/
SYNCLEAD: 'synclead',
/**
* localStorage bus name for calling-over tabs
* @field
* @type {string}
*/
SYNCLIST: 'synclist',
/**
* localStorage bus name for main data transfer
* @field
* @type {string}
*/
SYNCQUEUE: 'syncqueue',
/**
* generates unique name
* @type {number}
*/
generateName: function () {
return Math.round(Math.random() * (1e10 - 1)) + 1;
},
/**
* reminds others, that master is still alive
* @type {number}
*/
sync: function () {
localStorage.setItem(this.SYNCLEAD, Date.now());
},
/**
* utiliy method to distinguish tabs and support peaceful leadership transfer
* @param {number} arg
* @param {string} prefix
*/
name: function (arg, prefix) {
localStorage.setItem(this.SYNCLIST, (prefix || '') + arg);
},
/**
* process all "bureaucracy" related to becoming master
* @param {string} others comma separated values
*/
becomeMaster: function (others) {
this.sync();
this.slaves = {};
this.leading = 1;
if (typeof others == 'string') {
others.split(',').slice(1).forEach(function(n) { this.slaves[n] = this.leading++; }, this);
} else {
if (others) { this.name(0); } // ask alive tabs to name each other
}
var me = this;
this.start(function (msg) {
me.onMessage(msg);
localStorage.setItem(me.SYNCQUEUE, msg);
}); // start polling
},
/**
* init processing of sync signal
*/
initSyncLead: function () {
var me = this;
this.synclead = parseInt(localStorage.getItem(me.SYNCLEAD), 10) || 0;
window.addEventListener('storage', function (event) {
if (event.key != me.SYNCLEAD) { return; }
me.synclead = parseInt(event.newValue, 10);
me.leading = 0;
});
window.addEventListener('unload', function () {
if (me.leading) {
var a = [], k;
for (k in me.slaves) { a.push(k); }
if (a.length) {
me.name(a, '+');
me.leading = 0;
}
// just die, the last warrior
} else {
me.name(me.id, '-');
}
}, false);
// leadership sync signal
setInterval(function () {
if (me.leading >= 1) { me.sync(); }
if (!me.leading // we're not the leader
&& (Date.now() - me.LS_SYNC_T*2 > me.synclead)) { // and current one is dead
me.becomeMaster(true);
}
}, this.LS_SYNC_T);
},
/**
* init naming negotiations
*/
initNaming: function () {
var me = this;
var tryTakeLeadership;
function listenEcho (event) {
if (event.key != me.SYNCLEAD) { return; }
clearTimeout(tryTakeLeadership);
window.removeEventListener('storage', listenEcho, false);
}
window.addEventListener('storage', function (event) {
if (event.key != me.SYNCLIST) { return; }
var m = event.newValue.match(/^([!+\-])?(\d*)$/),
n = m[2];
switch (m[1]) {
case '-': // sometab says, it's going out
delete me.slaves[n];
me.leading--;
break;
case '+':
// current leader point sometab a new leader
// 'n' contains comma-separated full list of other ids
if (!me.leading && me.id == parseInt(n, 10)) { me.becomeMaster(n); }
// leader said: "decide by yourselves, who we'll be the king"
if (!me.leading && n == '') {
window.addEventListener('storage', listenEcho, false);
tryTakeLeadership = setTimeout(function () {
tryTakeLeadership = setTimeout(me.becomeMaster.bind(me), 1000);
}, this.id % 1000);
// listen to answer
}
break;
case '!': // somebody already use this name
if (!me.leading && n == me.id) { // if it's our name
// cry and run away
me.name(me.id = me.generateName());
}
break;
default:
if (me.id == n) { me.name(me.id, '!'); } // this name is already taken
if (0 == n) {
// distribute responses over one second
setTimeout(function () { me.name(me.id); }, me.id % 1000);
}
if (me.leading) {
me.sync(); // remind we're the leader
me.slaves[n] = me.leading++; // and write down we have another slave
}
}
}, false);
},
/**
* main initialization method
* @param {Object.<string,function>} cfg
* {function(function(string))} cfg.start runs main networking sequence and provide callback with results
* {function(string)} cfg.onMessage main callback for data response processing
*/
init: function (cfg) {
this.start = cfg.start;
this.onMessage = cfg.onMessage;
// first listen to data bus notifications
// if we'll take leadership, we'll not receive that data anyway
var me = this;
window.addEventListener('storage', function (event) {
if (event.key != me.SYNCQUEUE) { return; }
if (me.leading) { return; } // IE fires event even for tab initiated it
me.onMessage(event.newValue);
}, false);
this.initSyncLead();
this.initNaming();
this.decideMaster();
},
/**
* decides, whether we would be leader or there's sometab already
*/
decideMaster: function () {
var me = this;
this.leading = 0;
var fullFillLeadershipRequest;
// listen to response
function listenEcho (event) {
if (event.key == me.SYNCLEAD) { // there's a master already
clearTimeout(fullFillLeadershipRequest);
window.removeEventListener('storage', listenEcho, false);
}
}
// name ourselves
this.name(this.id = this.generateName());
// if nobody answer within one second, we are the only leader
fullFillLeadershipRequest = setTimeout(this.becomeMaster.bind(this), 1000);
// listen to answer
window.addEventListener('storage', listenEcho, false);
}
});