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