@whitesev/pops 3.2.2 → 3.3.0

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.cjs.js CHANGED
@@ -177,272 +177,55 @@ const PopsIcon = {
177
177
  */
178
178
  const SymbolEvents = Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1));
179
179
 
180
- const PopsCoreDefaultEnv = {
180
+ const OriginPrototype = {
181
+ Object: {
182
+ defineProperty: Object.defineProperty,
183
+ },
184
+ };
185
+ const PopsCoreDefaultApi = {
181
186
  document: document,
182
187
  window: window,
183
188
  globalThis: globalThis,
184
189
  self: self,
190
+ setTimeout: globalThis.setTimeout.bind(globalThis),
191
+ setInterval: globalThis.setInterval.bind(globalThis),
192
+ clearTimeout: globalThis.clearTimeout.bind(globalThis),
193
+ clearInterval: globalThis.clearInterval.bind(globalThis),
185
194
  };
186
- const PopsCoreEnv = Object.assign({}, PopsCoreDefaultEnv);
195
+ const PopsCoreApi = Object.assign({}, PopsCoreDefaultApi);
187
196
  const PopsCore = {
197
+ init(option) {
198
+ if (!option) {
199
+ option = Object.assign({}, PopsCoreDefaultApi);
200
+ }
201
+ Object.assign(PopsCoreApi, option);
202
+ },
188
203
  get document() {
189
- return PopsCoreEnv.document;
204
+ return PopsCoreApi.document;
190
205
  },
191
206
  get window() {
192
- return PopsCoreEnv.window;
207
+ return PopsCoreApi.window;
193
208
  },
194
209
  get globalThis() {
195
- return PopsCoreEnv.globalThis;
210
+ return PopsCoreApi.globalThis;
196
211
  },
197
212
  get self() {
198
- return PopsCoreEnv.self;
213
+ return PopsCoreApi.self;
199
214
  },
200
- };
201
- const OriginPrototype = {
202
- Object: {
203
- defineProperty: Object.defineProperty,
215
+ get setTimeout() {
216
+ return PopsCoreApi.setTimeout;
204
217
  },
205
- };
206
-
207
- const createCache = (lastNumberWeakMap) => {
208
- return (collection, nextNumber) => {
209
- lastNumberWeakMap.set(collection, nextNumber);
210
- return nextNumber;
211
- };
212
- };
213
-
214
- /*
215
- * The value of the constant Number.MAX_SAFE_INTEGER equals (2 ** 53 - 1) but it
216
- * is fairly new.
217
- */
218
- const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER === undefined ? 9007199254740991 : Number.MAX_SAFE_INTEGER;
219
- const TWO_TO_THE_POWER_OF_TWENTY_NINE = 536870912;
220
- const TWO_TO_THE_POWER_OF_THIRTY = TWO_TO_THE_POWER_OF_TWENTY_NINE * 2;
221
- const createGenerateUniqueNumber = (cache, lastNumberWeakMap) => {
222
- return (collection) => {
223
- const lastNumber = lastNumberWeakMap.get(collection);
224
- /*
225
- * Let's try the cheapest algorithm first. It might fail to produce a new
226
- * number, but it is so cheap that it is okay to take the risk. Just
227
- * increase the last number by one or reset it to 0 if we reached the upper
228
- * bound of SMIs (which stands for small integers). When the last number is
229
- * unknown it is assumed that the collection contains zero based consecutive
230
- * numbers.
231
- */
232
- let nextNumber = lastNumber === undefined ? collection.size : lastNumber < TWO_TO_THE_POWER_OF_THIRTY ? lastNumber + 1 : 0;
233
- if (!collection.has(nextNumber)) {
234
- return cache(collection, nextNumber);
235
- }
236
- /*
237
- * If there are less than half of 2 ** 30 numbers stored in the collection,
238
- * the chance to generate a new random number in the range from 0 to 2 ** 30
239
- * is at least 50%. It's benifitial to use only SMIs because they perform
240
- * much better in any environment based on V8.
241
- */
242
- if (collection.size < TWO_TO_THE_POWER_OF_TWENTY_NINE) {
243
- while (collection.has(nextNumber)) {
244
- nextNumber = Math.floor(Math.random() * TWO_TO_THE_POWER_OF_THIRTY);
245
- }
246
- return cache(collection, nextNumber);
247
- }
248
- // Quickly check if there is a theoretical chance to generate a new number.
249
- if (collection.size > MAX_SAFE_INTEGER) {
250
- throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!');
251
- }
252
- // Otherwise use the full scale of safely usable integers.
253
- while (collection.has(nextNumber)) {
254
- nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER);
255
- }
256
- return cache(collection, nextNumber);
257
- };
258
- };
259
-
260
- const LAST_NUMBER_WEAK_MAP = new WeakMap();
261
- const cache = createCache(LAST_NUMBER_WEAK_MAP);
262
- const generateUniqueNumber = createGenerateUniqueNumber(cache, LAST_NUMBER_WEAK_MAP);
263
-
264
- const createBrokerFactory = (createOrGetOngoingRequests, extendBrokerImplementation, generateUniqueNumber, isMessagePort) => (brokerImplementation) => {
265
- const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
266
- return (sender) => {
267
- const ongoingRequests = createOrGetOngoingRequests(sender);
268
- sender.addEventListener('message', (({ data: message }) => {
269
- const { id } = message;
270
- if (id !== null && ongoingRequests.has(id)) {
271
- const { reject, resolve } = ongoingRequests.get(id);
272
- ongoingRequests.delete(id);
273
- if (message.error === undefined) {
274
- resolve(message.result);
275
- }
276
- else {
277
- reject(new Error(message.error.message));
278
- }
279
- }
280
- }));
281
- if (isMessagePort(sender)) {
282
- sender.start();
283
- }
284
- const call = (method, params = null, transferables = []) => {
285
- return new Promise((resolve, reject) => {
286
- const id = generateUniqueNumber(ongoingRequests);
287
- ongoingRequests.set(id, { reject, resolve });
288
- if (params === null) {
289
- sender.postMessage({ id, method }, transferables);
290
- }
291
- else {
292
- sender.postMessage({ id, method, params }, transferables);
293
- }
294
- });
295
- };
296
- const notify = (method, params, transferables = []) => {
297
- sender.postMessage({ id: null, method, params }, transferables);
298
- };
299
- let functions = {};
300
- for (const [key, handler] of Object.entries(fullBrokerImplementation)) {
301
- functions = { ...functions, [key]: handler({ call, notify }) };
302
- }
303
- return { ...functions };
304
- };
305
- };
306
-
307
- const createCreateOrGetOngoingRequests = (ongoingRequestsMap) => (sender) => {
308
- if (ongoingRequestsMap.has(sender)) {
309
- // @todo TypeScript needs to be convinced that has() works as expected.
310
- return ongoingRequestsMap.get(sender);
311
- }
312
- const ongoingRequests = new Map();
313
- ongoingRequestsMap.set(sender, ongoingRequests);
314
- return ongoingRequests;
315
- };
316
-
317
- const createExtendBrokerImplementation = (portMap) => (partialBrokerImplementation) => ({
318
- ...partialBrokerImplementation,
319
- connect: ({ call }) => {
320
- return async () => {
321
- const { port1, port2 } = new MessageChannel();
322
- const portId = await call('connect', { port: port1 }, [port1]);
323
- portMap.set(port2, portId);
324
- return port2;
325
- };
218
+ get setInterval() {
219
+ return PopsCoreApi.setInterval;
326
220
  },
327
- disconnect: ({ call }) => {
328
- return async (port) => {
329
- const portId = portMap.get(port);
330
- if (portId === undefined) {
331
- throw new Error('The given port is not connected.');
332
- }
333
- await call('disconnect', { portId });
334
- };
221
+ get clearTimeout() {
222
+ return PopsCoreApi.clearTimeout;
223
+ },
224
+ get clearInterval() {
225
+ return PopsCoreApi.clearInterval;
335
226
  },
336
- isSupported: ({ call }) => {
337
- return () => call('isSupported');
338
- }
339
- });
340
-
341
- const isMessagePort = (sender) => {
342
- return typeof sender.start === 'function';
343
- };
344
-
345
- const createBroker = createBrokerFactory(createCreateOrGetOngoingRequests(new WeakMap()), createExtendBrokerImplementation(new WeakMap()), generateUniqueNumber, isMessagePort);
346
-
347
- const createClearIntervalFactory = (scheduledIntervalsState) => (clear) => (timerId) => {
348
- if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
349
- scheduledIntervalsState.set(timerId, null);
350
- clear(timerId).then(() => {
351
- scheduledIntervalsState.delete(timerId);
352
- });
353
- }
354
- };
355
-
356
- const createClearTimeoutFactory = (scheduledTimeoutsState) => (clear) => (timerId) => {
357
- if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
358
- scheduledTimeoutsState.set(timerId, null);
359
- clear(timerId).then(() => {
360
- scheduledTimeoutsState.delete(timerId);
361
- });
362
- }
363
- };
364
-
365
- const createSetIntervalFactory = (generateUniqueNumber, scheduledIntervalsState) => (set) => (func, delay = 0, ...args) => {
366
- const symbol = Symbol();
367
- const timerId = generateUniqueNumber(scheduledIntervalsState);
368
- scheduledIntervalsState.set(timerId, symbol);
369
- const schedule = () => set(delay, timerId).then(() => {
370
- const state = scheduledIntervalsState.get(timerId);
371
- if (state === undefined) {
372
- throw new Error('The timer is in an undefined state.');
373
- }
374
- if (state === symbol) {
375
- func(...args);
376
- // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
377
- if (scheduledIntervalsState.get(timerId) === symbol) {
378
- schedule();
379
- }
380
- }
381
- });
382
- schedule();
383
- return timerId;
384
- };
385
-
386
- const createSetTimeoutFactory = (generateUniqueNumber, scheduledTimeoutsState) => (set) => (func, delay = 0, ...args) => {
387
- const symbol = Symbol();
388
- const timerId = generateUniqueNumber(scheduledTimeoutsState);
389
- scheduledTimeoutsState.set(timerId, symbol);
390
- set(delay, timerId).then(() => {
391
- const state = scheduledTimeoutsState.get(timerId);
392
- if (state === undefined) {
393
- throw new Error('The timer is in an undefined state.');
394
- }
395
- if (state === symbol) {
396
- // A timeout can be savely deleted because it is only called once.
397
- scheduledTimeoutsState.delete(timerId);
398
- func(...args);
399
- }
400
- });
401
- return timerId;
402
- };
403
-
404
- // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
405
- const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
406
- const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
407
- const createClearInterval = createClearIntervalFactory(scheduledIntervalsState);
408
- const createClearTimeout = createClearTimeoutFactory(scheduledTimeoutsState);
409
- const createSetInterval = createSetIntervalFactory(generateUniqueNumber, scheduledIntervalsState);
410
- const createSetTimeout = createSetTimeoutFactory(generateUniqueNumber, scheduledTimeoutsState);
411
- const wrap = createBroker({
412
- clearInterval: ({ call }) => createClearInterval((timerId) => call('clear', { timerId, timerType: 'interval' })),
413
- clearTimeout: ({ call }) => createClearTimeout((timerId) => call('clear', { timerId, timerType: 'timeout' })),
414
- setInterval: ({ call }) => createSetInterval((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'interval' })),
415
- setTimeout: ({ call }) => createSetTimeout((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'timeout' }))
416
- });
417
- const load = (url) => {
418
- const worker = new Worker(url);
419
- return wrap(worker);
420
- };
421
-
422
- const createLoadOrReturnBroker = (loadBroker, worker) => {
423
- let broker = null;
424
- return () => {
425
- if (broker !== null) {
426
- return broker;
427
- }
428
- const blob = new Blob([worker], { type: 'application/javascript; charset=utf-8' });
429
- const url = URL.createObjectURL(blob);
430
- broker = loadBroker(url);
431
- // Bug #1: Edge up until v18 didn't like the URL to be revoked directly.
432
- setTimeout(() => URL.revokeObjectURL(url));
433
- return broker;
434
- };
435
227
  };
436
228
 
437
- // This is the minified and stringified code of the worker-timers-worker package.
438
- 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
439
-
440
- const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
441
- const clearInterval$1 = (timerId) => loadOrReturnBroker().clearInterval(timerId);
442
- const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
443
- const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
444
- const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
445
-
446
229
  let t$1 = class t{constructor(){this.__map={};}beforeEach(t){this.__interceptor=t;}on(t,i){const s=Array.isArray(t)?t:[t];for(const t of s){this.__map[t]=this.__map[t]||[];const s=this.__map[t];s&&s.push(i);}return this}emit(t,i,s){ void 0!==this.__interceptor?this.__interceptor(t,(()=>{this.__emit(t,i),s&&s();})):(this.__emit(t,i),s&&s());}__emit(t,i){const s=this.__map[t];if(Array.isArray(s)&&(null==s?void 0:s.length))for(const _ of s)_(i,t);this.event=i;}off(t,i){const s=this.__map[t];if(void 0!==s)if(void 0===i)delete this.__map[t];else {const t=s.findIndex((t=>t===i));s.splice(t,1);}}destroy(){this.__map={};}};
447
230
 
448
231
  const n$1="clientX",e$2="clientY",t=16,c$3="start",o$1="move",s$1="cancel",u$3="end",a$2="left",i$3="right",r$4="up",d$1="down",m$2={4:"start",5:"move",1:"end",3:"cancel"};function v$1(n){return m$2[n]}function b(n,e,t){const c={1:{0:{move:4},4:{move:5,end:1,cancel:3},5:{move:5,end:1,cancel:3}},0:{4:{move:2,end:1,cancel:3},5:{start:2,move:2,end:1,cancel:3}}}[Number(n)][e];return void 0!==c&&c[t]||0}function g$1(n){[1,3,2].includes(n.state)&&(n.state=0);}function h$3(n){return [5,1,3].includes(n)}function j(n){if(n.disabled)return n.state=0,true}function O(n,e){return Object.assign(Object.assign(Object.assign({},n),e),{state:0,disabled:false})}function p$3(n){return Math.round(100*n)/100}
@@ -708,55 +491,25 @@ class PopsUtils {
708
491
  * 自动使用 Worker 执行 setTimeout
709
492
  */
710
493
  setTimeout(callback, timeout = 0) {
711
- try {
712
- return setTimeout$1(callback, timeout);
713
- }
714
- catch {
715
- return setTimeout(callback, timeout);
716
- }
494
+ return PopsCore.setTimeout(callback, timeout);
717
495
  }
718
496
  /**
719
497
  * 配合 .setTimeout 使用
720
498
  */
721
499
  clearTimeout(timeId) {
722
- try {
723
- if (timeId != null) {
724
- clearTimeout$1(timeId);
725
- }
726
- }
727
- catch {
728
- // TODO
729
- }
730
- finally {
731
- clearTimeout(timeId);
732
- }
500
+ return PopsCore.clearTimeout(timeId);
733
501
  }
734
502
  /**
735
503
  * 自动使用 Worker 执行 setInterval
736
504
  */
737
505
  setInterval(callback, timeout = 0) {
738
- try {
739
- return setInterval$1(callback, timeout);
740
- }
741
- catch {
742
- return setInterval(callback, timeout);
743
- }
506
+ return PopsCore.setInterval(callback, timeout);
744
507
  }
745
508
  /**
746
509
  * 配合 .setInterval 使用
747
510
  */
748
511
  clearInterval(timeId) {
749
- try {
750
- if (timeId != null) {
751
- clearInterval$1(timeId);
752
- }
753
- }
754
- catch {
755
- // 忽略
756
- }
757
- finally {
758
- clearInterval(timeId);
759
- }
512
+ return PopsCore.clearInterval(timeId);
760
513
  }
761
514
  /**
762
515
  * 覆盖对象中的数组新值
@@ -13461,7 +13214,7 @@ const PopsSearchSuggestion = {
13461
13214
  },
13462
13215
  };
13463
13216
 
13464
- const version = "3.2.2";
13217
+ const version = "3.3.0";
13465
13218
 
13466
13219
  class Pops {
13467
13220
  /** 配置 */