@vueuse/shared 8.8.0 → 8.9.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/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { shallowRef, watchEffect, readonly, ref, watch, customRef, effectScope, provide, inject, getCurrentScope, onScopeDispose, isVue3, isRef, unref, computed, reactive, toRefs as toRefs$1, toRef, isVue2, set as set$1, getCurrentInstance, onBeforeMount, nextTick, onBeforeUnmount, onMounted, onUnmounted, isReactive } from 'vue-demi';
1
+ import { shallowRef, watchEffect, readonly, ref, unref, isVue3, watch, customRef, effectScope, provide, inject, getCurrentScope, onScopeDispose, isRef, computed, reactive, toRefs as toRefs$1, toRef, isVue2, set as set$1, getCurrentInstance, onBeforeMount, nextTick, onBeforeUnmount, onMounted, onUnmounted, isReactive } from 'vue-demi';
2
2
 
3
3
  var __defProp$9 = Object.defineProperty;
4
4
  var __defProps$6 = Object.defineProperties;
@@ -30,31 +30,219 @@ function computedEager(fn, options) {
30
30
  return readonly(result);
31
31
  }
32
32
 
33
+ var _a;
34
+ const isClient = typeof window !== "undefined";
35
+ const isDef = (val) => typeof val !== "undefined";
36
+ const assert = (condition, ...infos) => {
37
+ if (!condition)
38
+ console.warn(...infos);
39
+ };
40
+ const toString = Object.prototype.toString;
41
+ const isBoolean = (val) => typeof val === "boolean";
42
+ const isFunction = (val) => typeof val === "function";
43
+ const isNumber = (val) => typeof val === "number";
44
+ const isString = (val) => typeof val === "string";
45
+ const isObject = (val) => toString.call(val) === "[object Object]";
46
+ const isWindow = (val) => typeof window !== "undefined" && toString.call(val) === "[object Window]";
47
+ const now = () => Date.now();
48
+ const timestamp = () => +Date.now();
49
+ const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
50
+ const noop = () => {
51
+ };
52
+ const rand = (min, max) => {
53
+ min = Math.ceil(min);
54
+ max = Math.floor(max);
55
+ return Math.floor(Math.random() * (max - min + 1)) + min;
56
+ };
57
+ const isIOS = isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
58
+
59
+ function createFilterWrapper(filter, fn) {
60
+ function wrapper(...args) {
61
+ filter(() => fn.apply(this, args), { fn, thisArg: this, args });
62
+ }
63
+ return wrapper;
64
+ }
65
+ const bypassFilter = (invoke) => {
66
+ return invoke();
67
+ };
68
+ function debounceFilter(ms, options = {}) {
69
+ let timer;
70
+ let maxTimer;
71
+ const filter = (invoke) => {
72
+ const duration = unref(ms);
73
+ const maxDuration = unref(options.maxWait);
74
+ if (timer)
75
+ clearTimeout(timer);
76
+ if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) {
77
+ if (maxTimer) {
78
+ clearTimeout(maxTimer);
79
+ maxTimer = null;
80
+ }
81
+ return invoke();
82
+ }
83
+ if (maxDuration && !maxTimer) {
84
+ maxTimer = setTimeout(() => {
85
+ if (timer)
86
+ clearTimeout(timer);
87
+ maxTimer = null;
88
+ invoke();
89
+ }, maxDuration);
90
+ }
91
+ timer = setTimeout(() => {
92
+ if (maxTimer)
93
+ clearTimeout(maxTimer);
94
+ maxTimer = null;
95
+ invoke();
96
+ }, duration);
97
+ };
98
+ return filter;
99
+ }
100
+ function throttleFilter(ms, trailing = true, leading = true) {
101
+ let lastExec = 0;
102
+ let timer;
103
+ let isLeading = true;
104
+ const clear = () => {
105
+ if (timer) {
106
+ clearTimeout(timer);
107
+ timer = void 0;
108
+ }
109
+ };
110
+ const filter = (invoke) => {
111
+ const duration = unref(ms);
112
+ const elapsed = Date.now() - lastExec;
113
+ clear();
114
+ if (duration <= 0) {
115
+ lastExec = Date.now();
116
+ return invoke();
117
+ }
118
+ if (elapsed > duration && (leading || !isLeading)) {
119
+ lastExec = Date.now();
120
+ invoke();
121
+ } else if (trailing) {
122
+ timer = setTimeout(() => {
123
+ lastExec = Date.now();
124
+ isLeading = true;
125
+ clear();
126
+ invoke();
127
+ }, duration);
128
+ }
129
+ if (!leading && !timer)
130
+ timer = setTimeout(() => isLeading = true, duration);
131
+ isLeading = false;
132
+ };
133
+ return filter;
134
+ }
135
+ function pausableFilter(extendFilter = bypassFilter) {
136
+ const isActive = ref(true);
137
+ function pause() {
138
+ isActive.value = false;
139
+ }
140
+ function resume() {
141
+ isActive.value = true;
142
+ }
143
+ const eventFilter = (...args) => {
144
+ if (isActive.value)
145
+ extendFilter(...args);
146
+ };
147
+ return { isActive, pause, resume, eventFilter };
148
+ }
149
+
150
+ function __onlyVue3(name = "this function") {
151
+ if (isVue3)
152
+ return;
153
+ throw new Error(`[VueUse] ${name} is only works on Vue 3.`);
154
+ }
155
+ const directiveHooks = {
156
+ mounted: isVue3 ? "mounted" : "inserted",
157
+ updated: isVue3 ? "updated" : "componentUpdated",
158
+ unmounted: isVue3 ? "unmounted" : "unbind"
159
+ };
160
+
161
+ function promiseTimeout(ms, throwOnTimeout = false, reason = "Timeout") {
162
+ return new Promise((resolve, reject) => {
163
+ if (throwOnTimeout)
164
+ setTimeout(() => reject(reason), ms);
165
+ else
166
+ setTimeout(resolve, ms);
167
+ });
168
+ }
169
+ function identity(arg) {
170
+ return arg;
171
+ }
172
+ function createSingletonPromise(fn) {
173
+ let _promise;
174
+ function wrapper() {
175
+ if (!_promise)
176
+ _promise = fn();
177
+ return _promise;
178
+ }
179
+ wrapper.reset = async () => {
180
+ const _prev = _promise;
181
+ _promise = void 0;
182
+ if (_prev)
183
+ await _prev;
184
+ };
185
+ return wrapper;
186
+ }
187
+ function invoke(fn) {
188
+ return fn();
189
+ }
190
+ function containsProp(obj, ...props) {
191
+ return props.some((k) => k in obj);
192
+ }
193
+ function increaseWithUnit(target, delta) {
194
+ var _a;
195
+ if (typeof target === "number")
196
+ return target + delta;
197
+ const value = ((_a = target.match(/^-?[0-9]+\.?[0-9]*/)) == null ? void 0 : _a[0]) || "";
198
+ const unit = target.slice(value.length);
199
+ const result = parseFloat(value) + delta;
200
+ if (Number.isNaN(result))
201
+ return target;
202
+ return result + unit;
203
+ }
204
+ function objectPick(obj, keys, omitUndefined = false) {
205
+ return keys.reduce((n, k) => {
206
+ if (k in obj) {
207
+ if (!omitUndefined || obj[k] !== void 0)
208
+ n[k] = obj[k];
209
+ }
210
+ return n;
211
+ }, {});
212
+ }
213
+
33
214
  function computedWithControl(source, fn) {
34
215
  let v = void 0;
35
216
  let track;
36
217
  let trigger;
37
218
  const dirty = ref(true);
38
- watch(source, () => {
219
+ const update = () => {
39
220
  dirty.value = true;
40
221
  trigger();
41
- }, { flush: "sync" });
42
- return customRef((_track, _trigger) => {
222
+ };
223
+ watch(source, update, { flush: "sync" });
224
+ const get = isFunction(fn) ? fn : fn.get;
225
+ const set = isFunction(fn) ? void 0 : fn.set;
226
+ const result = customRef((_track, _trigger) => {
43
227
  track = _track;
44
228
  trigger = _trigger;
45
229
  return {
46
230
  get() {
47
231
  if (dirty.value) {
48
- v = fn();
232
+ v = get();
49
233
  dirty.value = false;
50
234
  }
51
235
  track();
52
236
  return v;
53
237
  },
54
- set() {
238
+ set(v2) {
239
+ set == null ? void 0 : set(v2);
55
240
  }
56
241
  };
57
242
  });
243
+ if (Object.isExtensible(result))
244
+ result.trigger = update;
245
+ return result;
58
246
  }
59
247
 
60
248
  function createEventHook() {
@@ -133,17 +321,6 @@ function createSharedComposable(composable) {
133
321
  };
134
322
  }
135
323
 
136
- function __onlyVue3(name = "this function") {
137
- if (isVue3)
138
- return;
139
- throw new Error(`[VueUse] ${name} is only works on Vue 3.`);
140
- }
141
- const directiveHooks = {
142
- mounted: isVue3 ? "mounted" : "inserted",
143
- updated: isVue3 ? "updated" : "componentUpdated",
144
- unmounted: isVue3 ? "unmounted" : "unbind"
145
- };
146
-
147
324
  function extendRef(ref, extend, { enumerable = false, unwrap = true } = {}) {
148
325
  __onlyVue3();
149
326
  for (const [key, value] of Object.entries(extend)) {
@@ -323,176 +500,6 @@ function refAutoReset(defaultValue, afterMs = 1e4) {
323
500
  });
324
501
  }
325
502
 
326
- var _a;
327
- const isClient = typeof window !== "undefined";
328
- const isDef = (val) => typeof val !== "undefined";
329
- const assert = (condition, ...infos) => {
330
- if (!condition)
331
- console.warn(...infos);
332
- };
333
- const toString = Object.prototype.toString;
334
- const isBoolean = (val) => typeof val === "boolean";
335
- const isFunction = (val) => typeof val === "function";
336
- const isNumber = (val) => typeof val === "number";
337
- const isString = (val) => typeof val === "string";
338
- const isObject = (val) => toString.call(val) === "[object Object]";
339
- const isWindow = (val) => typeof window !== "undefined" && toString.call(val) === "[object Window]";
340
- const now = () => Date.now();
341
- const timestamp = () => +Date.now();
342
- const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
343
- const noop = () => {
344
- };
345
- const rand = (min, max) => {
346
- min = Math.ceil(min);
347
- max = Math.floor(max);
348
- return Math.floor(Math.random() * (max - min + 1)) + min;
349
- };
350
- const isIOS = isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
351
-
352
- function createFilterWrapper(filter, fn) {
353
- function wrapper(...args) {
354
- filter(() => fn.apply(this, args), { fn, thisArg: this, args });
355
- }
356
- return wrapper;
357
- }
358
- const bypassFilter = (invoke) => {
359
- return invoke();
360
- };
361
- function debounceFilter(ms, options = {}) {
362
- let timer;
363
- let maxTimer;
364
- const filter = (invoke) => {
365
- const duration = unref(ms);
366
- const maxDuration = unref(options.maxWait);
367
- if (timer)
368
- clearTimeout(timer);
369
- if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) {
370
- if (maxTimer) {
371
- clearTimeout(maxTimer);
372
- maxTimer = null;
373
- }
374
- return invoke();
375
- }
376
- if (maxDuration && !maxTimer) {
377
- maxTimer = setTimeout(() => {
378
- if (timer)
379
- clearTimeout(timer);
380
- maxTimer = null;
381
- invoke();
382
- }, maxDuration);
383
- }
384
- timer = setTimeout(() => {
385
- if (maxTimer)
386
- clearTimeout(maxTimer);
387
- maxTimer = null;
388
- invoke();
389
- }, duration);
390
- };
391
- return filter;
392
- }
393
- function throttleFilter(ms, trailing = true, leading = true) {
394
- let lastExec = 0;
395
- let timer;
396
- let isLeading = true;
397
- const clear = () => {
398
- if (timer) {
399
- clearTimeout(timer);
400
- timer = void 0;
401
- }
402
- };
403
- const filter = (invoke) => {
404
- const duration = unref(ms);
405
- const elapsed = Date.now() - lastExec;
406
- clear();
407
- if (duration <= 0) {
408
- lastExec = Date.now();
409
- return invoke();
410
- }
411
- if (elapsed > duration && (leading || !isLeading)) {
412
- lastExec = Date.now();
413
- invoke();
414
- } else if (trailing) {
415
- timer = setTimeout(() => {
416
- lastExec = Date.now();
417
- isLeading = true;
418
- clear();
419
- invoke();
420
- }, duration);
421
- }
422
- if (!leading && !timer)
423
- timer = setTimeout(() => isLeading = true, duration);
424
- isLeading = false;
425
- };
426
- return filter;
427
- }
428
- function pausableFilter(extendFilter = bypassFilter) {
429
- const isActive = ref(true);
430
- function pause() {
431
- isActive.value = false;
432
- }
433
- function resume() {
434
- isActive.value = true;
435
- }
436
- const eventFilter = (...args) => {
437
- if (isActive.value)
438
- extendFilter(...args);
439
- };
440
- return { isActive, pause, resume, eventFilter };
441
- }
442
-
443
- function promiseTimeout(ms, throwOnTimeout = false, reason = "Timeout") {
444
- return new Promise((resolve, reject) => {
445
- if (throwOnTimeout)
446
- setTimeout(() => reject(reason), ms);
447
- else
448
- setTimeout(resolve, ms);
449
- });
450
- }
451
- function identity(arg) {
452
- return arg;
453
- }
454
- function createSingletonPromise(fn) {
455
- let _promise;
456
- function wrapper() {
457
- if (!_promise)
458
- _promise = fn();
459
- return _promise;
460
- }
461
- wrapper.reset = async () => {
462
- const _prev = _promise;
463
- _promise = void 0;
464
- if (_prev)
465
- await _prev;
466
- };
467
- return wrapper;
468
- }
469
- function invoke(fn) {
470
- return fn();
471
- }
472
- function containsProp(obj, ...props) {
473
- return props.some((k) => k in obj);
474
- }
475
- function increaseWithUnit(target, delta) {
476
- var _a;
477
- if (typeof target === "number")
478
- return target + delta;
479
- const value = ((_a = target.match(/^-?[0-9]+\.?[0-9]*/)) == null ? void 0 : _a[0]) || "";
480
- const unit = target.slice(value.length);
481
- const result = parseFloat(value) + delta;
482
- if (Number.isNaN(result))
483
- return target;
484
- return result + unit;
485
- }
486
- function objectPick(obj, keys, omitUndefined = false) {
487
- return keys.reduce((n, k) => {
488
- if (k in obj) {
489
- if (!omitUndefined || obj[k] !== void 0)
490
- n[k] = obj[k];
491
- }
492
- return n;
493
- }, {});
494
- }
495
-
496
503
  function useDebounceFn(fn, ms = 200, options = {}) {
497
504
  return createFilterWrapper(debounceFilter(ms, options), fn);
498
505
  }
@@ -1058,6 +1065,31 @@ function useToggle(initialValue = false, options = {}) {
1058
1065
  return [innerValue, toggle];
1059
1066
  }
1060
1067
 
1068
+ function watchArray(source, cb, options) {
1069
+ let oldList = (options == null ? void 0 : options.immediate) ? [] : [
1070
+ ...source instanceof Function ? source() : Array.isArray(source) ? source : unref(source)
1071
+ ];
1072
+ return watch(source, (newList, _, onCleanup) => {
1073
+ const oldListRemains = new Array(oldList.length);
1074
+ const added = [];
1075
+ for (const obj of newList) {
1076
+ let found = false;
1077
+ for (let i = 0; i < oldList.length; i++) {
1078
+ if (!oldListRemains[i] && obj === oldList[i]) {
1079
+ oldListRemains[i] = true;
1080
+ found = true;
1081
+ break;
1082
+ }
1083
+ }
1084
+ if (!found)
1085
+ added.push(obj);
1086
+ }
1087
+ const removed = oldList.filter((_2, i) => !oldListRemains[i]);
1088
+ cb(newList, oldList, added, removed, onCleanup);
1089
+ oldList = [...newList];
1090
+ }, options);
1091
+ }
1092
+
1061
1093
  var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
1062
1094
  var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
1063
1095
  var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
@@ -1407,4 +1439,4 @@ function whenever(source, cb, options) {
1407
1439
  }, options);
1408
1440
  }
1409
1441
 
1410
- export { __onlyVue3, logicAnd as and, assert, refAutoReset as autoResetRef, bypassFilter, clamp, computedEager, computedWithControl, containsProp, computedWithControl as controlledComputed, controlledRef, createEventHook, createFilterWrapper, createGlobalState, createInjectionState, reactify as createReactiveFn, createSharedComposable, createSingletonPromise, debounceFilter, refDebounced as debouncedRef, watchDebounced as debouncedWatch, directiveHooks, computedEager as eagerComputed, extendRef, formatDate, get, identity, watchIgnorable as ignorableWatch, increaseWithUnit, invoke, isBoolean, isClient, isDef, isDefined, isFunction, isIOS, isNumber, isObject, isString, isWindow, logicAnd, logicNot, logicOr, makeDestructurable, noop, normalizeDate, logicNot as not, now, objectPick, logicOr as or, pausableFilter, watchPausable as pausableWatch, promiseTimeout, rand, reactify, reactifyObject, reactiveComputed, reactiveOmit, reactivePick, refAutoReset, refDebounced, refDefault, refThrottled, refWithControl, resolveRef, resolveUnref, set, syncRef, syncRefs, throttleFilter, refThrottled as throttledRef, watchThrottled as throttledWatch, timestamp, toReactive, toRefs, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, until, useCounter, useDateFormat, refDebounced as useDebounce, useDebounceFn, useInterval, useIntervalFn, useLastChanged, refThrottled as useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useToggle, watchAtMost, watchDebounced, watchIgnorable, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever };
1442
+ export { __onlyVue3, logicAnd as and, assert, refAutoReset as autoResetRef, bypassFilter, clamp, computedEager, computedWithControl, containsProp, computedWithControl as controlledComputed, controlledRef, createEventHook, createFilterWrapper, createGlobalState, createInjectionState, reactify as createReactiveFn, createSharedComposable, createSingletonPromise, debounceFilter, refDebounced as debouncedRef, watchDebounced as debouncedWatch, directiveHooks, computedEager as eagerComputed, extendRef, formatDate, get, identity, watchIgnorable as ignorableWatch, increaseWithUnit, invoke, isBoolean, isClient, isDef, isDefined, isFunction, isIOS, isNumber, isObject, isString, isWindow, logicAnd, logicNot, logicOr, makeDestructurable, noop, normalizeDate, logicNot as not, now, objectPick, logicOr as or, pausableFilter, watchPausable as pausableWatch, promiseTimeout, rand, reactify, reactifyObject, reactiveComputed, reactiveOmit, reactivePick, refAutoReset, refDebounced, refDefault, refThrottled, refWithControl, resolveRef, resolveUnref, set, syncRef, syncRefs, throttleFilter, refThrottled as throttledRef, watchThrottled as throttledWatch, timestamp, toReactive, toRefs, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, until, useCounter, useDateFormat, refDebounced as useDebounce, useDebounceFn, useInterval, useIntervalFn, useLastChanged, refThrottled as useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useToggle, watchArray, watchAtMost, watchDebounced, watchIgnorable, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vueuse/shared",
3
- "version": "8.8.0",
3
+ "version": "8.9.1",
4
4
  "author": "Anthony Fu <https://github.com/antfu>",
5
5
  "license": "MIT",
6
6
  "funding": "https://github.com/sponsors/antfu",