@tb-dev/vue 2.2.0 → 3.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.
@@ -1,3 +1,18 @@
1
- import { WatchSource } from 'vue';
2
- export declare function log<T>(source: WatchSource<T>): import('vue').WatchHandle;
3
- export declare function warn<T>(source: WatchSource<T>): import('vue').WatchHandle;
1
+ import { MaybeNilRef } from '../../types';
2
+ export declare const console: {
3
+ readonly assert: typeof assert;
4
+ readonly debug: typeof debug;
5
+ readonly error: typeof error;
6
+ readonly info: typeof info;
7
+ readonly log: typeof log;
8
+ readonly trace: typeof trace;
9
+ readonly warn: typeof warn;
10
+ };
11
+ declare function assert(cond: MaybeNilRef<boolean>, message: string): import('vue').WatchStopHandle;
12
+ declare function debug<T>(source: MaybeNilRef<T>): import('vue').WatchStopHandle;
13
+ declare function error<T>(source: MaybeNilRef<T>): import('vue').WatchStopHandle;
14
+ declare function info<T>(source: MaybeNilRef<T>): import('vue').WatchStopHandle;
15
+ declare function log<T>(source: MaybeNilRef<T>): import('vue').WatchStopHandle;
16
+ declare function trace<T>(source: MaybeNilRef<T>): import('vue').WatchStopHandle;
17
+ declare function warn<T>(source: MaybeNilRef<T>): import('vue').WatchStopHandle;
18
+ export {};
@@ -1,6 +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;
5
- lock: <T = unknown>(fn: () => Promise<T>) => Promise<T>;
8
+ lock: <T = unknown>(fn: () => Promise<T>) => Promise<void | T>;
6
9
  };
package/dist/index.js CHANGED
@@ -1,38 +1,40 @@
1
- import { watch, ref, readonly, inject as inject$1, computed, effectScope, toValue, toRef } from 'vue';
1
+ import { toRef, inject as inject$1, ref, readonly, computed, effectScope, toValue } from 'vue';
2
+ import { watchImmediate, useAsyncState, useLocalStorage, tryOnScopeDispose, onKeyStroke, useSessionStorage, computedAsync, useWindowSize, useElementSize as useElementSize$1 } from '@vueuse/core';
3
+ export { useWindowSize } from '@vueuse/core';
2
4
  import { Mutex } from 'es-toolkit';
3
5
  import { unwrap, isNil } from '@tb-dev/utils';
4
- import { useAsyncState, useLocalStorage, tryOnScopeDispose, onKeyStroke, useSessionStorage, computedAsync, useWindowSize, useElementSize as useElementSize$1 } from '@vueuse/core';
5
- export { useWindowSize } from '@vueuse/core';
6
6
 
7
+ const console = {
8
+ assert,
9
+ debug,
10
+ error,
11
+ info,
12
+ log,
13
+ trace,
14
+ warn
15
+ };
16
+ function assert(cond, message) {
17
+ return watchImmediate(toRef(cond), (value) => {
18
+ globalThis.console.assert(value ?? false, message);
19
+ });
20
+ }
21
+ function debug(source) {
22
+ return watchImmediate(toRef(source), (value) => globalThis.console.debug(value));
23
+ }
24
+ function error(source) {
25
+ return watchImmediate(toRef(source), (value) => globalThis.console.error(value));
26
+ }
27
+ function info(source) {
28
+ return watchImmediate(toRef(source), (value) => globalThis.console.info(value));
29
+ }
7
30
  function log(source) {
8
- return watch(source, (value) => console.log(value));
31
+ return watchImmediate(toRef(source), (value) => globalThis.console.log(value));
9
32
  }
10
- function warn(source) {
11
- return watch(source, (value) => console.warn(value));
33
+ function trace(source) {
34
+ return watchImmediate(toRef(source), (value) => globalThis.console.trace(value));
12
35
  }
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
- function lock(fn) {
26
- const { resolve, reject, promise } = Promise.withResolvers();
27
- acquire().then(() => fn()).then((value) => resolve(value)).catch(reject).finally(release);
28
- return promise;
29
- }
30
- return {
31
- locked: readonly(locked),
32
- acquire,
33
- release,
34
- lock
35
- };
36
+ function warn(source) {
37
+ return watchImmediate(toRef(source), (value) => globalThis.console.warn(value));
36
38
  }
37
39
 
38
40
  function create$1() {
@@ -113,6 +115,39 @@ function create() {
113
115
  }
114
116
  const { get: getErrorHandler, set: setErrorHandler, handle: handleError } = create();
115
117
 
118
+ function useMutex(options) {
119
+ const mutex = new Mutex();
120
+ const locked = ref(mutex.isLocked);
121
+ let onError;
122
+ if (typeof options?.onError === "function") {
123
+ onError = options.onError;
124
+ } else if (options?.onError !== false) {
125
+ onError = handleError;
126
+ }
127
+ async function acquire() {
128
+ await mutex.acquire();
129
+ locked.value = mutex.isLocked;
130
+ }
131
+ function release() {
132
+ mutex.release();
133
+ locked.value = mutex.isLocked;
134
+ }
135
+ function lock(fn) {
136
+ const { resolve, reject, promise } = Promise.withResolvers();
137
+ acquire().then(() => fn()).then((value) => resolve(value)).catch(reject).finally(release);
138
+ if (onError) {
139
+ return promise.catch(onError);
140
+ }
141
+ return promise;
142
+ }
143
+ return {
144
+ locked: readonly(locked),
145
+ acquire,
146
+ release,
147
+ lock
148
+ };
149
+ }
150
+
116
151
  function asyncRef(initial, fn, options = {}) {
117
152
  const { immediate = true } = options;
118
153
  const value = useAsyncState(fn, initial, {
@@ -275,4 +310,4 @@ function maybe(value, fn) {
275
310
  return isNil(_value) ? null : fn(_value);
276
311
  }
277
312
 
278
- export { asyncComputed, asyncRef, getCurrentApp, getErrorHandler, handleError, inject, localRef, log, maybe, onAltKeyDown, onCtrlKeyDown, onCtrlShiftKeyDown, onKeyDown, onShiftKeyDown, provide, runWithContext, sessionRef, setCurrentApp, setErrorHandler, tryGetCurrentApp, tryInject, tryInjectOrElse, trySetCurrentApp, useElementSize, useHeight, useHeightDiff, useMutex, useWidth, useWidthDiff, useWindowHeight, useWindowWidth, warn };
313
+ export { asyncComputed, asyncRef, console, getCurrentApp, getErrorHandler, handleError, inject, localRef, maybe, onAltKeyDown, onCtrlKeyDown, onCtrlShiftKeyDown, onKeyDown, onShiftKeyDown, provide, runWithContext, sessionRef, setCurrentApp, setErrorHandler, tryGetCurrentApp, tryInject, tryInjectOrElse, trySetCurrentApp, useElementSize, useHeight, useHeightDiff, useMutex, useWidth, useWidthDiff, useWindowHeight, useWindowWidth };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/vue",
3
- "version": "2.2.0",
3
+ "version": "3.0.0",
4
4
  "description": "Vue utilities",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -26,13 +26,13 @@
26
26
  "dependencies": {
27
27
  "@tb-dev/utils": "^7.0.8",
28
28
  "@vueuse/core": "^13.5.0",
29
- "es-toolkit": "^1.39.7",
29
+ "es-toolkit": "^1.39.8",
30
30
  "vue": "^3.5.18"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@tb-dev/eslint-config": "^8.0.3",
34
34
  "@types/node": "^24.1.0",
35
- "eslint": "^9.31.0",
35
+ "eslint": "^9.32.0",
36
36
  "prettier": "^3.6.2",
37
37
  "tslib": "^2.8.1",
38
38
  "typedoc": "^0.28.7",