@whitesev/domutils 1.8.0 → 1.8.2

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