@tb-dev/vue 2.1.0 → 2.2.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.
@@ -1,5 +1,9 @@
1
- export declare function useMutex(): {
1
+ export interface UseMutexOptions {
2
+ onError?: ((err: unknown) => void) | boolean;
3
+ }
4
+ export declare function useMutex(options?: UseMutexOptions): {
2
5
  locked: Readonly<import('vue').Ref<boolean, boolean>>;
3
6
  acquire: () => Promise<void>;
4
7
  release: () => void;
8
+ lock: <T = unknown>(fn: () => Promise<T>) => Promise<void | T>;
5
9
  };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { watch, ref, readonly, inject as inject$1, computed, effectScope, toValue, toRef } from 'vue';
1
+ import { watch, inject as inject$1, ref, readonly, computed, effectScope, toValue, toRef } from 'vue';
2
2
  import { Mutex } from 'es-toolkit';
3
3
  import { unwrap, isNil } from '@tb-dev/utils';
4
4
  import { useAsyncState, useLocalStorage, tryOnScopeDispose, onKeyStroke, useSessionStorage, computedAsync, useWindowSize, useElementSize as useElementSize$1 } from '@vueuse/core';
@@ -11,24 +11,6 @@ function warn(source) {
11
11
  return watch(source, (value) => console.warn(value));
12
12
  }
13
13
 
14
- function useMutex() {
15
- const mutex = new Mutex();
16
- const locked = ref(mutex.isLocked);
17
- async function acquire() {
18
- await mutex.acquire();
19
- locked.value = mutex.isLocked;
20
- }
21
- function release() {
22
- mutex.release();
23
- locked.value = mutex.isLocked;
24
- }
25
- return {
26
- locked: readonly(locked),
27
- acquire,
28
- release
29
- };
30
- }
31
-
32
14
  function create$1() {
33
15
  let APP = null;
34
16
  function get() {
@@ -107,6 +89,39 @@ function create() {
107
89
  }
108
90
  const { get: getErrorHandler, set: setErrorHandler, handle: handleError } = create();
109
91
 
92
+ function useMutex(options) {
93
+ const mutex = new Mutex();
94
+ const locked = ref(mutex.isLocked);
95
+ let onError;
96
+ if (typeof options?.onError === "function") {
97
+ onError = options.onError;
98
+ } else if (options?.onError !== false) {
99
+ onError = handleError;
100
+ }
101
+ async function acquire() {
102
+ await mutex.acquire();
103
+ locked.value = mutex.isLocked;
104
+ }
105
+ function release() {
106
+ mutex.release();
107
+ locked.value = mutex.isLocked;
108
+ }
109
+ function lock(fn) {
110
+ const { resolve, reject, promise } = Promise.withResolvers();
111
+ acquire().then(() => fn()).then((value) => resolve(value)).catch(reject).finally(release);
112
+ if (onError) {
113
+ return promise.catch(onError);
114
+ }
115
+ return promise;
116
+ }
117
+ return {
118
+ locked: readonly(locked),
119
+ acquire,
120
+ release,
121
+ lock
122
+ };
123
+ }
124
+
110
125
  function asyncRef(initial, fn, options = {}) {
111
126
  const { immediate = true } = options;
112
127
  const value = useAsyncState(fn, initial, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/vue",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "description": "Vue utilities",
5
5
  "license": "MIT",
6
6
  "type": "module",