@@ -6,91 +6,83 @@ Object.defineProperty(exports, "__esModule", {
66exports . OPEN_BROADCAST_CHANNELS = exports . BroadcastChannel = void 0 ;
77exports . clearNodeFolder = clearNodeFolder ;
88exports . enforceOptions = enforceOptions ;
9-
109var _util = require ( "./util.js" ) ;
11-
1210var _methodChooser = require ( "./method-chooser.js" ) ;
13-
1411var _options = require ( "./options.js" ) ;
15-
1612/**
1713 * Contains all open channels,
1814 * used in tests to ensure everything is closed.
1915 */
2016var OPEN_BROADCAST_CHANNELS = new Set ( ) ;
2117exports . OPEN_BROADCAST_CHANNELS = OPEN_BROADCAST_CHANNELS ;
2218var lastId = 0 ;
23-
2419var BroadcastChannel = function BroadcastChannel ( name , options ) {
2520 // identifier of the channel to debug stuff
2621 this . id = lastId ++ ;
2722 OPEN_BROADCAST_CHANNELS . add ( this ) ;
2823 this . name = name ;
29-
3024 if ( ENFORCED_OPTIONS ) {
3125 options = ENFORCED_OPTIONS ;
3226 }
33-
3427 this . options = ( 0 , _options . fillOptionsWithDefaults ) ( options ) ;
35- this . method = ( 0 , _methodChooser . chooseMethod ) ( this . options ) ; // isListening
28+ this . method = ( 0 , _methodChooser . chooseMethod ) ( this . options ) ;
3629
30+ // isListening
3731 this . _iL = false ;
32+
3833 /**
3934 * _onMessageListener
4035 * setting onmessage twice,
4136 * will overwrite the first listener
4237 */
43-
4438 this . _onML = null ;
39+
4540 /**
4641 * _addEventListeners
4742 */
48-
4943 this . _addEL = {
5044 message : [ ] ,
5145 internal : [ ]
5246 } ;
47+
5348 /**
5449 * Unsend message promises
5550 * where the sending is still in progress
5651 * @type {Set<Promise> }
5752 */
58-
5953 this . _uMP = new Set ( ) ;
54+
6055 /**
6156 * _beforeClose
6257 * array of promises that will be awaited
6358 * before the channel is closed
6459 */
65-
6660 this . _befC = [ ] ;
61+
6762 /**
6863 * _preparePromise
6964 */
70-
7165 this . _prepP = null ;
72-
7366 _prepareChannel ( this ) ;
74- } ; // STATICS
67+ } ;
68+
69+ // STATICS
7570
7671/**
7772 * used to identify if someone overwrites
7873 * window.BroadcastChannel with this
7974 * See methods/native.js
8075 */
81-
82-
8376exports . BroadcastChannel = BroadcastChannel ;
8477BroadcastChannel . _pubkey = true ;
78+
8579/**
8680 * clears the tmp-folder if is node
8781 * @return {Promise<boolean> } true if has run, false if not node
8882 */
89-
9083function clearNodeFolder ( options ) {
9184 options = ( 0 , _options . fillOptionsWithDefaults ) ( options ) ;
9285 var method = ( 0 , _methodChooser . chooseMethod ) ( options ) ;
93-
9486 if ( method . type === 'node' ) {
9587 return method . clearNodeFolder ( ) . then ( function ( ) {
9688 return true ;
@@ -99,19 +91,17 @@ function clearNodeFolder(options) {
9991 return _util . PROMISE_RESOLVED_FALSE ;
10092 }
10193}
94+
10295/**
10396 * if set, this method is enforced,
10497 * no mather what the options are
10598 */
106-
107-
10899var ENFORCED_OPTIONS ;
109-
110100function enforceOptions ( options ) {
111101 ENFORCED_OPTIONS = options ;
112- } // PROTOTYPE
113-
102+ }
114103
104+ // PROTOTYPE
115105BroadcastChannel . prototype = {
116106 postMessage : function postMessage ( msg ) {
117107 if ( this . closed ) {
@@ -123,87 +113,77 @@ BroadcastChannel.prototype = {
123113 */
124114 JSON . stringify ( msg ) ) ;
125115 }
126-
127116 return _post ( this , 'message' , msg ) ;
128117 } ,
129118 postInternal : function postInternal ( msg ) {
130119 return _post ( this , 'internal' , msg ) ;
131120 } ,
132-
133121 set onmessage ( fn ) {
134122 var time = this . method . microSeconds ( ) ;
135123 var listenObj = {
136124 time : time ,
137125 fn : fn
138126 } ;
139-
140127 _removeListenerObject ( this , 'message' , this . _onML ) ;
141-
142128 if ( fn && typeof fn === 'function' ) {
143129 this . _onML = listenObj ;
144-
145130 _addListenerObject ( this , 'message' , listenObj ) ;
146131 } else {
147132 this . _onML = null ;
148133 }
149134 } ,
150-
151135 addEventListener : function addEventListener ( type , fn ) {
152136 var time = this . method . microSeconds ( ) ;
153137 var listenObj = {
154138 time : time ,
155139 fn : fn
156140 } ;
157-
158141 _addListenerObject ( this , type , listenObj ) ;
159142 } ,
160143 removeEventListener : function removeEventListener ( type , fn ) {
161144 var obj = this . _addEL [ type ] . find ( function ( obj ) {
162145 return obj . fn === fn ;
163146 } ) ;
164-
165147 _removeListenerObject ( this , type , obj ) ;
166148 } ,
167149 close : function close ( ) {
168150 var _this = this ;
169-
170151 if ( this . closed ) {
171152 return ;
172153 }
173-
174154 OPEN_BROADCAST_CHANNELS [ "delete" ] ( this ) ;
175155 this . closed = true ;
176156 var awaitPrepare = this . _prepP ? this . _prepP : _util . PROMISE_RESOLVED_VOID ;
177157 this . _onML = null ;
178158 this . _addEL . message = [ ] ;
179- return awaitPrepare // wait until all current sending are processed
159+ return awaitPrepare
160+ // wait until all current sending are processed
180161 . then ( function ( ) {
181162 return Promise . all ( Array . from ( _this . _uMP ) ) ;
182- } ) // run before-close hooks
163+ } )
164+ // run before-close hooks
183165 . then ( function ( ) {
184166 return Promise . all ( _this . _befC . map ( function ( fn ) {
185167 return fn ( ) ;
186168 } ) ) ;
187- } ) // close the channel
169+ } )
170+ // close the channel
188171 . then ( function ( ) {
189172 return _this . method . close ( _this . _state ) ;
190173 } ) ;
191174 } ,
192-
193175 get type ( ) {
194176 return this . method . type ;
195177 } ,
196-
197178 get isClosed ( ) {
198179 return this . closed ;
199180 }
200-
201181} ;
182+
202183/**
203184 * Post a message over the channel
204185 * @returns {Promise } that resolved when the message sending is done
205186 */
206-
207187function _post ( broadcastChannel , type , msg ) {
208188 var time = broadcastChannel . method . microSeconds ( ) ;
209189 var msgObj = {
@@ -213,25 +193,22 @@ function _post(broadcastChannel, type, msg) {
213193 } ;
214194 var awaitPrepare = broadcastChannel . _prepP ? broadcastChannel . _prepP : _util . PROMISE_RESOLVED_VOID ;
215195 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 ) ;
217197
198+ // add/remove to unsend messages list
218199 broadcastChannel . _uMP . add ( sendPromise ) ;
219-
220200 sendPromise [ "catch" ] ( ) . then ( function ( ) {
221201 return broadcastChannel . _uMP [ "delete" ] ( sendPromise ) ;
222202 } ) ;
223203 return sendPromise ;
224204 } ) ;
225205}
226-
227206function _prepareChannel ( channel ) {
228207 var maybePromise = channel . method . create ( channel . name , channel . options ) ;
229-
230208 if ( ( 0 , _util . isPromise ) ( maybePromise ) ) {
231209 channel . _prepP = maybePromise ;
232210 maybePromise . then ( function ( s ) {
233211 // used in tests to simulate slow runtime
234-
235212 /*if (channel.options.prepareDelay) {
236213 await new Promise(res => setTimeout(res, this.options.prepareDelay));
237214 }*/
@@ -241,30 +218,25 @@ function _prepareChannel(channel) {
241218 channel . _state = maybePromise ;
242219 }
243220}
244-
245221function _hasMessageListeners ( channel ) {
246222 if ( channel . _addEL . message . length > 0 ) return true ;
247223 if ( channel . _addEL . internal . length > 0 ) return true ;
248224 return false ;
249225}
250-
251226function _addListenerObject ( channel , type , obj ) {
252227 channel . _addEL [ type ] . push ( obj ) ;
253-
254228 _startListening ( channel ) ;
255229}
256-
257230function _removeListenerObject ( channel , type , obj ) {
258231 channel . _addEL [ type ] = channel . _addEL [ type ] . filter ( function ( o ) {
259232 return o !== obj ;
260233 } ) ;
261-
262234 _stopListening ( channel ) ;
263235}
264-
265236function _startListening ( channel ) {
266237 if ( ! channel . _iL && _hasMessageListeners ( channel ) ) {
267238 // someone is listening, start subscribing
239+
268240 var listenerFn = function listenerFn ( msgObj ) {
269241 channel . _addEL [ msgObj . type ] . forEach ( function ( listenerObject ) {
270242 /**
@@ -278,15 +250,12 @@ function _startListening(channel) {
278250 */
279251 var hundredMsInMicro = 100 * 1000 ;
280252 var minMessageTime = listenerObject . time - hundredMsInMicro ;
281-
282253 if ( msgObj . time >= minMessageTime ) {
283254 listenerObject . fn ( msgObj . data ) ;
284255 }
285256 } ) ;
286257 } ;
287-
288258 var time = channel . method . microSeconds ( ) ;
289-
290259 if ( channel . _prepP ) {
291260 channel . _prepP . then ( function ( ) {
292261 channel . _iL = true ;
@@ -298,7 +267,6 @@ function _startListening(channel) {
298267 }
299268 }
300269}
301-
302270function _stopListening ( channel ) {
303271 if ( channel . _iL && ! _hasMessageListeners ( channel ) ) {
304272 // noone is listening, stop subscribing
0 commit comments