@vueuse/shared 10.0.0-beta.4 → 10.0.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/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { shallowRef, watchEffect, readonly, unref, ref, isVue3, version, watch, customRef, getCurrentScope, onScopeDispose, effectScope, provide, inject, isRef, computed, reactive, toRefs as toRefs$1, toRef as toRef$1, isVue2, set as set$1, getCurrentInstance, onBeforeMount, nextTick, onBeforeUnmount, onMounted, onUnmounted, isReactive } from 'vue-demi';
1
+ import { shallowRef, watchEffect, readonly, ref, watch, customRef, getCurrentScope, onScopeDispose, effectScope, provide, inject, isVue3, version, isRef, unref, computed, reactive, toRefs as toRefs$1, toRef as toRef$1, isVue2, set as set$1, getCurrentInstance, onBeforeMount, nextTick, onBeforeUnmount, onMounted, onUnmounted, isReactive } from 'vue-demi';
2
2
 
3
3
  var __defProp$b = Object.defineProperty;
4
4
  var __defProps$8 = Object.defineProperties;
@@ -30,228 +30,6 @@ 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 hasOwn = (val, key) => Object.prototype.hasOwnProperty.call(val, key);
58
- const isIOS = isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
59
-
60
- function toValue(r) {
61
- return typeof r === "function" ? r() : unref(r);
62
- }
63
- const resolveUnref = toValue;
64
-
65
- function createFilterWrapper(filter, fn) {
66
- function wrapper(...args) {
67
- return new Promise((resolve, reject) => {
68
- Promise.resolve(filter(() => fn.apply(this, args), { fn, thisArg: this, args })).then(resolve).catch(reject);
69
- });
70
- }
71
- return wrapper;
72
- }
73
- const bypassFilter = (invoke) => {
74
- return invoke();
75
- };
76
- function debounceFilter(ms, options = {}) {
77
- let timer;
78
- let maxTimer;
79
- let lastRejector = noop;
80
- const _clearTimeout = (timer2) => {
81
- clearTimeout(timer2);
82
- lastRejector();
83
- lastRejector = noop;
84
- };
85
- const filter = (invoke) => {
86
- const duration = toValue(ms);
87
- const maxDuration = toValue(options.maxWait);
88
- if (timer)
89
- _clearTimeout(timer);
90
- if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) {
91
- if (maxTimer) {
92
- _clearTimeout(maxTimer);
93
- maxTimer = null;
94
- }
95
- return Promise.resolve(invoke());
96
- }
97
- return new Promise((resolve, reject) => {
98
- lastRejector = options.rejectOnCancel ? reject : resolve;
99
- if (maxDuration && !maxTimer) {
100
- maxTimer = setTimeout(() => {
101
- if (timer)
102
- _clearTimeout(timer);
103
- maxTimer = null;
104
- resolve(invoke());
105
- }, maxDuration);
106
- }
107
- timer = setTimeout(() => {
108
- if (maxTimer)
109
- _clearTimeout(maxTimer);
110
- maxTimer = null;
111
- resolve(invoke());
112
- }, duration);
113
- });
114
- };
115
- return filter;
116
- }
117
- function throttleFilter(ms, trailing = true, leading = true, rejectOnCancel = false) {
118
- let lastExec = 0;
119
- let timer;
120
- let isLeading = true;
121
- let lastRejector = noop;
122
- let lastValue;
123
- const clear = () => {
124
- if (timer) {
125
- clearTimeout(timer);
126
- timer = void 0;
127
- lastRejector();
128
- lastRejector = noop;
129
- }
130
- };
131
- const filter = (_invoke) => {
132
- const duration = toValue(ms);
133
- const elapsed = Date.now() - lastExec;
134
- const invoke = () => {
135
- return lastValue = _invoke();
136
- };
137
- clear();
138
- if (duration <= 0) {
139
- lastExec = Date.now();
140
- return invoke();
141
- }
142
- if (elapsed > duration && (leading || !isLeading)) {
143
- lastExec = Date.now();
144
- invoke();
145
- } else if (trailing) {
146
- lastValue = new Promise((resolve, reject) => {
147
- lastRejector = rejectOnCancel ? reject : resolve;
148
- timer = setTimeout(() => {
149
- lastExec = Date.now();
150
- isLeading = true;
151
- resolve(invoke());
152
- clear();
153
- }, Math.max(0, duration - elapsed));
154
- });
155
- }
156
- if (!leading && !timer)
157
- timer = setTimeout(() => isLeading = true, duration);
158
- isLeading = false;
159
- return lastValue;
160
- };
161
- return filter;
162
- }
163
- function pausableFilter(extendFilter = bypassFilter) {
164
- const isActive = ref(true);
165
- function pause() {
166
- isActive.value = false;
167
- }
168
- function resume() {
169
- isActive.value = true;
170
- }
171
- const eventFilter = (...args) => {
172
- if (isActive.value)
173
- extendFilter(...args);
174
- };
175
- return { isActive: readonly(isActive), pause, resume, eventFilter };
176
- }
177
-
178
- function __onlyVue3(name = "this function") {
179
- if (isVue3)
180
- return;
181
- throw new Error(`[VueUse] ${name} is only works on Vue 3.`);
182
- }
183
- function __onlyVue27Plus(name = "this function") {
184
- if (isVue3 || version.startsWith("2.7."))
185
- return;
186
- throw new Error(`[VueUse] ${name} is only works on Vue 2.7 or above.`);
187
- }
188
- const directiveHooks = {
189
- mounted: isVue3 ? "mounted" : "inserted",
190
- updated: isVue3 ? "updated" : "componentUpdated",
191
- unmounted: isVue3 ? "unmounted" : "unbind"
192
- };
193
-
194
- function promiseTimeout(ms, throwOnTimeout = false, reason = "Timeout") {
195
- return new Promise((resolve, reject) => {
196
- if (throwOnTimeout)
197
- setTimeout(() => reject(reason), ms);
198
- else
199
- setTimeout(resolve, ms);
200
- });
201
- }
202
- function identity(arg) {
203
- return arg;
204
- }
205
- function createSingletonPromise(fn) {
206
- let _promise;
207
- function wrapper() {
208
- if (!_promise)
209
- _promise = fn();
210
- return _promise;
211
- }
212
- wrapper.reset = async () => {
213
- const _prev = _promise;
214
- _promise = void 0;
215
- if (_prev)
216
- await _prev;
217
- };
218
- return wrapper;
219
- }
220
- function invoke(fn) {
221
- return fn();
222
- }
223
- function containsProp(obj, ...props) {
224
- return props.some((k) => k in obj);
225
- }
226
- function increaseWithUnit(target, delta) {
227
- var _a;
228
- if (typeof target === "number")
229
- return target + delta;
230
- const value = ((_a = target.match(/^-?[0-9]+\.?[0-9]*/)) == null ? void 0 : _a[0]) || "";
231
- const unit = target.slice(value.length);
232
- const result = parseFloat(value) + delta;
233
- if (Number.isNaN(result))
234
- return target;
235
- return result + unit;
236
- }
237
- function objectPick(obj, keys, omitUndefined = false) {
238
- return keys.reduce((n, k) => {
239
- if (k in obj) {
240
- if (!omitUndefined || obj[k] !== void 0)
241
- n[k] = obj[k];
242
- }
243
- return n;
244
- }, {});
245
- }
246
- function objectOmit(obj, keys, omitUndefined = false) {
247
- return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
248
- return (!omitUndefined || value !== void 0) && !keys.includes(key);
249
- }));
250
- }
251
- function objectEntries(obj) {
252
- return Object.entries(obj);
253
- }
254
-
255
33
  function computedWithControl(source, fn) {
256
34
  let v = void 0;
257
35
  let track;
@@ -262,8 +40,8 @@ function computedWithControl(source, fn) {
262
40
  trigger();
263
41
  };
264
42
  watch(source, update, { flush: "sync" });
265
- const get = isFunction(fn) ? fn : fn.get;
266
- const set = isFunction(fn) ? void 0 : fn.set;
43
+ const get = typeof fn === "function" ? fn : fn.get;
44
+ const set = typeof fn === "function" ? void 0 : fn.set;
267
45
  const result = customRef((_track, _trigger) => {
268
46
  track = _track;
269
47
  trigger = _trigger;
@@ -365,7 +143,11 @@ function createSharedComposable(composable) {
365
143
  }
366
144
 
367
145
  function extendRef(ref, extend, { enumerable = false, unwrap = true } = {}) {
368
- __onlyVue27Plus();
146
+ if (!isVue3 && !version.startsWith("2.7.")) {
147
+ if (process.env.NODE_ENV !== "production")
148
+ throw new Error("[VueUse] extendRef only works in Vue 2.7 or above.");
149
+ return;
150
+ }
369
151
  for (const [key, value] of Object.entries(extend)) {
370
152
  if (key === "value")
371
153
  continue;
@@ -433,6 +215,11 @@ function makeDestructurable(obj, arr) {
433
215
  }
434
216
  }
435
217
 
218
+ function toValue(r) {
219
+ return typeof r === "function" ? r() : unref(r);
220
+ }
221
+ const resolveUnref = toValue;
222
+
436
223
  function reactify(fn, options) {
437
224
  const unrefFn = (options == null ? void 0 : options.computedGetter) === false ? unref : toValue;
438
225
  return function(...args) {
@@ -508,6 +295,212 @@ function reactiveOmit(obj, ...keys) {
508
295
  );
509
296
  }
510
297
 
298
+ const isClient = typeof window !== "undefined";
299
+ const isDef = (val) => typeof val !== "undefined";
300
+ const notNullish = (val) => val != null;
301
+ const assert = (condition, ...infos) => {
302
+ if (!condition)
303
+ console.warn(...infos);
304
+ };
305
+ const toString = Object.prototype.toString;
306
+ const isObject = (val) => toString.call(val) === "[object Object]";
307
+ const now = () => Date.now();
308
+ const timestamp = () => +Date.now();
309
+ const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
310
+ const noop = () => {
311
+ };
312
+ const rand = (min, max) => {
313
+ min = Math.ceil(min);
314
+ max = Math.floor(max);
315
+ return Math.floor(Math.random() * (max - min + 1)) + min;
316
+ };
317
+ const hasOwn = (val, key) => Object.prototype.hasOwnProperty.call(val, key);
318
+ const isIOS = /* @__PURE__ */ getIsIOS();
319
+ function getIsIOS() {
320
+ var _a;
321
+ return isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /* @__PURE__ */ /iP(ad|hone|od)/.test(window.navigator.userAgent);
322
+ }
323
+
324
+ function createFilterWrapper(filter, fn) {
325
+ function wrapper(...args) {
326
+ return new Promise((resolve, reject) => {
327
+ Promise.resolve(filter(() => fn.apply(this, args), { fn, thisArg: this, args })).then(resolve).catch(reject);
328
+ });
329
+ }
330
+ return wrapper;
331
+ }
332
+ const bypassFilter = (invoke) => {
333
+ return invoke();
334
+ };
335
+ function debounceFilter(ms, options = {}) {
336
+ let timer;
337
+ let maxTimer;
338
+ let lastRejector = noop;
339
+ const _clearTimeout = (timer2) => {
340
+ clearTimeout(timer2);
341
+ lastRejector();
342
+ lastRejector = noop;
343
+ };
344
+ const filter = (invoke) => {
345
+ const duration = toValue(ms);
346
+ const maxDuration = toValue(options.maxWait);
347
+ if (timer)
348
+ _clearTimeout(timer);
349
+ if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) {
350
+ if (maxTimer) {
351
+ _clearTimeout(maxTimer);
352
+ maxTimer = null;
353
+ }
354
+ return Promise.resolve(invoke());
355
+ }
356
+ return new Promise((resolve, reject) => {
357
+ lastRejector = options.rejectOnCancel ? reject : resolve;
358
+ if (maxDuration && !maxTimer) {
359
+ maxTimer = setTimeout(() => {
360
+ if (timer)
361
+ _clearTimeout(timer);
362
+ maxTimer = null;
363
+ resolve(invoke());
364
+ }, maxDuration);
365
+ }
366
+ timer = setTimeout(() => {
367
+ if (maxTimer)
368
+ _clearTimeout(maxTimer);
369
+ maxTimer = null;
370
+ resolve(invoke());
371
+ }, duration);
372
+ });
373
+ };
374
+ return filter;
375
+ }
376
+ function throttleFilter(ms, trailing = true, leading = true, rejectOnCancel = false) {
377
+ let lastExec = 0;
378
+ let timer;
379
+ let isLeading = true;
380
+ let lastRejector = noop;
381
+ let lastValue;
382
+ const clear = () => {
383
+ if (timer) {
384
+ clearTimeout(timer);
385
+ timer = void 0;
386
+ lastRejector();
387
+ lastRejector = noop;
388
+ }
389
+ };
390
+ const filter = (_invoke) => {
391
+ const duration = toValue(ms);
392
+ const elapsed = Date.now() - lastExec;
393
+ const invoke = () => {
394
+ return lastValue = _invoke();
395
+ };
396
+ clear();
397
+ if (duration <= 0) {
398
+ lastExec = Date.now();
399
+ return invoke();
400
+ }
401
+ if (elapsed > duration && (leading || !isLeading)) {
402
+ lastExec = Date.now();
403
+ invoke();
404
+ } else if (trailing) {
405
+ lastValue = new Promise((resolve, reject) => {
406
+ lastRejector = rejectOnCancel ? reject : resolve;
407
+ timer = setTimeout(() => {
408
+ lastExec = Date.now();
409
+ isLeading = true;
410
+ resolve(invoke());
411
+ clear();
412
+ }, Math.max(0, duration - elapsed));
413
+ });
414
+ }
415
+ if (!leading && !timer)
416
+ timer = setTimeout(() => isLeading = true, duration);
417
+ isLeading = false;
418
+ return lastValue;
419
+ };
420
+ return filter;
421
+ }
422
+ function pausableFilter(extendFilter = bypassFilter) {
423
+ const isActive = ref(true);
424
+ function pause() {
425
+ isActive.value = false;
426
+ }
427
+ function resume() {
428
+ isActive.value = true;
429
+ }
430
+ const eventFilter = (...args) => {
431
+ if (isActive.value)
432
+ extendFilter(...args);
433
+ };
434
+ return { isActive: readonly(isActive), pause, resume, eventFilter };
435
+ }
436
+
437
+ const directiveHooks = {
438
+ mounted: isVue3 ? "mounted" : "inserted",
439
+ updated: isVue3 ? "updated" : "componentUpdated",
440
+ unmounted: isVue3 ? "unmounted" : "unbind"
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
+ function objectOmit(obj, keys, omitUndefined = false) {
496
+ return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
497
+ return (!omitUndefined || value !== void 0) && !keys.includes(key);
498
+ }));
499
+ }
500
+ function objectEntries(obj) {
501
+ return Object.entries(obj);
502
+ }
503
+
511
504
  function toRef(...args) {
512
505
  if (args.length !== 1)
513
506
  return toRef$1(...args);
@@ -908,7 +901,7 @@ function useArrayDifference(...args) {
908
901
  const list = args[0];
909
902
  const values = args[1];
910
903
  let compareFn = (_a = args[2]) != null ? _a : defaultComparator;
911
- if (isString(compareFn)) {
904
+ if (typeof compareFn === "string") {
912
905
  const key = compareFn;
913
906
  compareFn = (value, othVal) => value[key] === othVal[key];
914
907
  }
@@ -1128,7 +1121,7 @@ function useIntervalFn(cb, interval = 1e3, options = {}) {
1128
1121
  }
1129
1122
  if (immediate && isClient)
1130
1123
  resume();
1131
- if (isRef(interval) || isFunction(interval)) {
1124
+ if (isRef(interval) || typeof interval === "function") {
1132
1125
  const stopWatch = watch(interval, () => {
1133
1126
  if (isActive.value && isClient)
1134
1127
  resume();
@@ -1791,4 +1784,4 @@ function whenever(source, cb, options) {
1791
1784
  );
1792
1785
  }
1793
1786
 
1794
- export { __onlyVue27Plus, __onlyVue3, 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, hasOwn, identity, watchIgnorable as ignorableWatch, increaseWithUnit, invoke, isBoolean, isClient, isDef, isDefined, isFunction, isIOS, isNumber, isObject, isString, isWindow, makeDestructurable, noop, normalizeDate, now, objectEntries, objectOmit, objectPick, 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, toRef, toRefs, toValue, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, until, useArrayDifference, useArrayEvery, useArrayFilter, useArrayFind, useArrayFindIndex, useArrayFindLast, useArrayIncludes, useArrayJoin, useArrayMap, useArrayReduce, useArraySome, useArrayUnique, useCounter, useDateFormat, refDebounced as useDebounce, useDebounceFn, useInterval, useIntervalFn, useLastChanged, refThrottled as useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useToNumber, useToString, useToggle, watchArray, watchAtMost, watchDebounced, watchDeep, watchIgnorable, watchImmediate, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever };
1787
+ export { 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, hasOwn, identity, watchIgnorable as ignorableWatch, increaseWithUnit, invoke, isClient, isDef, isDefined, isIOS, isObject, makeDestructurable, noop, normalizeDate, notNullish, now, objectEntries, objectOmit, objectPick, 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, toRef, toRefs, toValue, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, until, useArrayDifference, useArrayEvery, useArrayFilter, useArrayFind, useArrayFindIndex, useArrayFindLast, useArrayIncludes, useArrayJoin, useArrayMap, useArrayReduce, useArraySome, useArrayUnique, useCounter, useDateFormat, refDebounced as useDebounce, useDebounceFn, useInterval, useIntervalFn, useLastChanged, refThrottled as useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useToNumber, useToString, useToggle, watchArray, watchAtMost, watchDebounced, watchDeep, watchIgnorable, watchImmediate, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vueuse/shared",
3
- "version": "10.0.0-beta.4",
3
+ "version": "10.0.0",
4
4
  "author": "Anthony Fu <https://github.com/antfu>",
5
5
  "license": "MIT",
6
6
  "funding": "https://github.com/sponsors/antfu",
@@ -33,6 +33,6 @@
33
33
  "jsdelivr": "./index.iife.min.js",
34
34
  "types": "./index.d.ts",
35
35
  "dependencies": {
36
- "vue-demi": "*"
36
+ "vue-demi": ">=0.14.0"
37
37
  }
38
38
  }