@@ -6,91 +6,83 @@ Object.defineProperty(exports, "__esModule", {
6
6
exports . OPEN_BROADCAST_CHANNELS = exports . BroadcastChannel = void 0 ;
7
7
exports . clearNodeFolder = clearNodeFolder ;
8
8
exports . enforceOptions = enforceOptions ;
9
-
10
9
var _util = require ( "./util.js" ) ;
11
-
12
10
var _methodChooser = require ( "./method-chooser.js" ) ;
13
-
14
11
var _options = require ( "./options.js" ) ;
15
-
16
12
/**
17
13
* Contains all open channels,
18
14
* used in tests to ensure everything is closed.
19
15
*/
20
16
var OPEN_BROADCAST_CHANNELS = new Set ( ) ;
21
17
exports . OPEN_BROADCAST_CHANNELS = OPEN_BROADCAST_CHANNELS ;
22
18
var lastId = 0 ;
23
-
24
19
var BroadcastChannel = function BroadcastChannel ( name , options ) {
25
20
// identifier of the channel to debug stuff
26
21
this . id = lastId ++ ;
27
22
OPEN_BROADCAST_CHANNELS . add ( this ) ;
28
23
this . name = name ;
29
-
30
24
if ( ENFORCED_OPTIONS ) {
31
25
options = ENFORCED_OPTIONS ;
32
26
}
33
-
34
27
this . options = ( 0 , _options . fillOptionsWithDefaults ) ( options ) ;
35
- this . method = ( 0 , _methodChooser . chooseMethod ) ( this . options ) ; // isListening
28
+ this . method = ( 0 , _methodChooser . chooseMethod ) ( this . options ) ;
36
29
30
+ // isListening
37
31
this . _iL = false ;
32
+
38
33
/**
39
34
* _onMessageListener
40
35
* setting onmessage twice,
41
36
* will overwrite the first listener
42
37
*/
43
-
44
38
this . _onML = null ;
39
+
45
40
/**
46
41
* _addEventListeners
47
42
*/
48
-
49
43
this . _addEL = {
50
44
message : [ ] ,
51
45
internal : [ ]
52
46
} ;
47
+
53
48
/**
54
49
* Unsend message promises
55
50
* where the sending is still in progress
56
51
* @type {Set<Promise> }
57
52
*/
58
-
59
53
this . _uMP = new Set ( ) ;
54
+
60
55
/**
61
56
* _beforeClose
62
57
* array of promises that will be awaited
63
58
* before the channel is closed
64
59
*/
65
-
66
60
this . _befC = [ ] ;
61
+
67
62
/**
68
63
* _preparePromise
69
64
*/
70
-
71
65
this . _prepP = null ;
72
-
73
66
_prepareChannel ( this ) ;
74
- } ; // STATICS
67
+ } ;
68
+
69
+ // STATICS
75
70
76
71
/**
77
72
* used to identify if someone overwrites
78
73
* window.BroadcastChannel with this
79
74
* See methods/native.js
80
75
*/
81
-
82
-
83
76
exports . BroadcastChannel = BroadcastChannel ;
84
77
BroadcastChannel . _pubkey = true ;
78
+
85
79
/**
86
80
* clears the tmp-folder if is node
87
81
* @return {Promise<boolean> } true if has run, false if not node
88
82
*/
89
-
90
83
function clearNodeFolder ( options ) {
91
84
options = ( 0 , _options . fillOptionsWithDefaults ) ( options ) ;
92
85
var method = ( 0 , _methodChooser . chooseMethod ) ( options ) ;
93
-
94
86
if ( method . type === 'node' ) {
95
87
return method . clearNodeFolder ( ) . then ( function ( ) {
96
88
return true ;
@@ -99,19 +91,17 @@ function clearNodeFolder(options) {
99
91
return _util . PROMISE_RESOLVED_FALSE ;
100
92
}
101
93
}
94
+
102
95
/**
103
96
* if set, this method is enforced,
104
97
* no mather what the options are
105
98
*/
106
-
107
-
108
99
var ENFORCED_OPTIONS ;
109
-
110
100
function enforceOptions ( options ) {
111
101
ENFORCED_OPTIONS = options ;
112
- } // PROTOTYPE
113
-
102
+ }
114
103
104
+ // PROTOTYPE
115
105
BroadcastChannel . prototype = {
116
106
postMessage : function postMessage ( msg ) {
117
107
if ( this . closed ) {
@@ -123,87 +113,77 @@ BroadcastChannel.prototype = {
123
113
*/
124
114
JSON . stringify ( msg ) ) ;
125
115
}
126
-
127
116
return _post ( this , 'message' , msg ) ;
128
117
} ,
129
118
postInternal : function postInternal ( msg ) {
130
119
return _post ( this , 'internal' , msg ) ;
131
120
} ,
132
-
133
121
set onmessage ( fn ) {
134
122
var time = this . method . microSeconds ( ) ;
135
123
var listenObj = {
136
124
time : time ,
137
125
fn : fn
138
126
} ;
139
-
140
127
_removeListenerObject ( this , 'message' , this . _onML ) ;
141
-
142
128
if ( fn && typeof fn === 'function' ) {
143
129
this . _onML = listenObj ;
144
-
145
130
_addListenerObject ( this , 'message' , listenObj ) ;
146
131
} else {
147
132
this . _onML = null ;
148
133
}
149
134
} ,
150
-
151
135
addEventListener : function addEventListener ( type , fn ) {
152
136
var time = this . method . microSeconds ( ) ;
153
137
var listenObj = {
154
138
time : time ,
155
139
fn : fn
156
140
} ;
157
-
158
141
_addListenerObject ( this , type , listenObj ) ;
159
142
} ,
160
143
removeEventListener : function removeEventListener ( type , fn ) {
161
144
var obj = this . _addEL [ type ] . find ( function ( obj ) {
162
145
return obj . fn === fn ;
163
146
} ) ;
164
-
165
147
_removeListenerObject ( this , type , obj ) ;
166
148
} ,
167
149
close : function close ( ) {
168
150
var _this = this ;
169
-
170
151
if ( this . closed ) {
171
152
return ;
172
153
}
173
-
174
154
OPEN_BROADCAST_CHANNELS [ "delete" ] ( this ) ;
175
155
this . closed = true ;
176
156
var awaitPrepare = this . _prepP ? this . _prepP : _util . PROMISE_RESOLVED_VOID ;
177
157
this . _onML = null ;
178
158
this . _addEL . message = [ ] ;
179
- return awaitPrepare // wait until all current sending are processed
159
+ return awaitPrepare
160
+ // wait until all current sending are processed
180
161
. then ( function ( ) {
181
162
return Promise . all ( Array . from ( _this . _uMP ) ) ;
182
- } ) // run before-close hooks
163
+ } )
164
+ // run before-close hooks
183
165
. then ( function ( ) {
184
166
return Promise . all ( _this . _befC . map ( function ( fn ) {
185
167
return fn ( ) ;
186
168
} ) ) ;
187
- } ) // close the channel
169
+ } )
170
+ // close the channel
188
171
. then ( function ( ) {
189
172
return _this . method . close ( _this . _state ) ;
190
173
} ) ;
191
174
} ,
192
-
193
175
get type ( ) {
194
176
return this . method . type ;
195
177
} ,
196
-
197
178
get isClosed ( ) {
198
179
return this . closed ;
199
180
}
200
-
201
181
} ;
182
+
202
183
/**
203
184
* Post a message over the channel
204
185
* @returns {Promise } that resolved when the message sending is done
205
186
*/
206
-
207
187
function _post ( broadcastChannel , type , msg ) {
208
188
var time = broadcastChannel . method . microSeconds ( ) ;
209
189
var msgObj = {
@@ -213,25 +193,22 @@ function _post(broadcastChannel, type, msg) {
213
193
} ;
214
194
var awaitPrepare = broadcastChannel . _prepP ? broadcastChannel . _prepP : _util . PROMISE_RESOLVED_VOID ;
215
195
return awaitPrepare . then ( function ( ) {
216
- var sendPromise = broadcastChannel . method . postMessage ( broadcastChannel . _state , msgObj ) ; // add/remove to unsend messages list
196
+ var sendPromise = broadcastChannel . method . postMessage ( broadcastChannel . _state , msgObj ) ;
217
197
198
+ // add/remove to unsend messages list
218
199
broadcastChannel . _uMP . add ( sendPromise ) ;
219
-
220
200
sendPromise [ "catch" ] ( ) . then ( function ( ) {
221
201
return broadcastChannel . _uMP [ "delete" ] ( sendPromise ) ;
222
202
} ) ;
223
203
return sendPromise ;
224
204
} ) ;
225
205
}
226
-
227
206
function _prepareChannel ( channel ) {
228
207
var maybePromise = channel . method . create ( channel . name , channel . options ) ;
229
-
230
208
if ( ( 0 , _util . isPromise ) ( maybePromise ) ) {
231
209
channel . _prepP = maybePromise ;
232
210
maybePromise . then ( function ( s ) {
233
211
// used in tests to simulate slow runtime
234
-
235
212
/*if (channel.options.prepareDelay) {
236
213
await new Promise(res => setTimeout(res, this.options.prepareDelay));
237
214
}*/
@@ -241,30 +218,25 @@ function _prepareChannel(channel) {
241
218
channel . _state = maybePromise ;
242
219
}
243
220
}
244
-
245
221
function _hasMessageListeners ( channel ) {
246
222
if ( channel . _addEL . message . length > 0 ) return true ;
247
223
if ( channel . _addEL . internal . length > 0 ) return true ;
248
224
return false ;
249
225
}
250
-
251
226
function _addListenerObject ( channel , type , obj ) {
252
227
channel . _addEL [ type ] . push ( obj ) ;
253
-
254
228
_startListening ( channel ) ;
255
229
}
256
-
257
230
function _removeListenerObject ( channel , type , obj ) {
258
231
channel . _addEL [ type ] = channel . _addEL [ type ] . filter ( function ( o ) {
259
232
return o !== obj ;
260
233
} ) ;
261
-
262
234
_stopListening ( channel ) ;
263
235
}
264
-
265
236
function _startListening ( channel ) {
266
237
if ( ! channel . _iL && _hasMessageListeners ( channel ) ) {
267
238
// someone is listening, start subscribing
239
+
268
240
var listenerFn = function listenerFn ( msgObj ) {
269
241
channel . _addEL [ msgObj . type ] . forEach ( function ( listenerObject ) {
270
242
/**
@@ -278,15 +250,12 @@ function _startListening(channel) {
278
250
*/
279
251
var hundredMsInMicro = 100 * 1000 ;
280
252
var minMessageTime = listenerObject . time - hundredMsInMicro ;
281
-
282
253
if ( msgObj . time >= minMessageTime ) {
283
254
listenerObject . fn ( msgObj . data ) ;
284
255
}
285
256
} ) ;
286
257
} ;
287
-
288
258
var time = channel . method . microSeconds ( ) ;
289
-
290
259
if ( channel . _prepP ) {
291
260
channel . _prepP . then ( function ( ) {
292
261
channel . _iL = true ;
@@ -298,7 +267,6 @@ function _startListening(channel) {
298
267
}
299
268
}
300
269
}
301
-
302
270
function _stopListening ( channel ) {
303
271
if ( channel . _iL && ! _hasMessageListeners ( channel ) ) {
304
272
// noone is listening, stop subscribing
0 commit comments