@tb-dev/vue 3.1.2 → 3.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.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Andrew Ferreira <andrew.shien2@gmail.com>
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Andrew Ferreira <andrew.shien2@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './composables';
2
1
  export * from './utils';
3
2
  export type * from './types';
3
+ export * from './composables';
package/dist/index.js CHANGED
@@ -1,9 +1,71 @@
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';
4
- import { Mutex } from 'es-toolkit';
5
- import { unwrap, isNil } from '@tb-dev/utils';
6
-
1
+ import { unwrap, isNil } from "@tb-dev/utils";
2
+ import { inject as inject$1, toValue, toRef, ref, readonly, computed, effectScope } from "vue";
3
+ import { watchImmediate, useAsyncState, useLocalStorage, tryOnScopeDispose, onKeyStroke, useSessionStorage, computedAsync, useWindowSize, useElementSize as useElementSize$1 } from "@vueuse/core";
4
+ import { useWindowSize as useWindowSize2 } from "@vueuse/core";
5
+ import { Mutex } from "es-toolkit";
6
+ function getCurrentApp() {
7
+ return unwrap(globalThis.__VUEUTILS__.app, "No active app");
8
+ }
9
+ function setCurrentApp(app) {
10
+ globalThis.__VUEUTILS__.app = app;
11
+ }
12
+ function tryGetCurrentApp() {
13
+ return globalThis.__VUEUTILS__.app;
14
+ }
15
+ function trySetCurrentApp(app) {
16
+ globalThis.__VUEUTILS__.app ??= app;
17
+ }
18
+ function runWithContext(fn) {
19
+ return getCurrentApp().runWithContext(fn);
20
+ }
21
+ function provide(key, value) {
22
+ getCurrentApp().provide(key, value);
23
+ }
24
+ function inject(key) {
25
+ const value = tryInject(key);
26
+ if (typeof value === "undefined") {
27
+ throw new TypeError("Injection failed: value not provided");
28
+ }
29
+ return value;
30
+ }
31
+ function tryInject(key) {
32
+ return runWithContext(() => inject$1(key));
33
+ }
34
+ function tryInjectOrElse(key, fn) {
35
+ let value = tryInject(key);
36
+ if (typeof value === "undefined") {
37
+ value = fn();
38
+ provide(key, value);
39
+ }
40
+ return value;
41
+ }
42
+ function getErrorHandler() {
43
+ return globalThis.__VUEUTILS__.errorHandler;
44
+ }
45
+ function setErrorHandler(fn, app = true) {
46
+ globalThis.__VUEUTILS__.errorHandler = fn;
47
+ if (app) {
48
+ if (app === true) {
49
+ app = getCurrentApp();
50
+ } else {
51
+ trySetCurrentApp(app);
52
+ }
53
+ app.config.errorHandler = (err) => {
54
+ handleError(err, true);
55
+ };
56
+ }
57
+ }
58
+ function handleError(err, rethrow = true) {
59
+ if (globalThis.__VUEUTILS__.errorHandler) {
60
+ void Promise.try(globalThis.__VUEUTILS__.errorHandler, err);
61
+ } else if (rethrow) {
62
+ throw err;
63
+ }
64
+ }
65
+ function maybe(value, fn) {
66
+ const _value = toValue(value);
67
+ return isNil(_value) ? null : fn(_value);
68
+ }
7
69
  const console = {
8
70
  assert,
9
71
  debug,
@@ -36,85 +98,6 @@ function trace(source) {
36
98
  function warn(source) {
37
99
  return watchImmediate(toRef(source), (value) => globalThis.console.warn(value));
38
100
  }
39
-
40
- function create$1() {
41
- let APP = null;
42
- function get() {
43
- return unwrap(APP, "no active app");
44
- }
45
- function tryGet() {
46
- return APP;
47
- }
48
- function set(app) {
49
- APP = app;
50
- }
51
- function trySet(app) {
52
- APP ??= app;
53
- }
54
- return { get, set, tryGet, trySet };
55
- }
56
- const {
57
- get: getCurrentApp,
58
- set: setCurrentApp,
59
- tryGet: tryGetCurrentApp,
60
- trySet: trySetCurrentApp
61
- } = create$1();
62
- function runWithContext(fn) {
63
- return getCurrentApp().runWithContext(fn);
64
- }
65
- function provide(key, value) {
66
- getCurrentApp().provide(key, value);
67
- }
68
- function inject(key) {
69
- const value = tryInject(key);
70
- if (typeof value === "undefined") {
71
- throw new TypeError("injection failed: value not provided");
72
- }
73
- return value;
74
- }
75
- function tryInject(key) {
76
- return runWithContext(() => inject$1(key));
77
- }
78
- function tryInjectOrElse(key, fn) {
79
- let value = tryInject(key);
80
- if (typeof value === "undefined") {
81
- value = fn();
82
- provide(key, value);
83
- }
84
- return value;
85
- }
86
-
87
- function create() {
88
- let ERROR_HANDLER_FN = null;
89
- function get() {
90
- return ERROR_HANDLER_FN;
91
- }
92
- function set(fn, app = true) {
93
- ERROR_HANDLER_FN = fn;
94
- if (app) {
95
- let _app;
96
- if (app === true) {
97
- _app = getCurrentApp();
98
- } else {
99
- trySetCurrentApp(app);
100
- _app = app;
101
- }
102
- _app.config.errorHandler = (err) => {
103
- handle(err, true);
104
- };
105
- }
106
- }
107
- function handle(err, rethrow = true) {
108
- if (ERROR_HANDLER_FN) {
109
- void Promise.try(ERROR_HANDLER_FN, err);
110
- } else if (rethrow) {
111
- throw err;
112
- }
113
- }
114
- return { get, set, handle };
115
- }
116
- const { get: getErrorHandler, set: setErrorHandler, handle: handleError } = create();
117
-
118
101
  function useMutex(options) {
119
102
  const mutex = new Mutex();
120
103
  const locked = ref(mutex.isLocked);
@@ -147,11 +130,9 @@ function useMutex(options) {
147
130
  lock
148
131
  };
149
132
  }
150
-
151
133
  function asyncRef(initial, fn, options = {}) {
152
- const { immediate = true } = options;
153
134
  const value = useAsyncState(fn, initial, {
154
- immediate,
135
+ immediate: options.immediate ?? true,
155
136
  onError: handleError,
156
137
  resetOnExecute: false,
157
138
  shallow: true,
@@ -168,7 +149,6 @@ function asyncRef(initial, fn, options = {}) {
168
149
  execute
169
150
  };
170
151
  }
171
-
172
152
  function localRef(key, initial, options) {
173
153
  const defaultValue = { inner: initial };
174
154
  const local = useLocalStorage(key, defaultValue, {
@@ -188,7 +168,6 @@ function localRef(key, initial, options) {
188
168
  }
189
169
  });
190
170
  }
191
-
192
171
  function onKeyDown(key, handler, options = {}) {
193
172
  const {
194
173
  altKey = false,
@@ -247,7 +226,6 @@ function onShiftKeyDown(key, handler, options) {
247
226
  function onCtrlShiftKeyDown(key, handler, options) {
248
227
  return onKeyDown(key, handler, { ...options, ctrlKey: true, shiftKey: true });
249
228
  }
250
-
251
229
  function sessionRef(key, initial, options) {
252
230
  const defaultValue = { inner: initial };
253
231
  const session = useSessionStorage(key, defaultValue, {
@@ -267,7 +245,6 @@ function sessionRef(key, initial, options) {
267
245
  }
268
246
  });
269
247
  }
270
-
271
248
  function asyncComputed(initial, callback, options) {
272
249
  const state = computedAsync(callback, initial, {
273
250
  onError: handleError,
@@ -277,14 +254,12 @@ function asyncComputed(initial, callback, options) {
277
254
  });
278
255
  return state;
279
256
  }
280
-
281
257
  function useWindowHeight() {
282
258
  return useWindowSize().height;
283
259
  }
284
260
  function useWindowWidth() {
285
261
  return useWindowSize().width;
286
262
  }
287
-
288
263
  function useElementSize(element) {
289
264
  return useElementSize$1(toRef(element), { height: 0, width: 0 }, { box: "border-box" });
290
265
  }
@@ -304,10 +279,45 @@ function useWidthDiff(element, lhs) {
304
279
  const lhsRef = lhs ? toRef(lhs) : useWindowWidth();
305
280
  return computed(() => lhsRef.value - width.value);
306
281
  }
307
-
308
- function maybe(value, fn) {
309
- const _value = toValue(value);
310
- return isNil(_value) ? null : fn(_value);
282
+ if (!Object.hasOwn(globalThis, "__VUEUTILS__")) {
283
+ Object.defineProperty(globalThis, "__VUEUTILS__", {
284
+ configurable: false,
285
+ enumerable: true,
286
+ writable: false,
287
+ value: {}
288
+ });
311
289
  }
312
-
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 };
290
+ export {
291
+ asyncComputed,
292
+ asyncRef,
293
+ console,
294
+ getCurrentApp,
295
+ getErrorHandler,
296
+ handleError,
297
+ inject,
298
+ localRef,
299
+ maybe,
300
+ onAltKeyDown,
301
+ onCtrlKeyDown,
302
+ onCtrlShiftKeyDown,
303
+ onKeyDown,
304
+ onShiftKeyDown,
305
+ provide,
306
+ runWithContext,
307
+ sessionRef,
308
+ setCurrentApp,
309
+ setErrorHandler,
310
+ tryGetCurrentApp,
311
+ tryInject,
312
+ tryInjectOrElse,
313
+ trySetCurrentApp,
314
+ useElementSize,
315
+ useHeight,
316
+ useHeightDiff,
317
+ useMutex,
318
+ useWidth,
319
+ useWidthDiff,
320
+ useWindowHeight,
321
+ useWindowSize2 as useWindowSize,
322
+ useWindowWidth
323
+ };
@@ -1,6 +1,8 @@
1
- import { Option } from '@tb-dev/utils';
2
1
  import { App, InjectionKey } from 'vue';
3
- export declare const getCurrentApp: () => App, setCurrentApp: (app: App) => void, tryGetCurrentApp: () => Option<App>, trySetCurrentApp: (app: App) => void;
2
+ export declare function getCurrentApp(): App<any>;
3
+ export declare function setCurrentApp(app: App): void;
4
+ export declare function tryGetCurrentApp(): import('@tb-dev/utils').Option<App<any>>;
5
+ export declare function trySetCurrentApp(app: App): void;
4
6
  export declare function runWithContext<T>(fn: () => T): T;
5
7
  export declare function provide<T>(key: InjectionKey<T>, value: T): void;
6
8
  export declare function inject<T>(key: InjectionKey<T>): T;
@@ -1,4 +1,6 @@
1
1
  import { App } from 'vue';
2
2
  import { MaybePromise, Option } from '@tb-dev/utils';
3
3
  export type ErrorHandler = (err: unknown) => MaybePromise<void>;
4
- export declare const getErrorHandler: () => Option<ErrorHandler>, setErrorHandler: (fn: ErrorHandler, app?: Option<App | boolean>) => void, handleError: (err: unknown, rethrow?: boolean) => void;
4
+ export declare function getErrorHandler(): Option<ErrorHandler>;
5
+ export declare function setErrorHandler(fn: ErrorHandler, app?: Option<App | boolean>): void;
6
+ export declare function handleError(err: unknown, rethrow?: boolean): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/vue",
3
- "version": "3.1.2",
3
+ "version": "3.2.1",
4
4
  "description": "Vue utilities",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -24,16 +24,15 @@
24
24
  "composables"
25
25
  ],
26
26
  "dependencies": {
27
- "@tb-dev/utils": "^7.0.11",
28
- "@vueuse/core": "^13.6.0",
29
- "es-toolkit": "^1.39.9",
27
+ "@tb-dev/utils": "^7.0.12",
28
+ "@vueuse/core": "^13.7.0",
29
+ "es-toolkit": "^1.39.10",
30
30
  "vue": "^3.5.18"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@tb-dev/eslint-config": "^8.0.5",
34
- "@types/node": "^24.2.1",
34
+ "@types/node": "^24.3.0",
35
35
  "eslint": "^9.33.0",
36
- "prettier": "^3.6.2",
37
36
  "tslib": "^2.8.1",
38
37
  "typedoc": "^0.28.10",
39
38
  "typedoc-plugin-mdn-links": "^5.0.8",
@@ -55,7 +54,7 @@
55
54
  "scripts": {
56
55
  "build": "vite build",
57
56
  "docs": "typedoc --plugin typedoc-plugin-mdn-links",
58
- "format": "prettier . --write",
57
+ "format": "dprint fmt",
59
58
  "lint": "eslint . --config eslint.config.js",
60
59
  "release": "pnpm run build && pnpm publish",
61
60
  "type-check": "tsc --noEmit",