@whitesev/domutils 1.8.0 → 1.8.1

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
@@ -119,47 +119,7 @@
119
119
  const cache = createCache(LAST_NUMBER_WEAK_MAP);
120
120
  const generateUniqueNumber = createGenerateUniqueNumber(cache, LAST_NUMBER_WEAK_MAP);
121
121
 
122
- const isMessagePort = (sender) => {
123
- return typeof sender.start === 'function';
124
- };
125
-
126
- const PORT_MAP = new WeakMap();
127
-
128
- const extendBrokerImplementation = (partialBrokerImplementation) => ({
129
- ...partialBrokerImplementation,
130
- connect: ({ call }) => {
131
- return async () => {
132
- const { port1, port2 } = new MessageChannel();
133
- const portId = await call('connect', { port: port1 }, [port1]);
134
- PORT_MAP.set(port2, portId);
135
- return port2;
136
- };
137
- },
138
- disconnect: ({ call }) => {
139
- return async (port) => {
140
- const portId = PORT_MAP.get(port);
141
- if (portId === undefined) {
142
- throw new Error('The given port is not connected.');
143
- }
144
- await call('disconnect', { portId });
145
- };
146
- },
147
- isSupported: ({ call }) => {
148
- return () => call('isSupported');
149
- }
150
- });
151
-
152
- const ONGOING_REQUESTS = new WeakMap();
153
- const createOrGetOngoingRequests = (sender) => {
154
- if (ONGOING_REQUESTS.has(sender)) {
155
- // @todo TypeScript needs to be convinced that has() works as expected.
156
- return ONGOING_REQUESTS.get(sender);
157
- }
158
- const ongoingRequests = new Map();
159
- ONGOING_REQUESTS.set(sender, ongoingRequests);
160
- return ongoingRequests;
161
- };
162
- const createBroker = (brokerImplementation) => {
122
+ const createBrokerFactory = (createOrGetOngoingRequests, extendBrokerImplementation, generateUniqueNumber, isMessagePort) => (brokerImplementation) => {
163
123
  const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
164
124
  return (sender) => {
165
125
  const ongoingRequests = createOrGetOngoingRequests(sender);
@@ -202,82 +162,116 @@
202
162
  };
203
163
  };
204
164
 
205
- // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
206
- const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
207
- const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
208
- const wrap = createBroker({
209
- clearInterval: ({ call }) => {
210
- return (timerId) => {
211
- if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
212
- scheduledIntervalsState.set(timerId, null);
213
- call('clear', { timerId, timerType: 'interval' }).then(() => {
214
- scheduledIntervalsState.delete(timerId);
215
- });
216
- }
165
+ const createCreateOrGetOngoingRequests = (ongoingRequestsMap) => (sender) => {
166
+ if (ongoingRequestsMap.has(sender)) {
167
+ // @todo TypeScript needs to be convinced that has() works as expected.
168
+ return ongoingRequestsMap.get(sender);
169
+ }
170
+ const ongoingRequests = new Map();
171
+ ongoingRequestsMap.set(sender, ongoingRequests);
172
+ return ongoingRequests;
173
+ };
174
+
175
+ const createExtendBrokerImplementation = (portMap) => (partialBrokerImplementation) => ({
176
+ ...partialBrokerImplementation,
177
+ connect: ({ call }) => {
178
+ return async () => {
179
+ const { port1, port2 } = new MessageChannel();
180
+ const portId = await call('connect', { port: port1 }, [port1]);
181
+ portMap.set(port2, portId);
182
+ return port2;
217
183
  };
218
184
  },
219
- clearTimeout: ({ call }) => {
220
- return (timerId) => {
221
- if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
222
- scheduledTimeoutsState.set(timerId, null);
223
- call('clear', { timerId, timerType: 'timeout' }).then(() => {
224
- scheduledTimeoutsState.delete(timerId);
225
- });
185
+ disconnect: ({ call }) => {
186
+ return async (port) => {
187
+ const portId = portMap.get(port);
188
+ if (portId === undefined) {
189
+ throw new Error('The given port is not connected.');
226
190
  }
191
+ await call('disconnect', { portId });
227
192
  };
228
193
  },
229
- setInterval: ({ call }) => {
230
- return (func, delay = 0, ...args) => {
231
- const symbol = Symbol();
232
- const timerId = generateUniqueNumber(scheduledIntervalsState);
233
- scheduledIntervalsState.set(timerId, symbol);
234
- const schedule = () => call('set', {
235
- delay,
236
- now: performance.timeOrigin + performance.now(),
237
- timerId,
238
- timerType: 'interval'
239
- }).then(() => {
240
- const state = scheduledIntervalsState.get(timerId);
241
- if (state === undefined) {
242
- throw new Error('The timer is in an undefined state.');
243
- }
244
- if (state === symbol) {
245
- func(...args);
246
- // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
247
- if (scheduledIntervalsState.get(timerId) === symbol) {
248
- schedule();
249
- }
250
- }
251
- });
252
- schedule();
253
- return timerId;
254
- };
255
- },
256
- setTimeout: ({ call }) => {
257
- return (func, delay = 0, ...args) => {
258
- const symbol = Symbol();
259
- const timerId = generateUniqueNumber(scheduledTimeoutsState);
260
- scheduledTimeoutsState.set(timerId, symbol);
261
- call('set', {
262
- delay,
263
- now: performance.timeOrigin + performance.now(),
264
- timerId,
265
- timerType: 'timeout'
266
- }).then(() => {
267
- const state = scheduledTimeoutsState.get(timerId);
268
- if (state === undefined) {
269
- throw new Error('The timer is in an undefined state.');
270
- }
271
- if (state === symbol) {
272
- // A timeout can be savely deleted because it is only called once.
273
- scheduledTimeoutsState.delete(timerId);
274
- func(...args);
275
- }
276
- });
277
- return timerId;
278
- };
194
+ isSupported: ({ call }) => {
195
+ return () => call('isSupported');
279
196
  }
280
197
  });
198
+
199
+ const isMessagePort = (sender) => {
200
+ return typeof sender.start === 'function';
201
+ };
202
+
203
+ const createBroker = createBrokerFactory(createCreateOrGetOngoingRequests(new WeakMap()), createExtendBrokerImplementation(new WeakMap()), generateUniqueNumber, isMessagePort);
204
+
205
+ const createClearIntervalFactory = (scheduledIntervalsState) => (clear) => (timerId) => {
206
+ if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
207
+ scheduledIntervalsState.set(timerId, null);
208
+ clear(timerId).then(() => {
209
+ scheduledIntervalsState.delete(timerId);
210
+ });
211
+ }
212
+ };
213
+
214
+ const createClearTimeoutFactory = (scheduledTimeoutsState) => (clear) => (timerId) => {
215
+ if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
216
+ scheduledTimeoutsState.set(timerId, null);
217
+ clear(timerId).then(() => {
218
+ scheduledTimeoutsState.delete(timerId);
219
+ });
220
+ }
221
+ };
222
+
223
+ const createSetIntervalFactory = (generateUniqueNumber, scheduledIntervalsState) => (set) => (func, delay = 0, ...args) => {
224
+ const symbol = Symbol();
225
+ const timerId = generateUniqueNumber(scheduledIntervalsState);
226
+ scheduledIntervalsState.set(timerId, symbol);
227
+ const schedule = () => set(delay, timerId).then(() => {
228
+ const state = scheduledIntervalsState.get(timerId);
229
+ if (state === undefined) {
230
+ throw new Error('The timer is in an undefined state.');
231
+ }
232
+ if (state === symbol) {
233
+ func(...args);
234
+ // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
235
+ if (scheduledIntervalsState.get(timerId) === symbol) {
236
+ schedule();
237
+ }
238
+ }
239
+ });
240
+ schedule();
241
+ return timerId;
242
+ };
243
+
244
+ const createSetTimeoutFactory = (generateUniqueNumber, scheduledTimeoutsState) => (set) => (func, delay = 0, ...args) => {
245
+ const symbol = Symbol();
246
+ const timerId = generateUniqueNumber(scheduledTimeoutsState);
247
+ scheduledTimeoutsState.set(timerId, symbol);
248
+ set(delay, timerId).then(() => {
249
+ const state = scheduledTimeoutsState.get(timerId);
250
+ if (state === undefined) {
251
+ throw new Error('The timer is in an undefined state.');
252
+ }
253
+ if (state === symbol) {
254
+ // A timeout can be savely deleted because it is only called once.
255
+ scheduledTimeoutsState.delete(timerId);
256
+ func(...args);
257
+ }
258
+ });
259
+ return timerId;
260
+ };
261
+
262
+ // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
263
+ const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
264
+ const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
265
+ const createClearInterval = createClearIntervalFactory(scheduledIntervalsState);
266
+ const createClearTimeout = createClearTimeoutFactory(scheduledTimeoutsState);
267
+ const createSetInterval = createSetIntervalFactory(generateUniqueNumber, scheduledIntervalsState);
268
+ const createSetTimeout = createSetTimeoutFactory(generateUniqueNumber, scheduledTimeoutsState);
269
+ const wrap = createBroker({
270
+ clearInterval: ({ call }) => createClearInterval((timerId) => call('clear', { timerId, timerType: 'interval' })),
271
+ clearTimeout: ({ call }) => createClearTimeout((timerId) => call('clear', { timerId, timerType: 'timeout' })),
272
+ setInterval: ({ call }) => createSetInterval((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'interval' })),
273
+ setTimeout: ({ call }) => createSetTimeout((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'timeout' }))
274
+ });
281
275
  const load = (url) => {
282
276
  const worker = new Worker(url);
283
277
  return wrap(worker);
@@ -299,7 +293,7 @@
299
293
  };
300
294
 
301
295
  // This is the minified and stringified code of the worker-timers-worker package.
302
- 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
296
+ 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
303
297
 
304
298
  const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
305
299
  const clearInterval$1 = (timerId) => loadOrReturnBroker().clearInterval(timerId);
@@ -527,7 +521,7 @@
527
521
  },
528
522
  };
529
523
 
530
- const version = "1.8.0";
524
+ const version = "1.8.1";
531
525
 
532
526
  class ElementSelector {
533
527
  windowApi;
@@ -2621,14 +2615,18 @@
2621
2615
  let timer = void 0;
2622
2616
  /** 是否是移动端点击 */
2623
2617
  let isMobileTouch = false;
2624
- const dblclick_handler = async (evt) => {
2618
+ const dblclick_handler = async (evt, option) => {
2625
2619
  if (evt.type === "dblclick" && isMobileTouch) {
2626
2620
  // 禁止在移动端触发dblclick事件
2627
2621
  return;
2628
2622
  }
2629
- await handler(evt);
2623
+ await handler(evt, option);
2630
2624
  };
2631
- const dblClickListener = this.on($el, "dblclick", dblclick_handler, options);
2625
+ const dblClickListener = this.on($el, "dblclick", (evt) => {
2626
+ dblclick_handler(evt, {
2627
+ isDoubleClick: true,
2628
+ });
2629
+ }, options);
2632
2630
  const touchEndListener = this.on($el, "touchend", selector, (evt, selectorTarget) => {
2633
2631
  isMobileTouch = true;
2634
2632
  CommonUtils.clearTimeout(timer);
@@ -2637,12 +2635,17 @@
2637
2635
  isDoubleClick = false;
2638
2636
  $click = null;
2639
2637
  /* 判定为双击 */
2640
- dblclick_handler(evt);
2638
+ dblclick_handler(evt, {
2639
+ isDoubleClick: true,
2640
+ });
2641
2641
  }
2642
2642
  else {
2643
2643
  timer = CommonUtils.setTimeout(() => {
2644
2644
  isDoubleClick = false;
2645
2645
  // 判断为单击
2646
+ dblclick_handler(evt, {
2647
+ isDoubleClick: false,
2648
+ });
2646
2649
  }, 200);
2647
2650
  isDoubleClick = true;
2648
2651
  $click = selectorTarget;