@vueuse/shared 8.8.0 → 8.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/index.cjs CHANGED
@@ -34,31 +34,219 @@ function computedEager(fn, options) {
34
34
  return vueDemi.readonly(result);
35
35
  }
36
36
 
37
+ var _a;
38
+ const isClient = typeof window !== "undefined";
39
+ const isDef = (val) => typeof val !== "undefined";
40
+ const assert = (condition, ...infos) => {
41
+ if (!condition)
42
+ console.warn(...infos);
43
+ };
44
+ const toString = Object.prototype.toString;
45
+ const isBoolean = (val) => typeof val === "boolean";
46
+ const isFunction = (val) => typeof val === "function";
47
+ const isNumber = (val) => typeof val === "number";
48
+ const isString = (val) => typeof val === "string";
49
+ const isObject = (val) => toString.call(val) === "[object Object]";
50
+ const isWindow = (val) => typeof window !== "undefined" && toString.call(val) === "[object Window]";
51
+ const now = () => Date.now();
52
+ const timestamp = () => +Date.now();
53
+ const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
54
+ const noop = () => {
55
+ };
56
+ const rand = (min, max) => {
57
+ min = Math.ceil(min);
58
+ max = Math.floor(max);
59
+ return Math.floor(Math.random() * (max - min + 1)) + min;
60
+ };
61
+ const isIOS = isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
62
+
63
+ function createFilterWrapper(filter, fn) {
64
+ function wrapper(...args) {
65
+ filter(() => fn.apply(this, args), { fn, thisArg: this, args });
66
+ }
67
+ return wrapper;
68
+ }
69
+ const bypassFilter = (invoke) => {
70
+ return invoke();
71
+ };
72
+ function debounceFilter(ms, options = {}) {
73
+ let timer;
74
+ let maxTimer;
75
+ const filter = (invoke) => {
76
+ const duration = vueDemi.unref(ms);
77
+ const maxDuration = vueDemi.unref(options.maxWait);
78
+ if (timer)
79
+ clearTimeout(timer);
80
+ if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) {
81
+ if (maxTimer) {
82
+ clearTimeout(maxTimer);
83
+ maxTimer = null;
84
+ }
85
+ return invoke();
86
+ }
87
+ if (maxDuration && !maxTimer) {
88
+ maxTimer = setTimeout(() => {
89
+ if (timer)
90
+ clearTimeout(timer);
91
+ maxTimer = null;
92
+ invoke();
93
+ }, maxDuration);
94
+ }
95
+ timer = setTimeout(() => {
96
+ if (maxTimer)
97
+ clearTimeout(maxTimer);
98
+ maxTimer = null;
99
+ invoke();
100
+ }, duration);
101
+ };
102
+ return filter;
103
+ }
104
+ function throttleFilter(ms, trailing = true, leading = true) {
105
+ let lastExec = 0;
106
+ let timer;
107
+ let isLeading = true;
108
+ const clear = () => {
109
+ if (timer) {
110
+ clearTimeout(timer);
111
+ timer = void 0;
112
+ }
113
+ };
114
+ const filter = (invoke) => {
115
+ const duration = vueDemi.unref(ms);
116
+ const elapsed = Date.now() - lastExec;
117
+ clear();
118
+ if (duration <= 0) {
119
+ lastExec = Date.now();
120
+ return invoke();
121
+ }
122
+ if (elapsed > duration && (leading || !isLeading)) {
123
+ lastExec = Date.now();
124
+ invoke();
125
+ } else if (trailing) {
126
+ timer = setTimeout(() => {
127
+ lastExec = Date.now();
128
+ isLeading = true;
129
+ clear();
130
+ invoke();
131
+ }, duration);
132
+ }
133
+ if (!leading && !timer)
134
+ timer = setTimeout(() => isLeading = true, duration);
135
+ isLeading = false;
136
+ };
137
+ return filter;
138
+ }
139
+ function pausableFilter(extendFilter = bypassFilter) {
140
+ const isActive = vueDemi.ref(true);
141
+ function pause() {
142
+ isActive.value = false;
143
+ }
144
+ function resume() {
145
+ isActive.value = true;
146
+ }
147
+ const eventFilter = (...args) => {
148
+ if (isActive.value)
149
+ extendFilter(...args);
150
+ };
151
+ return { isActive, pause, resume, eventFilter };
152
+ }
153
+
154
+ function __onlyVue3(name = "this function") {
155
+ if (vueDemi.isVue3)
156
+ return;
157
+ throw new Error(`[VueUse] ${name} is only works on Vue 3.`);
158
+ }
159
+ const directiveHooks = {
160
+ mounted: vueDemi.isVue3 ? "mounted" : "inserted",
161
+ updated: vueDemi.isVue3 ? "updated" : "componentUpdated",
162
+ unmounted: vueDemi.isVue3 ? "unmounted" : "unbind"
163
+ };
164
+
165
+ function promiseTimeout(ms, throwOnTimeout = false, reason = "Timeout") {
166
+ return new Promise((resolve, reject) => {
167
+ if (throwOnTimeout)
168
+ setTimeout(() => reject(reason), ms);
169
+ else
170
+ setTimeout(resolve, ms);
171
+ });
172
+ }
173
+ function identity(arg) {
174
+ return arg;
175
+ }
176
+ function createSingletonPromise(fn) {
177
+ let _promise;
178
+ function wrapper() {
179
+ if (!_promise)
180
+ _promise = fn();
181
+ return _promise;
182
+ }
183
+ wrapper.reset = async () => {
184
+ const _prev = _promise;
185
+ _promise = void 0;
186
+ if (_prev)
187
+ await _prev;
188
+ };
189
+ return wrapper;
190
+ }
191
+ function invoke(fn) {
192
+ return fn();
193
+ }
194
+ function containsProp(obj, ...props) {
195
+ return props.some((k) => k in obj);
196
+ }
197
+ function increaseWithUnit(target, delta) {
198
+ var _a;
199
+ if (typeof target === "number")
200
+ return target + delta;
201
+ const value = ((_a = target.match(/^-?[0-9]+\.?[0-9]*/)) == null ? void 0 : _a[0]) || "";
202
+ const unit = target.slice(value.length);
203
+ const result = parseFloat(value) + delta;
204
+ if (Number.isNaN(result))
205
+ return target;
206
+ return result + unit;
207
+ }
208
+ function objectPick(obj, keys, omitUndefined = false) {
209
+ return keys.reduce((n, k) => {
210
+ if (k in obj) {
211
+ if (!omitUndefined || obj[k] !== void 0)
212
+ n[k] = obj[k];
213
+ }
214
+ return n;
215
+ }, {});
216
+ }
217
+
37
218
  function computedWithControl(source, fn) {
38
219
  let v = void 0;
39
220
  let track;
40
221
  let trigger;
41
222
  const dirty = vueDemi.ref(true);
42
- vueDemi.watch(source, () => {
223
+ const update = () => {
43
224
  dirty.value = true;
44
225
  trigger();
45
- }, { flush: "sync" });
46
- return vueDemi.customRef((_track, _trigger) => {
226
+ };
227
+ vueDemi.watch(source, update, { flush: "sync" });
228
+ const get = isFunction(fn) ? fn : fn.get;
229
+ const set = isFunction(fn) ? void 0 : fn.set;
230
+ const result = vueDemi.customRef((_track, _trigger) => {
47
231
  track = _track;
48
232
  trigger = _trigger;
49
233
  return {
50
234
  get() {
51
235
  if (dirty.value) {
52
- v = fn();
236
+ v = get();
53
237
  dirty.value = false;
54
238
  }
55
239
  track();
56
240
  return v;
57
241
  },
58
- set() {
242
+ set(v2) {
243
+ set == null ? void 0 : set(v2);
59
244
  }
60
245
  };
61
246
  });
247
+ if (Object.isExtensible(result))
248
+ result.trigger = update;
249
+ return result;
62
250
  }
63
251
 
64
252
  function createEventHook() {
@@ -137,17 +325,6 @@ function createSharedComposable(composable) {
137
325
  };
138
326
  }
139
327
 
140
- function __onlyVue3(name = "this function") {
141
- if (vueDemi.isVue3)
142
- return;
143
- throw new Error(`[VueUse] ${name} is only works on Vue 3.`);
144
- }
145
- const directiveHooks = {
146
- mounted: vueDemi.isVue3 ? "mounted" : "inserted",
147
- updated: vueDemi.isVue3 ? "updated" : "componentUpdated",
148
- unmounted: vueDemi.isVue3 ? "unmounted" : "unbind"
149
- };
150
-
151
328
  function extendRef(ref, extend, { enumerable = false, unwrap = true } = {}) {
152
329
  __onlyVue3();
153
330
  for (const [key, value] of Object.entries(extend)) {
@@ -327,176 +504,6 @@ function refAutoReset(defaultValue, afterMs = 1e4) {
327
504
  });
328
505
  }
329
506
 
330
- var _a;
331
- const isClient = typeof window !== "undefined";
332
- const isDef = (val) => typeof val !== "undefined";
333
- const assert = (condition, ...infos) => {
334
- if (!condition)
335
- console.warn(...infos);
336
- };
337
- const toString = Object.prototype.toString;
338
- const isBoolean = (val) => typeof val === "boolean";
339
- const isFunction = (val) => typeof val === "function";
340
- const isNumber = (val) => typeof val === "number";
341
- const isString = (val) => typeof val === "string";
342
- const isObject = (val) => toString.call(val) === "[object Object]";
343
- const isWindow = (val) => typeof window !== "undefined" && toString.call(val) === "[object Window]";
344
- const now = () => Date.now();
345
- const timestamp = () => +Date.now();
346
- const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
347
- const noop = () => {
348
- };
349
- const rand = (min, max) => {
350
- min = Math.ceil(min);
351
- max = Math.floor(max);
352
- return Math.floor(Math.random() * (max - min + 1)) + min;
353
- };
354
- const isIOS = isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
355
-
356
- function createFilterWrapper(filter, fn) {
357
- function wrapper(...args) {
358
- filter(() => fn.apply(this, args), { fn, thisArg: this, args });
359
- }
360
- return wrapper;
361
- }
362
- const bypassFilter = (invoke) => {
363
- return invoke();
364
- };
365
- function debounceFilter(ms, options = {}) {
366
- let timer;
367
- let maxTimer;
368
- const filter = (invoke) => {
369
- const duration = vueDemi.unref(ms);
370
- const maxDuration = vueDemi.unref(options.maxWait);
371
- if (timer)
372
- clearTimeout(timer);
373
- if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) {
374
- if (maxTimer) {
375
- clearTimeout(maxTimer);
376
- maxTimer = null;
377
- }
378
- return invoke();
379
- }
380
- if (maxDuration && !maxTimer) {
381
- maxTimer = setTimeout(() => {
382
- if (timer)
383
- clearTimeout(timer);
384
- maxTimer = null;
385
- invoke();
386
- }, maxDuration);
387
- }
388
- timer = setTimeout(() => {
389
- if (maxTimer)
390
- clearTimeout(maxTimer);
391
- maxTimer = null;
392
- invoke();
393
- }, duration);
394
- };
395
- return filter;
396
- }
397
- function throttleFilter(ms, trailing = true, leading = true) {
398
- let lastExec = 0;
399
- let timer;
400
- let isLeading = true;
401
- const clear = () => {
402
- if (timer) {
403
- clearTimeout(timer);
404
- timer = void 0;
405
- }
406
- };
407
- const filter = (invoke) => {
408
- const duration = vueDemi.unref(ms);
409
- const elapsed = Date.now() - lastExec;
410
- clear();
411
- if (duration <= 0) {
412
- lastExec = Date.now();
413
- return invoke();
414
- }
415
- if (elapsed > duration && (leading || !isLeading)) {
416
- lastExec = Date.now();
417
- invoke();
418
- } else if (trailing) {
419
- timer = setTimeout(() => {
420
- lastExec = Date.now();
421
- isLeading = true;
422
- clear();
423
- invoke();
424
- }, duration);
425
- }
426
- if (!leading && !timer)
427
- timer = setTimeout(() => isLeading = true, duration);
428
- isLeading = false;
429
- };
430
- return filter;
431
- }
432
- function pausableFilter(extendFilter = bypassFilter) {
433
- const isActive = vueDemi.ref(true);
434
- function pause() {
435
- isActive.value = false;
436
- }
437
- function resume() {
438
- isActive.value = true;
439
- }
440
- const eventFilter = (...args) => {
441
- if (isActive.value)
442
- extendFilter(...args);
443
- };
444
- return { isActive, pause, resume, eventFilter };
445
- }
446
-
447
- function promiseTimeout(ms, throwOnTimeout = false, reason = "Timeout") {
448
- return new Promise((resolve, reject) => {
449
- if (throwOnTimeout)
450
- setTimeout(() => reject(reason), ms);
451
- else
452
- setTimeout(resolve, ms);
453
- });
454
- }
455
- function identity(arg) {
456
- return arg;
457
- }
458
- function createSingletonPromise(fn) {
459
- let _promise;
460
- function wrapper() {
461
- if (!_promise)
462
- _promise = fn();
463
- return _promise;
464
- }
465
- wrapper.reset = async () => {
466
- const _prev = _promise;
467
- _promise = void 0;
468
- if (_prev)
469
- await _prev;
470
- };
471
- return wrapper;
472
- }
473
- function invoke(fn) {
474
- return fn();
475
- }
476
- function containsProp(obj, ...props) {
477
- return props.some((k) => k in obj);
478
- }
479
- function increaseWithUnit(target, delta) {
480
- var _a;
481
- if (typeof target === "number")
482
- return target + delta;
483
- const value = ((_a = target.match(/^-?[0-9]+\.?[0-9]*/)) == null ? void 0 : _a[0]) || "";
484
- const unit = target.slice(value.length);
485
- const result = parseFloat(value) + delta;
486
- if (Number.isNaN(result))
487
- return target;
488
- return result + unit;
489
- }
490
- function objectPick(obj, keys, omitUndefined = false) {
491
- return keys.reduce((n, k) => {
492
- if (k in obj) {
493
- if (!omitUndefined || obj[k] !== void 0)
494
- n[k] = obj[k];
495
- }
496
- return n;
497
- }, {});
498
- }
499
-
500
507
  function useDebounceFn(fn, ms = 200, options = {}) {
501
508
  return createFilterWrapper(debounceFilter(ms, options), fn);
502
509
  }
package/index.d.ts CHANGED
@@ -1,16 +1,21 @@
1
1
  import * as vue_demi from 'vue-demi';
2
- import { WatchOptionsBase, Ref, WatchSource, ComputedRef, ShallowUnwrapRef as ShallowUnwrapRef$1, WatchOptions, UnwrapRef, ToRefs, WatchCallback, WatchStopHandle } from 'vue-demi';
2
+ import { WatchOptionsBase, Ref, ComputedRef, WritableComputedRef, WatchSource, ComputedGetter, WritableComputedOptions, ShallowUnwrapRef as ShallowUnwrapRef$1, WatchOptions, UnwrapRef, ToRefs, WatchCallback, WatchStopHandle } from 'vue-demi';
3
3
  import { MaybeRef as MaybeRef$1 } from '@vueuse/shared';
4
4
 
5
5
  declare function computedEager<T>(fn: () => T, options?: WatchOptionsBase): Readonly<Ref<T>>;
6
6
 
7
- /**
8
- * Explicitly define the deps of computed.
9
- *
10
- * @param source
11
- * @param fn
12
- */
13
- declare function computedWithControl<T, S>(source: WatchSource<S> | WatchSource<S>[], fn: () => T): ComputedRef<T>;
7
+ interface ComputedWithControlRefExtra {
8
+ /**
9
+ * Force update the computed value.
10
+ */
11
+ trigger(): void;
12
+ }
13
+ interface ComputedRefWithControl<T> extends ComputedRef<T>, ComputedWithControlRefExtra {
14
+ }
15
+ interface WritableComputedRefWithControl<T> extends WritableComputedRef<T>, ComputedWithControlRefExtra {
16
+ }
17
+ declare function computedWithControl<T, S>(source: WatchSource<S> | WatchSource<S>[], fn: ComputedGetter<T>): ComputedRefWithControl<T>;
18
+ declare function computedWithControl<T, S>(source: WatchSource<S> | WatchSource<S>[], fn: WritableComputedOptions<T>): WritableComputedRefWithControl<T>;
14
19
 
15
20
  /**
16
21
  * The source code for this function was inspired by vue-apollo's `useEventHook` util
@@ -858,4 +863,4 @@ declare function watchTriggerable<T extends object, FnReturnT>(source: T, cb: Wa
858
863
  */
859
864
  declare function whenever<T>(source: WatchSource<T | false | null | undefined>, cb: WatchCallback<T>, options?: WatchOptions): vue_demi.WatchStopHandle;
860
865
 
861
- export { Awaitable, ConfigurableEventFilter, ConfigurableFlush, ConfigurableFlushSync, ControlledRefOptions, CreateGlobalStateReturn, DateLike, DebounceFilterOptions, DeepMaybeRef, ElementOf, EventFilter, EventHook, EventHookOff, EventHookOn, EventHookTrigger, ExtendRefOptions, Fn, FunctionArgs, FunctionWrapperOptions, IgnoredUpdater, IntervalFnOptions, IntervalOptions, MapOldSources, MapSources, MaybeComputedRef, MaybeRef, Pausable, Reactify, ReactifyNested, ReactifyObjectOptions, RemovableRef, RemoveableRef, ShallowUnwrapRef, SingletonPromiseReturn, Stopable, Stoppable, SyncRefOptions, SyncRefsOptions, TimeoutFnOptions, TimeoutOptions, UntilArrayInstance, UntilBaseInstance, UntilToMatchOptions, UntilValueInstance, UseCounterOptions, UseDateFormatReturn, UseLastChangedOptions, UseToggleOptions, WatchAtMostOptions, WatchAtMostReturn, WatchDebouncedOptions, WatchIgnorableReturn, WatchPausableReturn, WatchThrottledOptions, WatchTriggerableCallback, WatchTriggerableReturn, WatchWithFilterOptions, __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 };
866
+ export { Awaitable, ComputedRefWithControl, ComputedWithControlRefExtra, ConfigurableEventFilter, ConfigurableFlush, ConfigurableFlushSync, ControlledRefOptions, CreateGlobalStateReturn, DateLike, DebounceFilterOptions, DeepMaybeRef, ElementOf, EventFilter, EventHook, EventHookOff, EventHookOn, EventHookTrigger, ExtendRefOptions, Fn, FunctionArgs, FunctionWrapperOptions, IgnoredUpdater, IntervalFnOptions, IntervalOptions, MapOldSources, MapSources, MaybeComputedRef, MaybeRef, Pausable, Reactify, ReactifyNested, ReactifyObjectOptions, RemovableRef, RemoveableRef, ShallowUnwrapRef, SingletonPromiseReturn, Stopable, Stoppable, SyncRefOptions, SyncRefsOptions, TimeoutFnOptions, TimeoutOptions, UntilArrayInstance, UntilBaseInstance, UntilToMatchOptions, UntilValueInstance, UseCounterOptions, UseDateFormatReturn, UseLastChangedOptions, UseToggleOptions, WatchAtMostOptions, WatchAtMostReturn, WatchDebouncedOptions, WatchIgnorableReturn, WatchPausableReturn, WatchThrottledOptions, WatchTriggerableCallback, WatchTriggerableReturn, WatchWithFilterOptions, WritableComputedRefWithControl, __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 };