@whitesev/pops 3.1.1 → 3.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.umd.js CHANGED
@@ -186,19 +186,9 @@
186
186
  window: window,
187
187
  globalThis: globalThis,
188
188
  self: self,
189
- setTimeout: globalThis.setTimeout.bind(globalThis),
190
- setInterval: globalThis.setInterval.bind(globalThis),
191
- clearTimeout: globalThis.clearTimeout.bind(globalThis),
192
- clearInterval: globalThis.clearInterval.bind(globalThis),
193
189
  };
194
190
  const PopsCoreEnv = Object.assign({}, PopsCoreDefaultEnv);
195
191
  const PopsCore = {
196
- init(option) {
197
- if (!option) {
198
- option = Object.assign({}, PopsCoreDefaultEnv);
199
- }
200
- Object.assign(PopsCoreEnv, option);
201
- },
202
192
  get document() {
203
193
  return PopsCoreEnv.document;
204
194
  },
@@ -211,18 +201,6 @@
211
201
  get self() {
212
202
  return PopsCoreEnv.self;
213
203
  },
214
- get setTimeout() {
215
- return PopsCoreEnv.setTimeout;
216
- },
217
- get setInterval() {
218
- return PopsCoreEnv.setInterval;
219
- },
220
- get clearTimeout() {
221
- return PopsCoreEnv.clearTimeout;
222
- },
223
- get clearInterval() {
224
- return PopsCoreEnv.clearInterval;
225
- },
226
204
  };
227
205
  const OriginPrototype = {
228
206
  Object: {
@@ -287,47 +265,7 @@
287
265
  const cache = createCache(LAST_NUMBER_WEAK_MAP);
288
266
  const generateUniqueNumber = createGenerateUniqueNumber(cache, LAST_NUMBER_WEAK_MAP);
289
267
 
290
- const isMessagePort = (sender) => {
291
- return typeof sender.start === 'function';
292
- };
293
-
294
- const PORT_MAP = new WeakMap();
295
-
296
- const extendBrokerImplementation = (partialBrokerImplementation) => ({
297
- ...partialBrokerImplementation,
298
- connect: ({ call }) => {
299
- return async () => {
300
- const { port1, port2 } = new MessageChannel();
301
- const portId = await call('connect', { port: port1 }, [port1]);
302
- PORT_MAP.set(port2, portId);
303
- return port2;
304
- };
305
- },
306
- disconnect: ({ call }) => {
307
- return async (port) => {
308
- const portId = PORT_MAP.get(port);
309
- if (portId === undefined) {
310
- throw new Error('The given port is not connected.');
311
- }
312
- await call('disconnect', { portId });
313
- };
314
- },
315
- isSupported: ({ call }) => {
316
- return () => call('isSupported');
317
- }
318
- });
319
-
320
- const ONGOING_REQUESTS = new WeakMap();
321
- const createOrGetOngoingRequests = (sender) => {
322
- if (ONGOING_REQUESTS.has(sender)) {
323
- // @todo TypeScript needs to be convinced that has() works as expected.
324
- return ONGOING_REQUESTS.get(sender);
325
- }
326
- const ongoingRequests = new Map();
327
- ONGOING_REQUESTS.set(sender, ongoingRequests);
328
- return ongoingRequests;
329
- };
330
- const createBroker = (brokerImplementation) => {
268
+ const createBrokerFactory = (createOrGetOngoingRequests, extendBrokerImplementation, generateUniqueNumber, isMessagePort) => (brokerImplementation) => {
331
269
  const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
332
270
  return (sender) => {
333
271
  const ongoingRequests = createOrGetOngoingRequests(sender);
@@ -370,82 +308,116 @@
370
308
  };
371
309
  };
372
310
 
373
- // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
374
- const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
375
- const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
376
- const wrap = createBroker({
377
- clearInterval: ({ call }) => {
378
- return (timerId) => {
379
- if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
380
- scheduledIntervalsState.set(timerId, null);
381
- call('clear', { timerId, timerType: 'interval' }).then(() => {
382
- scheduledIntervalsState.delete(timerId);
383
- });
384
- }
311
+ const createCreateOrGetOngoingRequests = (ongoingRequestsMap) => (sender) => {
312
+ if (ongoingRequestsMap.has(sender)) {
313
+ // @todo TypeScript needs to be convinced that has() works as expected.
314
+ return ongoingRequestsMap.get(sender);
315
+ }
316
+ const ongoingRequests = new Map();
317
+ ongoingRequestsMap.set(sender, ongoingRequests);
318
+ return ongoingRequests;
319
+ };
320
+
321
+ const createExtendBrokerImplementation = (portMap) => (partialBrokerImplementation) => ({
322
+ ...partialBrokerImplementation,
323
+ connect: ({ call }) => {
324
+ return async () => {
325
+ const { port1, port2 } = new MessageChannel();
326
+ const portId = await call('connect', { port: port1 }, [port1]);
327
+ portMap.set(port2, portId);
328
+ return port2;
385
329
  };
386
330
  },
387
- clearTimeout: ({ call }) => {
388
- return (timerId) => {
389
- if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
390
- scheduledTimeoutsState.set(timerId, null);
391
- call('clear', { timerId, timerType: 'timeout' }).then(() => {
392
- scheduledTimeoutsState.delete(timerId);
393
- });
331
+ disconnect: ({ call }) => {
332
+ return async (port) => {
333
+ const portId = portMap.get(port);
334
+ if (portId === undefined) {
335
+ throw new Error('The given port is not connected.');
394
336
  }
337
+ await call('disconnect', { portId });
395
338
  };
396
339
  },
397
- setInterval: ({ call }) => {
398
- return (func, delay = 0, ...args) => {
399
- const symbol = Symbol();
400
- const timerId = generateUniqueNumber(scheduledIntervalsState);
401
- scheduledIntervalsState.set(timerId, symbol);
402
- const schedule = () => call('set', {
403
- delay,
404
- now: performance.timeOrigin + performance.now(),
405
- timerId,
406
- timerType: 'interval'
407
- }).then(() => {
408
- const state = scheduledIntervalsState.get(timerId);
409
- if (state === undefined) {
410
- throw new Error('The timer is in an undefined state.');
411
- }
412
- if (state === symbol) {
413
- func(...args);
414
- // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
415
- if (scheduledIntervalsState.get(timerId) === symbol) {
416
- schedule();
417
- }
418
- }
419
- });
420
- schedule();
421
- return timerId;
422
- };
423
- },
424
- setTimeout: ({ call }) => {
425
- return (func, delay = 0, ...args) => {
426
- const symbol = Symbol();
427
- const timerId = generateUniqueNumber(scheduledTimeoutsState);
428
- scheduledTimeoutsState.set(timerId, symbol);
429
- call('set', {
430
- delay,
431
- now: performance.timeOrigin + performance.now(),
432
- timerId,
433
- timerType: 'timeout'
434
- }).then(() => {
435
- const state = scheduledTimeoutsState.get(timerId);
436
- if (state === undefined) {
437
- throw new Error('The timer is in an undefined state.');
438
- }
439
- if (state === symbol) {
440
- // A timeout can be savely deleted because it is only called once.
441
- scheduledTimeoutsState.delete(timerId);
442
- func(...args);
443
- }
444
- });
445
- return timerId;
446
- };
340
+ isSupported: ({ call }) => {
341
+ return () => call('isSupported');
447
342
  }
448
343
  });
344
+
345
+ const isMessagePort = (sender) => {
346
+ return typeof sender.start === 'function';
347
+ };
348
+
349
+ const createBroker = createBrokerFactory(createCreateOrGetOngoingRequests(new WeakMap()), createExtendBrokerImplementation(new WeakMap()), generateUniqueNumber, isMessagePort);
350
+
351
+ const createClearIntervalFactory = (scheduledIntervalsState) => (clear) => (timerId) => {
352
+ if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
353
+ scheduledIntervalsState.set(timerId, null);
354
+ clear(timerId).then(() => {
355
+ scheduledIntervalsState.delete(timerId);
356
+ });
357
+ }
358
+ };
359
+
360
+ const createClearTimeoutFactory = (scheduledTimeoutsState) => (clear) => (timerId) => {
361
+ if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
362
+ scheduledTimeoutsState.set(timerId, null);
363
+ clear(timerId).then(() => {
364
+ scheduledTimeoutsState.delete(timerId);
365
+ });
366
+ }
367
+ };
368
+
369
+ const createSetIntervalFactory = (generateUniqueNumber, scheduledIntervalsState) => (set) => (func, delay = 0, ...args) => {
370
+ const symbol = Symbol();
371
+ const timerId = generateUniqueNumber(scheduledIntervalsState);
372
+ scheduledIntervalsState.set(timerId, symbol);
373
+ const schedule = () => set(delay, timerId).then(() => {
374
+ const state = scheduledIntervalsState.get(timerId);
375
+ if (state === undefined) {
376
+ throw new Error('The timer is in an undefined state.');
377
+ }
378
+ if (state === symbol) {
379
+ func(...args);
380
+ // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
381
+ if (scheduledIntervalsState.get(timerId) === symbol) {
382
+ schedule();
383
+ }
384
+ }
385
+ });
386
+ schedule();
387
+ return timerId;
388
+ };
389
+
390
+ const createSetTimeoutFactory = (generateUniqueNumber, scheduledTimeoutsState) => (set) => (func, delay = 0, ...args) => {
391
+ const symbol = Symbol();
392
+ const timerId = generateUniqueNumber(scheduledTimeoutsState);
393
+ scheduledTimeoutsState.set(timerId, symbol);
394
+ set(delay, timerId).then(() => {
395
+ const state = scheduledTimeoutsState.get(timerId);
396
+ if (state === undefined) {
397
+ throw new Error('The timer is in an undefined state.');
398
+ }
399
+ if (state === symbol) {
400
+ // A timeout can be savely deleted because it is only called once.
401
+ scheduledTimeoutsState.delete(timerId);
402
+ func(...args);
403
+ }
404
+ });
405
+ return timerId;
406
+ };
407
+
408
+ // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
409
+ const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
410
+ const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
411
+ const createClearInterval = createClearIntervalFactory(scheduledIntervalsState);
412
+ const createClearTimeout = createClearTimeoutFactory(scheduledTimeoutsState);
413
+ const createSetInterval = createSetIntervalFactory(generateUniqueNumber, scheduledIntervalsState);
414
+ const createSetTimeout = createSetTimeoutFactory(generateUniqueNumber, scheduledTimeoutsState);
415
+ const wrap = createBroker({
416
+ clearInterval: ({ call }) => createClearInterval((timerId) => call('clear', { timerId, timerType: 'interval' })),
417
+ clearTimeout: ({ call }) => createClearTimeout((timerId) => call('clear', { timerId, timerType: 'timeout' })),
418
+ setInterval: ({ call }) => createSetInterval((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'interval' })),
419
+ setTimeout: ({ call }) => createSetTimeout((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'timeout' }))
420
+ });
449
421
  const load = (url) => {
450
422
  const worker = new Worker(url);
451
423
  return wrap(worker);
@@ -467,7 +439,7 @@
467
439
  };
468
440
 
469
441
  // This is the minified and stringified code of the worker-timers-worker package.
470
- const worker = `(()=>{var e={455:function(e,t){!function(e){"use strict";var t=function(e){return function(t){var r=e(t);return t.add(r),r}},r=function(e){return function(t,r){return e.set(t,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,s=2*o,a=function(e,t){return function(r){var a=t.get(r),i=void 0===a?r.size:a<s?a+1:0;if(!r.has(i))return e(r,i);if(r.size<o){for(;r.has(i);)i=Math.floor(Math.random()*s);return e(r,i)}if(r.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;r.has(i);)i=Math.floor(Math.random()*n);return e(r,i)}},i=new WeakMap,u=r(i),c=a(u,i),l=t(c);e.addUniqueNumber=l,e.generateUniqueNumber=c}(t)}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n].call(s.exports,s,s.exports,r),s.exports}(()=>{"use strict";const e=-32603,t=-32602,n=-32601,o=(e,t)=>Object.assign(new Error(e),{status:t}),s=t=>o('The handler of the method called "'.concat(t,'" returned an unexpected result.'),e),a=(t,r)=>async({data:{id:a,method:i,params:u}})=>{const c=r[i];try{if(void 0===c)throw(e=>o('The requested method called "'.concat(e,'" is not supported.'),n))(i);const r=void 0===u?c():c(u);if(void 0===r)throw(t=>o('The handler of the method called "'.concat(t,'" returned no required result.'),e))(i);const l=r instanceof Promise?await r:r;if(null===a){if(void 0!==l.result)throw s(i)}else{if(void 0===l.result)throw s(i);const{result:e,transferables:r=[]}=l;t.postMessage({id:a,result:e},r)}}catch(e){const{message:r,status:n=-32603}=e;t.postMessage({error:{code:n,message:r},id:a})}};var i=r(455);const u=new Map,c=(e,r,n)=>({...r,connect:({port:t})=>{t.start();const n=e(t,r),o=(0,i.generateUniqueNumber)(u);return u.set(o,()=>{n(),t.close(),u.delete(o)}),{result:o}},disconnect:({portId:e})=>{const r=u.get(e);if(void 0===r)throw(e=>o('The specified parameter called "portId" with the given value "'.concat(e,'" does not identify a port connected to this worker.'),t))(e);return r(),{result:null}},isSupported:async()=>{if(await new Promise(e=>{const t=new ArrayBuffer(0),{port1:r,port2:n}=new MessageChannel;r.onmessage=({data:t})=>e(null!==t),n.postMessage(t,[t])})){const e=n();return{result:e instanceof Promise?await e:e}}return{result:!1}}}),l=(e,t,r=()=>!0)=>{const n=c(l,t,r),o=a(e,n);return e.addEventListener("message",o),()=>e.removeEventListener("message",o)},d=(e,t)=>r=>{const n=t.get(r);if(void 0===n)return Promise.resolve(!1);const[o,s]=n;return e(o),t.delete(r),s(!1),Promise.resolve(!0)},f=(e,t,r,n)=>(o,s,a)=>{const i=o+s-t.timeOrigin,u=i-t.now();return new Promise(t=>{e.set(a,[r(n,u,i,e,t,a),t])})},m=new Map,h=d(globalThis.clearTimeout,m),p=new Map,v=d(globalThis.clearTimeout,p),w=((e,t)=>{const r=(n,o,s,a)=>{const i=n-e.now();i>0?o.set(a,[t(r,i,n,o,s,a),s]):(o.delete(a),s(!0))};return r})(performance,globalThis.setTimeout),g=f(m,performance,globalThis.setTimeout,w),T=f(p,performance,globalThis.setTimeout,w);l(self,{clear:async({timerId:e,timerType:t})=>({result:await("interval"===t?h(e):v(e))}),set:async({delay:e,now:t,timerId:r,timerType:n})=>({result:await("interval"===n?g:T)(e,t,r)})})})()})();`; // tslint:disable-line:max-line-length
442
+ const worker = `(()=>{var e={455(e,t){!function(e){"use strict";var t=function(e){return function(t){var r=e(t);return t.add(r),r}},r=function(e){return function(t,r){return e.set(t,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,s=2*o,a=function(e,t){return function(r){var a=t.get(r),i=void 0===a?r.size:a<s?a+1:0;if(!r.has(i))return e(r,i);if(r.size<o){for(;r.has(i);)i=Math.floor(Math.random()*s);return e(r,i)}if(r.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;r.has(i);)i=Math.floor(Math.random()*n);return e(r,i)}},i=new WeakMap,u=r(i),c=a(u,i),l=t(c);e.addUniqueNumber=l,e.generateUniqueNumber=c}(t)}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n].call(s.exports,s,s.exports,r),s.exports}(()=>{"use strict";const e=-32603,t=-32602,n=-32601,o=(e,t)=>Object.assign(new Error(e),{status:t}),s=t=>o('The handler of the method called "'.concat(t,'" returned an unexpected result.'),e),a=(t,r)=>async({data:{id:a,method:i,params:u}})=>{const c=r[i];try{if(void 0===c)throw(e=>o('The requested method called "'.concat(e,'" is not supported.'),n))(i);const r=void 0===u?c():c(u);if(void 0===r)throw(t=>o('The handler of the method called "'.concat(t,'" returned no required result.'),e))(i);const l=r instanceof Promise?await r:r;if(null===a){if(void 0!==l.result)throw s(i)}else{if(void 0===l.result)throw s(i);const{result:e,transferables:r=[]}=l;t.postMessage({id:a,result:e},r)}}catch(e){const{message:r,status:n=-32603}=e;t.postMessage({error:{code:n,message:r},id:a})}};var i=r(455);const u=new Map,c=(e,r,n)=>({...r,connect:({port:t})=>{t.start();const n=e(t,r),o=(0,i.generateUniqueNumber)(u);return u.set(o,()=>{n(),t.close(),u.delete(o)}),{result:o}},disconnect:({portId:e})=>{const r=u.get(e);if(void 0===r)throw(e=>o('The specified parameter called "portId" with the given value "'.concat(e,'" does not identify a port connected to this worker.'),t))(e);return r(),{result:null}},isSupported:async()=>{if(await new Promise(e=>{const t=new ArrayBuffer(0),{port1:r,port2:n}=new MessageChannel;r.onmessage=({data:t})=>e(null!==t),n.postMessage(t,[t])})){const e=n();return{result:e instanceof Promise?await e:e}}return{result:!1}}}),l=(e,t,r=()=>!0)=>{const n=c(l,t,r),o=a(e,n);return e.addEventListener("message",o),()=>e.removeEventListener("message",o)},d=(e,t)=>r=>{const n=t.get(r);if(void 0===n)return Promise.resolve(!1);const[o,s]=n;return e(o),t.delete(r),s(!1),Promise.resolve(!0)},m=(e,t,r,n)=>(o,s,a)=>{const i=o+s-t.timeOrigin,u=i-t.now();return new Promise(t=>{e.set(a,[r(n,u,i,e,t,a),t])})},f=new Map,h=d(globalThis.clearTimeout,f),p=new Map,v=d(globalThis.clearTimeout,p),w=((e,t)=>{const r=(n,o,s,a)=>{const i=n-e.now();i>0?o.set(a,[t(r,i,n,o,s,a),s]):(o.delete(a),s(!0))};return r})(performance,globalThis.setTimeout),g=m(f,performance,globalThis.setTimeout,w),T=m(p,performance,globalThis.setTimeout,w);l(self,{clear:async({timerId:e,timerType:t})=>({result:await("interval"===t?h(e):v(e))}),set:async({delay:e,now:t,timerId:r,timerType:n})=>({result:await("interval"===n?g:T)(e,t,r)})})})()})();`; // tslint:disable-line:max-line-length
471
443
 
472
444
  const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
473
445
  const clearInterval$1 = (timerId) => loadOrReturnBroker().clearInterval(timerId);
@@ -744,7 +716,7 @@
744
716
  return setTimeout$1(callback, timeout);
745
717
  }
746
718
  catch {
747
- return PopsCore.setTimeout(callback, timeout);
719
+ return setTimeout(callback, timeout);
748
720
  }
749
721
  }
750
722
  /**
@@ -760,7 +732,7 @@
760
732
  // TODO
761
733
  }
762
734
  finally {
763
- PopsCore.clearTimeout(timeId);
735
+ clearTimeout(timeId);
764
736
  }
765
737
  }
766
738
  /**
@@ -771,7 +743,7 @@
771
743
  return setInterval$1(callback, timeout);
772
744
  }
773
745
  catch {
774
- return PopsCore.setInterval(callback, timeout);
746
+ return setInterval(callback, timeout);
775
747
  }
776
748
  }
777
749
  /**
@@ -787,7 +759,7 @@
787
759
  // 忽略
788
760
  }
789
761
  finally {
790
- PopsCore.clearInterval(timeId);
762
+ clearInterval(timeId);
791
763
  }
792
764
  }
793
765
  /**
@@ -7415,9 +7387,7 @@
7415
7387
  * 取消绑定 显示事件
7416
7388
  */
7417
7389
  offShowEvent() {
7418
- popsDOMUtils.off(this.$data.config.$target, this.$data.config.onShowEventName, this.show, {
7419
- capture: true,
7420
- });
7390
+ popsDOMUtils.off(this.$data.config.$target, this.$data.config.onShowEventName, this.show, this.$data.config.eventOption);
7421
7391
  }
7422
7392
  /**
7423
7393
  * 关闭提示框
@@ -7476,9 +7446,7 @@
7476
7446
  * 取消绑定 关闭事件
7477
7447
  */
7478
7448
  offCloseEvent() {
7479
- popsDOMUtils.off(this.$data.config.$target, this.$data.config.onCloseEventName, this.close, {
7480
- capture: true,
7481
- });
7449
+ popsDOMUtils.off(this.$data.config.$target, this.$data.config.onCloseEventName, this.close, this.$data.config.eventOption);
7482
7450
  }
7483
7451
  /**
7484
7452
  * 销毁元素
@@ -13365,7 +13333,7 @@
13365
13333
  },
13366
13334
  };
13367
13335
 
13368
- const version = "3.1.1";
13336
+ const version = "3.1.3";
13369
13337
 
13370
13338
  class Pops {
13371
13339
  /** 配置 */