3
3
Object . defineProperty ( exports , "__esModule" , {
4
4
value : true
5
5
} ) ;
6
- exports . beLeader = beLeader ;
7
6
exports . createLeaderElection = createLeaderElection ;
8
7
var _util = require ( "./util.js" ) ;
9
- var _unload = require ( "unload" ) ;
8
+ var _leaderElectionUtil = require ( "./leader-election-util.js" ) ;
9
+ var _leaderElectionWebLock = require ( "./leader-election-web-lock.js" ) ;
10
10
var LeaderElection = function LeaderElection ( broadcastChannel , options ) {
11
11
var _this = this ;
12
12
this . broadcastChannel = broadcastChannel ;
13
13
this . _options = options ;
14
14
this . isLeader = false ;
15
- this . hasLeader = false ;
15
+ this . _hasLeader = false ;
16
16
this . isDead = false ;
17
17
this . token = ( 0 , _util . randomToken ) ( ) ;
18
18
@@ -39,17 +39,20 @@ var LeaderElection = function LeaderElection(broadcastChannel, options) {
39
39
var hasLeaderListener = function hasLeaderListener ( msg ) {
40
40
if ( msg . context === 'leader' ) {
41
41
if ( msg . action === 'death' ) {
42
- _this . hasLeader = false ;
42
+ _this . _hasLeader = false ;
43
43
}
44
44
if ( msg . action === 'tell' ) {
45
- _this . hasLeader = true ;
45
+ _this . _hasLeader = true ;
46
46
}
47
47
}
48
48
} ;
49
49
this . broadcastChannel . addEventListener ( 'internal' , hasLeaderListener ) ;
50
50
this . _lstns . push ( hasLeaderListener ) ;
51
51
} ;
52
52
LeaderElection . prototype = {
53
+ hasLeader : function hasLeader ( ) {
54
+ return Promise . resolve ( this . _hasLeader ) ;
55
+ } ,
53
56
/**
54
57
* Returns true if the instance is leader,
55
58
* false if not.
@@ -115,7 +118,7 @@ LeaderElection.prototype = {
115
118
if ( msg . action === 'tell' ) {
116
119
// other is already leader
117
120
stopCriteriaPromiseResolve ( ) ;
118
- _this2 . hasLeader = true ;
121
+ _this2 . _hasLeader = true ;
119
122
}
120
123
}
121
124
} ;
@@ -132,15 +135,15 @@ LeaderElection.prototype = {
132
135
* run in the background.
133
136
*/
134
137
var waitForAnswerTime = isFromFallbackInterval ? _this2 . _options . responseTime * 4 : _this2 . _options . responseTime ;
135
- return _sendMessage ( _this2 , 'apply' ) // send out that this one is applying
138
+ return ( 0 , _leaderElectionUtil . sendLeaderMessage ) ( _this2 , 'apply' ) // send out that this one is applying
136
139
. then ( function ( ) {
137
140
return Promise . race ( [ ( 0 , _util . sleep ) ( waitForAnswerTime ) , stopCriteriaPromise . then ( function ( ) {
138
141
return Promise . reject ( new Error ( ) ) ;
139
142
} ) ] ) ;
140
143
} )
141
144
// send again in case another instance was just created
142
145
. then ( function ( ) {
143
- return _sendMessage ( _this2 , 'apply' ) ;
146
+ return ( 0 , _leaderElectionUtil . sendLeaderMessage ) ( _this2 , 'apply' ) ;
144
147
} )
145
148
// let others time to respond
146
149
. then ( function ( ) {
@@ -151,7 +154,7 @@ LeaderElection.prototype = {
151
154
_this2 . broadcastChannel . removeEventListener ( 'internal' , handleMessage ) ;
152
155
if ( ! stopCriteria ) {
153
156
// no stop criteria -> own is leader
154
- return beLeader ( _this2 ) . then ( function ( ) {
157
+ return ( 0 , _leaderElectionUtil . beLeader ) ( _this2 ) . then ( function ( ) {
155
158
return true ;
156
159
} ) ;
157
160
} else {
@@ -191,11 +194,11 @@ LeaderElection.prototype = {
191
194
} ) ;
192
195
this . _unl = [ ] ;
193
196
if ( this . isLeader ) {
194
- this . hasLeader = false ;
197
+ this . _hasLeader = false ;
195
198
this . isLeader = false ;
196
199
}
197
200
this . isDead = true ;
198
- return _sendMessage ( this , 'death' ) ;
201
+ return ( 0 , _leaderElectionUtil . sendLeaderMessage ) ( this , 'death' ) ;
199
202
}
200
203
} ;
201
204
@@ -251,7 +254,7 @@ function _awaitLeadershipOnce(leaderElector) {
251
254
// try when other leader dies
252
255
var whenDeathListener = function whenDeathListener ( msg ) {
253
256
if ( msg . context === 'leader' && msg . action === 'death' ) {
254
- leaderElector . hasLeader = false ;
257
+ leaderElector . _hasLeader = false ;
255
258
leaderElector . applyOnce ( ) . then ( function ( ) {
256
259
if ( leaderElector . isLeader ) {
257
260
finish ( ) ;
@@ -263,48 +266,6 @@ function _awaitLeadershipOnce(leaderElector) {
263
266
leaderElector . _lstns . push ( whenDeathListener ) ;
264
267
} ) ;
265
268
}
266
-
267
- /**
268
- * sends and internal message over the broadcast-channel
269
- */
270
- function _sendMessage ( leaderElector , action ) {
271
- var msgJson = {
272
- context : 'leader' ,
273
- action : action ,
274
- token : leaderElector . token
275
- } ;
276
- return leaderElector . broadcastChannel . postInternal ( msgJson ) ;
277
- }
278
- function beLeader ( leaderElector ) {
279
- leaderElector . isLeader = true ;
280
- leaderElector . hasLeader = true ;
281
- var unloadFn = ( 0 , _unload . add ) ( function ( ) {
282
- return leaderElector . die ( ) ;
283
- } ) ;
284
- leaderElector . _unl . push ( unloadFn ) ;
285
- var isLeaderListener = function isLeaderListener ( msg ) {
286
- if ( msg . context === 'leader' && msg . action === 'apply' ) {
287
- _sendMessage ( leaderElector , 'tell' ) ;
288
- }
289
- if ( msg . context === 'leader' && msg . action === 'tell' && ! leaderElector . _dpLC ) {
290
- /**
291
- * another instance is also leader!
292
- * This can happen on rare events
293
- * like when the CPU is at 100% for long time
294
- * or the tabs are open very long and the browser throttles them.
295
- * @link https://github.com/pubkey/broadcast-channel/issues/414
296
- * @link https://github.com/pubkey/broadcast-channel/issues/385
297
- */
298
- leaderElector . _dpLC = true ;
299
- leaderElector . _dpL ( ) ; // message the lib user so the app can handle the problem
300
- _sendMessage ( leaderElector , 'tell' ) ; // ensure other leader also knows the problem
301
- }
302
- } ;
303
-
304
- leaderElector . broadcastChannel . addEventListener ( 'internal' , isLeaderListener ) ;
305
- leaderElector . _lstns . push ( isLeaderListener ) ;
306
- return _sendMessage ( leaderElector , 'tell' ) ;
307
- }
308
269
function fillOptionsWithDefaults ( options , channel ) {
309
270
if ( ! options ) options = { } ;
310
271
options = JSON . parse ( JSON . stringify ( options ) ) ;
@@ -321,7 +282,7 @@ function createLeaderElection(channel, options) {
321
282
throw new Error ( 'BroadcastChannel already has a leader-elector' ) ;
322
283
}
323
284
options = fillOptionsWithDefaults ( options , channel ) ;
324
- var elector = new LeaderElection ( channel , options ) ;
285
+ var elector = ( 0 , _util . supportsWebLockAPI ) ( ) ? new _leaderElectionWebLock . LeaderElectionWebLock ( channel , options ) : new LeaderElection ( channel , options ) ;
325
286
channel . _befC . push ( function ( ) {
326
287
return elector . die ( ) ;
327
288
} ) ;
0 commit comments