@tb-dev/vue 4.0.5 → 4.0.7

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.
Files changed (2) hide show
  1. package/dist/index.js +224 -235
  2. package/package.json +8 -8
package/dist/index.js CHANGED
@@ -1,303 +1,292 @@
1
- import { unwrap, isNil } from '@tb-dev/utils';
2
- import { inject as inject$1, toValue, toRef, ref, readonly, effectScope, computed } from 'vue';
3
- import { watchImmediate, useAsyncState, useLocalStorage, tryOnScopeDispose, onKeyStroke, useSessionStorage, computedAsync, useWindowSize, useElementSize as useElementSize$1, useBreakpoints as useBreakpoints$1, breakpointsTailwind } from '@vueuse/core';
4
- export { useWindowSize } from '@vueuse/core';
5
- import { Mutex } from 'es-toolkit';
6
-
1
+ import { isNil, unwrap } from "@tb-dev/utils";
2
+ import { computed, effectScope, inject as inject$1, readonly, ref, toRef, toValue } from "vue";
3
+ import { breakpointsTailwind, computedAsync, onKeyStroke, tryOnScopeDispose, useAsyncState, useBreakpoints as useBreakpoints$1, useElementSize as useElementSize$1, useLocalStorage, useSessionStorage, useWindowSize, watchImmediate } from "@vueuse/core";
4
+ import { Mutex } from "es-toolkit";
5
+ //#region src/utils/app.ts
7
6
  function getCurrentApp() {
8
- return unwrap(globalThis.__VUEUTILS__.app, "No active app");
7
+ return unwrap(globalThis.__VUEUTILS__.app, "No active app");
9
8
  }
10
9
  function setCurrentApp(app) {
11
- globalThis.__VUEUTILS__.app = app;
10
+ globalThis.__VUEUTILS__.app = app;
12
11
  }
13
12
  function tryGetCurrentApp() {
14
- return globalThis.__VUEUTILS__.app;
13
+ return globalThis.__VUEUTILS__.app;
15
14
  }
16
15
  function trySetCurrentApp(app) {
17
- globalThis.__VUEUTILS__.app ??= app;
16
+ globalThis.__VUEUTILS__.app ??= app;
18
17
  }
19
18
  function runWithContext(fn) {
20
- return getCurrentApp().runWithContext(fn);
19
+ return getCurrentApp().runWithContext(fn);
21
20
  }
22
21
  function provide(key, value) {
23
- getCurrentApp().provide(key, value);
22
+ getCurrentApp().provide(key, value);
24
23
  }
25
24
  function inject(key) {
26
- const value = tryInject(key);
27
- if (typeof value === "undefined") {
28
- throw new TypeError("Injection failed: value not provided");
29
- }
30
- return value;
25
+ const value = tryInject(key);
26
+ if (typeof value === "undefined") throw new TypeError("Injection failed: value not provided");
27
+ return value;
31
28
  }
32
29
  function tryInject(key) {
33
- return runWithContext(() => inject$1(key));
30
+ return runWithContext(() => inject$1(key));
34
31
  }
35
32
  function tryInjectOrElse(key, fn) {
36
- let value = tryInject(key);
37
- if (typeof value === "undefined") {
38
- value = fn();
39
- provide(key, value);
40
- }
41
- return value;
42
- }
43
-
33
+ let value = tryInject(key);
34
+ if (typeof value === "undefined") {
35
+ value = fn();
36
+ provide(key, value);
37
+ }
38
+ return value;
39
+ }
40
+ //#endregion
41
+ //#region src/utils/error.ts
44
42
  function getErrorHandler() {
45
- return globalThis.__VUEUTILS__.errorHandler;
43
+ return globalThis.__VUEUTILS__.errorHandler;
46
44
  }
47
45
  function setErrorHandler(fn, app = true) {
48
- globalThis.__VUEUTILS__.errorHandler = fn;
49
- if (app) {
50
- if (app === true) {
51
- app = getCurrentApp();
52
- } else {
53
- trySetCurrentApp(app);
54
- }
55
- app.config.errorHandler = (err) => {
56
- handleError(err, true);
57
- };
58
- }
46
+ globalThis.__VUEUTILS__.errorHandler = fn;
47
+ if (app) {
48
+ if (app === true) app = getCurrentApp();
49
+ else trySetCurrentApp(app);
50
+ app.config.errorHandler = (err) => {
51
+ handleError(err, true);
52
+ };
53
+ }
59
54
  }
60
55
  function handleError(err, rethrow = true) {
61
- if (globalThis.__VUEUTILS__.errorHandler) {
62
- void Promise.try(globalThis.__VUEUTILS__.errorHandler, err);
63
- } else if (rethrow) {
64
- throw err;
65
- }
56
+ if (globalThis.__VUEUTILS__.errorHandler) Promise.try(globalThis.__VUEUTILS__.errorHandler, err);
57
+ else if (rethrow) throw err;
66
58
  }
67
-
59
+ //#endregion
60
+ //#region src/utils/maybe.ts
68
61
  function maybe(value, fn) {
69
- const _value = toValue(value);
70
- return isNil(_value) ? null : fn(_value);
71
- }
72
-
73
- const console = {
74
- assert,
75
- debug,
76
- error,
77
- info,
78
- log,
79
- trace,
80
- warn
62
+ const _value = toValue(value);
63
+ return isNil(_value) ? null : fn(_value);
64
+ }
65
+ //#endregion
66
+ //#region src/composables/console/index.ts
67
+ var console = {
68
+ assert,
69
+ debug,
70
+ error,
71
+ info,
72
+ log,
73
+ trace,
74
+ warn
81
75
  };
82
76
  function assert(cond, message) {
83
- return watchImmediate(toRef(cond), (value) => {
84
- globalThis.console.assert(value ?? false, message);
85
- });
77
+ return watchImmediate(toRef(cond), (value) => {
78
+ globalThis.console.assert(value ?? false, message);
79
+ });
86
80
  }
87
81
  function debug(source) {
88
- return watchImmediate(toRef(source), (value) => globalThis.console.debug(value));
82
+ return watchImmediate(toRef(source), (value) => globalThis.console.debug(value));
89
83
  }
90
84
  function error(source) {
91
- return watchImmediate(toRef(source), (value) => globalThis.console.error(value));
85
+ return watchImmediate(toRef(source), (value) => globalThis.console.error(value));
92
86
  }
93
87
  function info(source) {
94
- return watchImmediate(toRef(source), (value) => globalThis.console.info(value));
88
+ return watchImmediate(toRef(source), (value) => globalThis.console.info(value));
95
89
  }
96
90
  function log(source) {
97
- return watchImmediate(toRef(source), (value) => globalThis.console.log(value));
91
+ return watchImmediate(toRef(source), (value) => globalThis.console.log(value));
98
92
  }
99
93
  function trace(source) {
100
- return watchImmediate(toRef(source), (value) => globalThis.console.trace(value));
94
+ return watchImmediate(toRef(source), (value) => globalThis.console.trace(value));
101
95
  }
102
96
  function warn(source) {
103
- return watchImmediate(toRef(source), (value) => globalThis.console.warn(value));
97
+ return watchImmediate(toRef(source), (value) => globalThis.console.warn(value));
104
98
  }
105
-
99
+ //#endregion
100
+ //#region src/composables/useMutex/index.ts
106
101
  function useMutex(options) {
107
- const mutex = new Mutex();
108
- const locked = ref(mutex.isLocked);
109
- let onError;
110
- if (typeof options?.onError === "function") {
111
- onError = options.onError;
112
- } else if (options?.onError !== false) {
113
- onError = handleError;
114
- }
115
- async function acquire() {
116
- await mutex.acquire();
117
- locked.value = mutex.isLocked;
118
- }
119
- function release() {
120
- mutex.release();
121
- locked.value = mutex.isLocked;
122
- }
123
- function lock(fn) {
124
- const { resolve, reject, promise } = Promise.withResolvers();
125
- acquire().then(() => fn()).then((value) => resolve(value)).catch(reject).finally(release);
126
- if (onError) {
127
- return promise.catch(onError);
128
- }
129
- return promise;
130
- }
131
- return {
132
- locked: readonly(locked),
133
- acquire,
134
- release,
135
- lock
136
- };
137
- }
138
-
102
+ const mutex = new Mutex();
103
+ const locked = ref(mutex.isLocked);
104
+ let onError;
105
+ if (typeof options?.onError === "function") onError = options.onError;
106
+ else if (options?.onError !== false) onError = handleError;
107
+ async function acquire() {
108
+ await mutex.acquire();
109
+ locked.value = mutex.isLocked;
110
+ }
111
+ function release() {
112
+ mutex.release();
113
+ locked.value = mutex.isLocked;
114
+ }
115
+ function lock(fn) {
116
+ const { resolve, reject, promise } = Promise.withResolvers();
117
+ acquire().then(() => fn()).then((value) => resolve(value)).catch(reject).finally(release);
118
+ if (onError) return promise.catch(onError);
119
+ return promise;
120
+ }
121
+ return {
122
+ locked: readonly(locked),
123
+ acquire,
124
+ release,
125
+ lock
126
+ };
127
+ }
128
+ //#endregion
129
+ //#region src/composables/asyncRef/index.ts
139
130
  function asyncRef(initial, fn, options = {}) {
140
- const value = useAsyncState(fn, initial, {
141
- immediate: options.immediate ?? true,
142
- onError: handleError,
143
- resetOnExecute: false,
144
- shallow: true,
145
- throwError: false,
146
- ...options
147
- });
148
- const load = async () => {
149
- await value.execute();
150
- };
151
- return {
152
- state: value.state,
153
- loading: value.isLoading,
154
- isReady: value.isReady,
155
- load
156
- };
157
- }
158
-
131
+ const value = useAsyncState(fn, initial, {
132
+ immediate: options.immediate ?? true,
133
+ onError: handleError,
134
+ resetOnExecute: false,
135
+ shallow: true,
136
+ throwError: false,
137
+ ...options
138
+ });
139
+ const load = async () => {
140
+ await value.execute();
141
+ };
142
+ return {
143
+ state: value.state,
144
+ loading: value.isLoading,
145
+ isReady: value.isReady,
146
+ load
147
+ };
148
+ }
149
+ //#endregion
150
+ //#region src/composables/localRef/index.ts
159
151
  function localRef(key, initial, options) {
160
- return useLocalStorage(key, initial, {
161
- deep: options?.deep ?? true,
162
- initOnMounted: options?.initOnMounted ?? true,
163
- listenToStorageChanges: options?.listenToStorageChanges ?? true,
164
- mergeDefaults: options?.mergeDefaults ?? true,
165
- onError: options?.onError ?? handleError,
166
- writeDefaults: options?.writeDefaults ?? true,
167
- serializer: {
168
- read: JSON.parse,
169
- write: JSON.stringify
170
- },
171
- ...options
172
- });
173
- }
174
-
152
+ return useLocalStorage(key, initial, {
153
+ deep: options?.deep ?? true,
154
+ initOnMounted: options?.initOnMounted ?? true,
155
+ listenToStorageChanges: options?.listenToStorageChanges ?? true,
156
+ mergeDefaults: options?.mergeDefaults ?? true,
157
+ onError: options?.onError ?? handleError,
158
+ writeDefaults: options?.writeDefaults ?? true,
159
+ serializer: {
160
+ read: JSON.parse,
161
+ write: JSON.stringify
162
+ },
163
+ ...options
164
+ });
165
+ }
166
+ //#endregion
167
+ //#region src/composables/onKeyDown/index.ts
175
168
  function onKeyDown(key, handler, options = {}) {
176
- const {
177
- altKey = false,
178
- ctrlKey = false,
179
- dedupe = true,
180
- detached = false,
181
- enabled = true,
182
- metaKey = false,
183
- prevent = true,
184
- shiftKey = false,
185
- stopPropagation = false
186
- } = options;
187
- const callback = (e) => {
188
- if (e.altKey !== altKey || e.ctrlKey !== ctrlKey || e.metaKey !== metaKey || e.shiftKey !== shiftKey) {
189
- return;
190
- }
191
- if (prevent) {
192
- e.preventDefault();
193
- }
194
- if (stopPropagation) {
195
- e.stopPropagation();
196
- }
197
- if (toValue(enabled) && handler) {
198
- Promise.try(handler, e).catch(handleError);
199
- }
200
- };
201
- const on = () => {
202
- return onKeyStroke(key, callback, {
203
- ...options,
204
- eventName: "keydown",
205
- dedupe
206
- });
207
- };
208
- let stop;
209
- if (detached) {
210
- const scope = effectScope(
211
- /* detached */
212
- true
213
- );
214
- stop = scope.run(() => on());
215
- } else {
216
- stop = on();
217
- tryOnScopeDispose(() => stop());
218
- }
219
- return stop;
169
+ const { altKey = false, ctrlKey = false, dedupe = true, detached = false, enabled = true, metaKey = false, prevent = true, shiftKey = false, stopPropagation = false } = options;
170
+ const callback = (e) => {
171
+ if (e.altKey !== altKey || e.ctrlKey !== ctrlKey || e.metaKey !== metaKey || e.shiftKey !== shiftKey) return;
172
+ if (prevent) e.preventDefault();
173
+ if (stopPropagation) e.stopPropagation();
174
+ if (toValue(enabled) && handler) Promise.try(handler, e).catch(handleError);
175
+ };
176
+ const on = () => {
177
+ return onKeyStroke(key, callback, {
178
+ ...options,
179
+ eventName: "keydown",
180
+ dedupe
181
+ });
182
+ };
183
+ let stop;
184
+ if (detached) stop = effectScope(true).run(() => on());
185
+ else {
186
+ stop = on();
187
+ tryOnScopeDispose(() => stop());
188
+ }
189
+ return stop;
220
190
  }
221
191
  function onAltKeyDown(key, handler, options) {
222
- return onKeyDown(key, handler, { ...options, altKey: true });
192
+ return onKeyDown(key, handler, {
193
+ ...options,
194
+ altKey: true
195
+ });
223
196
  }
224
197
  function onCtrlKeyDown(key, handler, options) {
225
- return onKeyDown(key, handler, { ...options, ctrlKey: true });
198
+ return onKeyDown(key, handler, {
199
+ ...options,
200
+ ctrlKey: true
201
+ });
226
202
  }
227
203
  function onShiftKeyDown(key, handler, options) {
228
- return onKeyDown(key, handler, { ...options, shiftKey: true });
204
+ return onKeyDown(key, handler, {
205
+ ...options,
206
+ shiftKey: true
207
+ });
229
208
  }
230
209
  function onCtrlShiftKeyDown(key, handler, options) {
231
- return onKeyDown(key, handler, { ...options, ctrlKey: true, shiftKey: true });
232
- }
233
-
210
+ return onKeyDown(key, handler, {
211
+ ...options,
212
+ ctrlKey: true,
213
+ shiftKey: true
214
+ });
215
+ }
216
+ //#endregion
217
+ //#region src/composables/sessionRef/index.ts
234
218
  function sessionRef(key, initial, options) {
235
- return useSessionStorage(key, initial, {
236
- deep: options?.deep ?? true,
237
- initOnMounted: options?.initOnMounted ?? true,
238
- listenToStorageChanges: options?.listenToStorageChanges ?? true,
239
- mergeDefaults: options?.mergeDefaults ?? true,
240
- onError: options?.onError ?? handleError,
241
- writeDefaults: options?.writeDefaults ?? true,
242
- serializer: {
243
- read: JSON.parse,
244
- write: JSON.stringify
245
- },
246
- ...options
247
- });
248
- }
249
-
219
+ return useSessionStorage(key, initial, {
220
+ deep: options?.deep ?? true,
221
+ initOnMounted: options?.initOnMounted ?? true,
222
+ listenToStorageChanges: options?.listenToStorageChanges ?? true,
223
+ mergeDefaults: options?.mergeDefaults ?? true,
224
+ onError: options?.onError ?? handleError,
225
+ writeDefaults: options?.writeDefaults ?? true,
226
+ serializer: {
227
+ read: JSON.parse,
228
+ write: JSON.stringify
229
+ },
230
+ ...options
231
+ });
232
+ }
233
+ //#endregion
234
+ //#region src/composables/asyncComputed/index.ts
250
235
  function asyncComputed(initial, callback, options) {
251
- const state = computedAsync(callback, initial, {
252
- onError: handleError,
253
- shallow: true,
254
- lazy: false,
255
- ...options
256
- });
257
- return state;
258
- }
259
-
236
+ return computedAsync(callback, initial, {
237
+ onError: handleError,
238
+ shallow: true,
239
+ lazy: false,
240
+ ...options
241
+ });
242
+ }
243
+ //#endregion
244
+ //#region src/composables/useWindowSize/index.ts
260
245
  function useWindowHeight() {
261
- return useWindowSize().height;
246
+ return useWindowSize().height;
262
247
  }
263
248
  function useWindowWidth() {
264
- return useWindowSize().width;
249
+ return useWindowSize().width;
265
250
  }
266
-
251
+ //#endregion
252
+ //#region src/composables/useElementSize/index.ts
267
253
  function useElementSize(element) {
268
- return useElementSize$1(toRef(element), { height: 0, width: 0 }, { box: "border-box" });
254
+ return useElementSize$1(toRef(element), {
255
+ height: 0,
256
+ width: 0
257
+ }, { box: "border-box" });
269
258
  }
270
259
  function useHeight(element) {
271
- return useElementSize(element).height;
260
+ return useElementSize(element).height;
272
261
  }
273
262
  function useHeightDiff(element, lhs) {
274
- const height = useHeight(element);
275
- const lhsRef = lhs ? toRef(lhs) : useWindowHeight();
276
- return computed(() => lhsRef.value - height.value);
263
+ const height = useHeight(element);
264
+ const lhsRef = lhs ? toRef(lhs) : useWindowHeight();
265
+ return computed(() => lhsRef.value - height.value);
277
266
  }
278
267
  function useWidth(element) {
279
- return useElementSize(element).width;
268
+ return useElementSize(element).width;
280
269
  }
281
270
  function useWidthDiff(element, lhs) {
282
- const width = useWidth(element);
283
- const lhsRef = lhs ? toRef(lhs) : useWindowWidth();
284
- return computed(() => lhsRef.value - width.value);
271
+ const width = useWidth(element);
272
+ const lhsRef = lhs ? toRef(lhs) : useWindowWidth();
273
+ return computed(() => lhsRef.value - width.value);
285
274
  }
286
-
287
- const breakpointsKey = /* @__PURE__ */ Symbol();
275
+ //#endregion
276
+ //#region src/composables/useBreakpoints/index.ts
277
+ var breakpointsKey = Symbol();
288
278
  function useBreakpoints() {
289
- return tryInjectOrElse(breakpointsKey, () => {
290
- return useBreakpoints$1(breakpointsTailwind);
291
- });
292
- }
293
-
294
- if (!Object.hasOwn(globalThis, "__VUEUTILS__")) {
295
- Object.defineProperty(globalThis, "__VUEUTILS__", {
296
- configurable: false,
297
- enumerable: true,
298
- writable: false,
299
- value: {}
300
- });
301
- }
302
-
303
- export { asyncComputed, asyncRef, console, getCurrentApp, getErrorHandler, handleError, inject, localRef, maybe, onAltKeyDown, onCtrlKeyDown, onCtrlShiftKeyDown, onKeyDown, onShiftKeyDown, provide, runWithContext, sessionRef, setCurrentApp, setErrorHandler, tryGetCurrentApp, tryInject, tryInjectOrElse, trySetCurrentApp, useBreakpoints, useElementSize, useHeight, useHeightDiff, useMutex, useWidth, useWidthDiff, useWindowHeight, useWindowWidth };
279
+ return tryInjectOrElse(breakpointsKey, () => {
280
+ return useBreakpoints$1(breakpointsTailwind);
281
+ });
282
+ }
283
+ //#endregion
284
+ //#region src/index.ts
285
+ if (!Object.hasOwn(globalThis, "__VUEUTILS__")) Object.defineProperty(globalThis, "__VUEUTILS__", {
286
+ configurable: false,
287
+ enumerable: true,
288
+ writable: false,
289
+ value: {}
290
+ });
291
+ //#endregion
292
+ export { asyncComputed, asyncRef, console, getCurrentApp, getErrorHandler, handleError, inject, localRef, maybe, onAltKeyDown, onCtrlKeyDown, onCtrlShiftKeyDown, onKeyDown, onShiftKeyDown, provide, runWithContext, sessionRef, setCurrentApp, setErrorHandler, tryGetCurrentApp, tryInject, tryInjectOrElse, trySetCurrentApp, useBreakpoints, useElementSize, useHeight, useHeightDiff, useMutex, useWidth, useWidthDiff, useWindowHeight, useWindowSize, useWindowWidth };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/vue",
3
- "version": "4.0.5",
3
+ "version": "4.0.7",
4
4
  "description": "Vue utilities",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -24,20 +24,20 @@
24
24
  "composables"
25
25
  ],
26
26
  "dependencies": {
27
- "@tb-dev/utils": "^7.3.3",
27
+ "@tb-dev/utils": "^7.3.5",
28
28
  "@vueuse/core": "^14.2.1",
29
- "es-toolkit": "^1.44.0",
30
- "vue": "^3.5.28"
29
+ "es-toolkit": "^1.45.1",
30
+ "vue": "^3.5.30"
31
31
  },
32
32
  "devDependencies": {
33
- "@tb-dev/eslint-config": "^9.0.1",
34
- "@types/node": "^25.3.0",
35
- "eslint": "^10.0.2",
33
+ "@tb-dev/eslint-config": "^9.0.3",
34
+ "@types/node": "^25.5.0",
35
+ "eslint": "^10.0.3",
36
36
  "tslib": "^2.8.1",
37
37
  "typedoc": "^0.28.17",
38
38
  "typedoc-plugin-mdn-links": "^5.1.1",
39
39
  "typescript": "~5.9.3",
40
- "vite": "^7.3.1",
40
+ "vite": "^8.0.0",
41
41
  "vite-plugin-dts": "^4.5.4"
42
42
  },
43
43
  "files": [