@reactive-vscode/vueuse 0.4.1 → 1.0.0-beta.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 CHANGED
@@ -4,6 +4,18 @@ const reactivity = require("@reactive-vscode/reactivity");
4
4
  function getCurrentInstance() {
5
5
  return null;
6
6
  }
7
+ function computedEager(fn, options) {
8
+ var _options$flush;
9
+ const result = reactivity.shallowRef();
10
+ reactivity.watchEffect(() => {
11
+ result.value = fn();
12
+ }, {
13
+ ...options,
14
+ flush: (_options$flush = options === null || options === void 0 ? void 0 : options.flush) !== null && _options$flush !== void 0 ? _options$flush : "sync"
15
+ });
16
+ return reactivity.readonly(result);
17
+ }
18
+ const eagerComputed = computedEager;
7
19
  function computedWithControl(source, fn, options = {}) {
8
20
  let v = void 0;
9
21
  let track;
@@ -39,6 +51,7 @@ function computedWithControl(source, fn, options = {}) {
39
51
  result.trigger = update;
40
52
  return result;
41
53
  }
54
+ const controlledComputed = computedWithControl;
42
55
  function tryOnScopeDispose(fn, failSilently) {
43
56
  if (reactivity.getCurrentScope()) {
44
57
  reactivity.onScopeDispose(fn, failSilently);
@@ -84,6 +97,38 @@ function createGlobalState(stateFactory) {
84
97
  return state;
85
98
  });
86
99
  }
100
+ const localProvidedStateMap = /* @__PURE__ */ new WeakMap();
101
+ const injectLocal = /* @__NO_SIDE_EFFECTS__ */ (...args) => {
102
+ var _getCurrentInstance;
103
+ const key = args[0];
104
+ const instance = (_getCurrentInstance = getCurrentInstance()) === null || _getCurrentInstance === void 0 ? void 0 : _getCurrentInstance.proxy;
105
+ const owner = instance !== null && instance !== void 0 ? instance : reactivity.getCurrentScope();
106
+ if (owner == null && !reactivity.hasInjectionContext()) throw new Error("injectLocal must be called in setup");
107
+ if (owner && localProvidedStateMap.has(owner) && key in localProvidedStateMap.get(owner)) return localProvidedStateMap.get(owner)[key];
108
+ return reactivity.inject(...args);
109
+ };
110
+ function provideLocal(key, value) {
111
+ var _getCurrentInstance;
112
+ const instance = (_getCurrentInstance = getCurrentInstance()) === null || _getCurrentInstance === void 0 ? void 0 : _getCurrentInstance.proxy;
113
+ const owner = instance !== null && instance !== void 0 ? instance : reactivity.getCurrentScope();
114
+ if (owner == null) throw new Error("provideLocal must be called in setup");
115
+ if (!localProvidedStateMap.has(owner)) localProvidedStateMap.set(owner, /* @__PURE__ */ Object.create(null));
116
+ const localProvidedState = localProvidedStateMap.get(owner);
117
+ localProvidedState[key] = value;
118
+ return reactivity.provide(key, value);
119
+ }
120
+ // @__NO_SIDE_EFFECTS__
121
+ function createInjectionState(composable, options) {
122
+ const key = (options === null || options === void 0 ? void 0 : options.injectionKey) || Symbol(composable.name || "InjectionState");
123
+ const defaultValue = options === null || options === void 0 ? void 0 : options.defaultValue;
124
+ const useProvidingState = (...args) => {
125
+ const state = composable(...args);
126
+ provideLocal(key, state);
127
+ return state;
128
+ };
129
+ const useInjectedState = () => /* @__PURE__ */ injectLocal(key, defaultValue);
130
+ return [useProvidingState, useInjectedState];
131
+ }
87
132
  // @__NO_SIDE_EFFECTS__
88
133
  function createRef(value, deep) {
89
134
  if (deep === true) return reactivity.ref(value);
@@ -275,6 +320,9 @@ function increaseWithUnit(target, delta) {
275
320
  if (Number.isNaN(result)) return target;
276
321
  return result + unit;
277
322
  }
323
+ function pxValue(px) {
324
+ return px.endsWith("rem") ? Number.parseFloat(px) * 16 : Number.parseFloat(px);
325
+ }
278
326
  function objectPick(obj, keys, omitUndefined = false) {
279
327
  return keys.reduce((n, k) => {
280
328
  if (k in obj) {
@@ -383,6 +431,7 @@ function reactify(fn, options) {
383
431
  return reactivity.computed(() => fn.apply(this, args.map((i) => unrefFn(i))));
384
432
  };
385
433
  }
434
+ const createReactiveFn = reactify;
386
435
  // @__NO_SIDE_EFFECTS__
387
436
  function reactifyObject(obj, optionsOrKeys = {}) {
388
437
  let keys = [];
@@ -465,6 +514,7 @@ function refAutoReset(defaultValue, afterMs = 1e4) {
465
514
  };
466
515
  });
467
516
  }
517
+ const autoResetRef = refAutoReset;
468
518
  // @__NO_SIDE_EFFECTS__
469
519
  function useDebounceFn(fn, ms = 200, options = {}) {
470
520
  return createFilterWrapper(debounceFilter(ms, options), fn);
@@ -477,6 +527,8 @@ function refDebounced(value, ms = 200, options = {}) {
477
527
  reactivity.watch(value, () => updater());
478
528
  return reactivity.shallowReadonly(debounced);
479
529
  }
530
+ const debouncedRef = refDebounced;
531
+ const useDebounce = refDebounced;
480
532
  // @__NO_SIDE_EFFECTS__
481
533
  function refDefault(source, defaultValue) {
482
534
  return reactivity.computed({
@@ -489,6 +541,29 @@ function refDefault(source, defaultValue) {
489
541
  }
490
542
  });
491
543
  }
544
+ function refManualReset(defaultValue) {
545
+ let value = reactivity.toValue(defaultValue);
546
+ let trigger;
547
+ const reset = () => {
548
+ value = reactivity.toValue(defaultValue);
549
+ trigger();
550
+ };
551
+ const refValue = reactivity.customRef((track, _trigger) => {
552
+ trigger = _trigger;
553
+ return {
554
+ get() {
555
+ track();
556
+ return value;
557
+ },
558
+ set(newValue) {
559
+ value = newValue;
560
+ trigger();
561
+ }
562
+ };
563
+ });
564
+ refValue.reset = reset;
565
+ return refValue;
566
+ }
492
567
  // @__NO_SIDE_EFFECTS__
493
568
  function useThrottleFn(fn, ms = 200, trailing = false, leading = true, rejectOnCancel = false) {
494
569
  return createFilterWrapper(throttleFilter(ms, trailing, leading, rejectOnCancel), fn);
@@ -502,6 +577,8 @@ function refThrottled(value, delay = 200, trailing = true, leading = true) {
502
577
  reactivity.watch(value, () => updater());
503
578
  return throttled;
504
579
  }
580
+ const throttledRef = refThrottled;
581
+ const useThrottle = refThrottled;
505
582
  // @__NO_SIDE_EFFECTS__
506
583
  function refWithControl(initial, options = {}) {
507
584
  let source = initial;
@@ -637,11 +714,22 @@ function toRefs(objectRef, options = {}) {
637
714
  }));
638
715
  return result;
639
716
  }
717
+ function tryOnBeforeMount(fn, sync = true, target) {
718
+ if (getLifeCycleTarget(target)) reactivity.onBeforeMount(fn, target);
719
+ else if (sync) fn();
720
+ else reactivity.nextTick(fn);
721
+ }
722
+ function tryOnBeforeUnmount(fn, target) {
723
+ if (getLifeCycleTarget(target)) reactivity.onBeforeUnmount(fn, target);
724
+ }
640
725
  function tryOnMounted(fn, sync = true, target) {
641
726
  if (getLifeCycleTarget(target)) ;
642
727
  else if (sync) fn();
643
728
  else reactivity.nextTick(fn);
644
729
  }
730
+ function tryOnUnmounted(fn, target) {
731
+ if (getLifeCycleTarget(target)) reactivity.onUnmounted(fn, target);
732
+ }
645
733
  function createUntil(r, isNot = false) {
646
734
  function toMatch(condition, { flush = "sync", deep = false, timeout, throwOnTimeout } = {}) {
647
735
  let stop = null;
@@ -1130,6 +1218,7 @@ function watchDebounced(source, cb, options = {}) {
1130
1218
  eventFilter: debounceFilter(debounce, { maxWait })
1131
1219
  });
1132
1220
  }
1221
+ const debouncedWatch = watchDebounced;
1133
1222
  function watchDeep(source, cb, options) {
1134
1223
  return reactivity.watch(source, cb, {
1135
1224
  ...options,
@@ -1189,6 +1278,7 @@ function watchIgnorable(source, cb, options = {}) {
1189
1278
  ignorePrevAsyncUpdates
1190
1279
  };
1191
1280
  }
1281
+ const ignorableWatch = watchIgnorable;
1192
1282
  function watchImmediate(source, cb, options) {
1193
1283
  return reactivity.watch(source, cb, {
1194
1284
  ...options,
@@ -1208,6 +1298,7 @@ function watchThrottled(source, cb, options = {}) {
1208
1298
  eventFilter: throttleFilter(throttle, trailing, leading)
1209
1299
  });
1210
1300
  }
1301
+ const throttledWatch = watchThrottled;
1211
1302
  function watchTriggerable(source, cb, options = {}) {
1212
1303
  let cleanupFn;
1213
1304
  function onEffect() {
@@ -1295,6 +1386,7 @@ function computedAsync(evaluationCallback, initialState, optionsOrRef) {
1295
1386
  });
1296
1387
  else return current;
1297
1388
  }
1389
+ const asyncComputed = computedAsync;
1298
1390
  // @__NO_SIDE_EFFECTS__
1299
1391
  function createUnrefFn(fn) {
1300
1392
  return function(...args) {
@@ -1308,11 +1400,6 @@ function unrefElement(elRef) {
1308
1400
  return (_$el = plain === null || plain === void 0 ? void 0 : plain.$el) !== null && _$el !== void 0 ? _$el : plain;
1309
1401
  }
1310
1402
  function useEventListener(...args) {
1311
- const cleanups = [];
1312
- const cleanup = () => {
1313
- cleanups.forEach((fn) => fn());
1314
- cleanups.length = 0;
1315
- };
1316
1403
  const register = (el, event, listener, options) => {
1317
1404
  el.addEventListener(event, listener, options);
1318
1405
  return () => el.removeEventListener(event, listener, options);
@@ -1321,7 +1408,7 @@ function useEventListener(...args) {
1321
1408
  const test = toArray(reactivity.toValue(args[0])).filter((e) => e != null);
1322
1409
  return test.every((e) => typeof e !== "string") ? test : void 0;
1323
1410
  });
1324
- const stopWatch = watchImmediate(() => {
1411
+ return watchImmediate(() => {
1325
1412
  var _firstParamTargets$va, _firstParamTargets$va2;
1326
1413
  return [
1327
1414
  (_firstParamTargets$va = (_firstParamTargets$va2 = firstParamTargets.value) === null || _firstParamTargets$va2 === void 0 ? void 0 : _firstParamTargets$va2.map((e) => unrefElement(e))) !== null && _firstParamTargets$va !== void 0 ? _firstParamTargets$va : [defaultWindow].filter((e) => e != null),
@@ -1329,18 +1416,14 @@ function useEventListener(...args) {
1329
1416
  toArray(reactivity.unref(firstParamTargets.value ? args[2] : args[1])),
1330
1417
  reactivity.toValue(firstParamTargets.value ? args[3] : args[2])
1331
1418
  ];
1332
- }, ([raw_targets, raw_events, raw_listeners, raw_options]) => {
1333
- cleanup();
1419
+ }, ([raw_targets, raw_events, raw_listeners, raw_options], _, onCleanup) => {
1334
1420
  if (!(raw_targets === null || raw_targets === void 0 ? void 0 : raw_targets.length) || !(raw_events === null || raw_events === void 0 ? void 0 : raw_events.length) || !(raw_listeners === null || raw_listeners === void 0 ? void 0 : raw_listeners.length)) return;
1335
1421
  const optionsClone = isObject(raw_options) ? { ...raw_options } : raw_options;
1336
- cleanups.push(...raw_targets.flatMap((el) => raw_events.flatMap((event) => raw_listeners.map((listener) => register(el, event, listener, optionsClone)))));
1422
+ const cleanups = raw_targets.flatMap((el) => raw_events.flatMap((event) => raw_listeners.map((listener) => register(el, event, listener, optionsClone))));
1423
+ onCleanup(() => {
1424
+ cleanups.forEach((fn) => fn());
1425
+ });
1337
1426
  }, { flush: "post" });
1338
- const stop = () => {
1339
- stopWatch();
1340
- cleanup();
1341
- };
1342
- tryOnScopeDispose(cleanup);
1343
- return stop;
1344
1427
  }
1345
1428
  // @__NO_SIDE_EFFECTS__
1346
1429
  function useMounted() {
@@ -1455,6 +1538,7 @@ function useAsyncQueue(tasks, options) {
1455
1538
  }
1456
1539
  updateResult(promiseState.rejected, e);
1457
1540
  onError();
1541
+ if (activeIndex.value === tasks.length - 1) onFinished();
1458
1542
  return e;
1459
1543
  });
1460
1544
  }, Promise.resolve());
@@ -2710,7 +2794,8 @@ function useWebSocket(url, options = {}) {
2710
2794
  const { retries = -1, delay = 1e3, onFailed } = resolveNestedOptions(options.autoReconnect);
2711
2795
  if ((typeof retries === "function" ? retries : () => typeof retries === "number" && (retries < 0 || retried < retries))(retried)) {
2712
2796
  retried += 1;
2713
- retryTimeout = setTimeout(_init, delay);
2797
+ const delayTime = typeof delay === "function" ? delay(retried) : delay;
2798
+ retryTimeout = setTimeout(_init, delayTime);
2714
2799
  } else onFailed === null || onFailed === void 0 || onFailed();
2715
2800
  }
2716
2801
  };
@@ -2862,41 +2947,33 @@ function useWebWorkerFn(fn, options = {}) {
2862
2947
  workerTerminate
2863
2948
  };
2864
2949
  }
2865
- Object.defineProperty(exports, "resolveRef", {
2866
- enumerable: true,
2867
- get: () => reactivity.toRef
2868
- });
2869
- Object.defineProperty(exports, "resolveUnref", {
2870
- enumerable: true,
2871
- get: () => reactivity.toValue
2872
- });
2873
- Object.defineProperty(exports, "toValue", {
2874
- enumerable: true,
2875
- get: () => reactivity.toValue
2876
- });
2877
2950
  exports.assert = assert;
2878
- exports.asyncComputed = computedAsync;
2879
- exports.autoResetRef = refAutoReset;
2951
+ exports.asyncComputed = asyncComputed;
2952
+ exports.autoResetRef = autoResetRef;
2880
2953
  exports.bypassFilter = bypassFilter;
2881
2954
  exports.camelize = camelize;
2882
2955
  exports.clamp = clamp;
2883
2956
  exports.cloneFnJSON = cloneFnJSON;
2884
2957
  exports.computedAsync = computedAsync;
2958
+ exports.computedEager = computedEager;
2885
2959
  exports.computedWithControl = computedWithControl;
2886
2960
  exports.containsProp = containsProp;
2887
- exports.controlledComputed = computedWithControl;
2961
+ exports.controlledComputed = controlledComputed;
2888
2962
  exports.controlledRef = controlledRef;
2889
2963
  exports.createEventHook = createEventHook;
2890
2964
  exports.createFetch = createFetch;
2891
2965
  exports.createFilterWrapper = createFilterWrapper;
2892
2966
  exports.createGlobalState = createGlobalState;
2893
- exports.createReactiveFn = reactify;
2967
+ exports.createInjectionState = createInjectionState;
2968
+ exports.createReactiveFn = createReactiveFn;
2969
+ exports.createRef = createRef;
2894
2970
  exports.createSharedComposable = createSharedComposable;
2895
2971
  exports.createSingletonPromise = createSingletonPromise;
2896
2972
  exports.createUnrefFn = createUnrefFn;
2897
2973
  exports.debounceFilter = debounceFilter;
2898
- exports.debouncedRef = refDebounced;
2899
- exports.debouncedWatch = watchDebounced;
2974
+ exports.debouncedRef = debouncedRef;
2975
+ exports.debouncedWatch = debouncedWatch;
2976
+ exports.eagerComputed = eagerComputed;
2900
2977
  exports.extendRef = extendRef;
2901
2978
  exports.formatDate = formatDate;
2902
2979
  exports.formatTimeAgo = formatTimeAgo;
@@ -2905,8 +2982,9 @@ exports.getLifeCycleTarget = getLifeCycleTarget;
2905
2982
  exports.hasOwn = hasOwn;
2906
2983
  exports.hyphenate = hyphenate;
2907
2984
  exports.identity = identity;
2908
- exports.ignorableWatch = watchIgnorable;
2985
+ exports.ignorableWatch = ignorableWatch;
2909
2986
  exports.increaseWithUnit = increaseWithUnit;
2987
+ exports.injectLocal = injectLocal;
2910
2988
  exports.invoke = invoke;
2911
2989
  exports.isDef = isDef;
2912
2990
  exports.isDefined = isDefined;
@@ -2920,8 +2998,10 @@ exports.objectEntries = objectEntries;
2920
2998
  exports.objectOmit = objectOmit;
2921
2999
  exports.objectPick = objectPick;
2922
3000
  exports.pausableFilter = pausableFilter;
2923
- exports.pausableWatch = watchPausable;
3001
+ exports.pausableWatch = pausableWatch;
2924
3002
  exports.promiseTimeout = promiseTimeout;
3003
+ exports.provideLocal = provideLocal;
3004
+ exports.pxValue = pxValue;
2925
3005
  exports.rand = rand;
2926
3006
  exports.reactify = reactify;
2927
3007
  exports.reactifyObject = reactifyObject;
@@ -2931,18 +3011,25 @@ exports.reactivePick = reactivePick;
2931
3011
  exports.refAutoReset = refAutoReset;
2932
3012
  exports.refDebounced = refDebounced;
2933
3013
  exports.refDefault = refDefault;
3014
+ exports.refManualReset = refManualReset;
2934
3015
  exports.refThrottled = refThrottled;
2935
3016
  exports.refWithControl = refWithControl;
2936
3017
  exports.set = set;
2937
3018
  exports.syncRef = syncRef;
2938
3019
  exports.syncRefs = syncRefs;
2939
3020
  exports.throttleFilter = throttleFilter;
2940
- exports.throttledRef = refThrottled;
2941
- exports.throttledWatch = watchThrottled;
3021
+ exports.throttledRef = throttledRef;
3022
+ exports.throttledWatch = throttledWatch;
3023
+ exports.timestamp = timestamp;
3024
+ exports.toArray = toArray;
2942
3025
  exports.toReactive = toReactive;
2943
3026
  exports.toRef = toRef;
2944
3027
  exports.toRefs = toRefs;
3028
+ exports.tryOnBeforeMount = tryOnBeforeMount;
3029
+ exports.tryOnBeforeUnmount = tryOnBeforeUnmount;
3030
+ exports.tryOnMounted = tryOnMounted;
2945
3031
  exports.tryOnScopeDispose = tryOnScopeDispose;
3032
+ exports.tryOnUnmounted = tryOnUnmounted;
2946
3033
  exports.until = until;
2947
3034
  exports.useArrayDifference = useArrayDifference;
2948
3035
  exports.useArrayEvery = useArrayEvery;
@@ -2965,7 +3052,7 @@ exports.useCloned = useCloned;
2965
3052
  exports.useCounter = useCounter;
2966
3053
  exports.useCycleList = useCycleList;
2967
3054
  exports.useDateFormat = useDateFormat;
2968
- exports.useDebounce = refDebounced;
3055
+ exports.useDebounce = useDebounce;
2969
3056
  exports.useDebounceFn = useDebounceFn;
2970
3057
  exports.useDebouncedRefHistory = useDebouncedRefHistory;
2971
3058
  exports.useEventBus = useEventBus;
@@ -2982,7 +3069,7 @@ exports.usePrevious = usePrevious;
2982
3069
  exports.useRefHistory = useRefHistory;
2983
3070
  exports.useSorted = useSorted;
2984
3071
  exports.useStepper = useStepper;
2985
- exports.useThrottle = refThrottled;
3072
+ exports.useThrottle = useThrottle;
2986
3073
  exports.useThrottleFn = useThrottleFn;
2987
3074
  exports.useThrottledRefHistory = useThrottledRefHistory;
2988
3075
  exports.useTimeAgo = useTimeAgo;