@tb-dev/vue 2.0.14 → 2.2.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,8 +1,9 @@
1
- export * from './async-computed';
2
- export * from './async-ref';
3
1
  export * from './console';
4
- export * from './element-size';
5
- export * from './key-down';
6
- export * from './local-ref';
7
- export * from './session-ref';
8
- export * from './window-size';
2
+ export * from './useMutex';
3
+ export * from './asyncRef';
4
+ export * from './localRef';
5
+ export * from './onKeyDown';
6
+ export * from './sessionRef';
7
+ export * from './asyncComputed';
8
+ export * from './useWindowSize';
9
+ export * from './useElementSize';
@@ -0,0 +1,6 @@
1
+ export declare function useMutex(): {
2
+ locked: Readonly<import('vue').Ref<boolean, boolean>>;
3
+ acquire: () => Promise<void>;
4
+ release: () => void;
5
+ lock: <T = unknown>(fn: () => Promise<T>) => Promise<T>;
6
+ };
@@ -1,3 +1,5 @@
1
1
  import { Ref } from 'vue';
2
+ import { useWindowSize } from '@vueuse/core';
3
+ export { useWindowSize };
2
4
  export declare function useWindowHeight(): Readonly<Ref<number>>;
3
5
  export declare function useWindowWidth(): Readonly<Ref<number>>;
package/dist/index.js CHANGED
@@ -1,6 +1,39 @@
1
+ import { watch, ref, readonly, inject as inject$1, computed, effectScope, toValue, toRef } from 'vue';
2
+ import { Mutex } from 'es-toolkit';
1
3
  import { unwrap, isNil } from '@tb-dev/utils';
2
- import { inject as inject$1, watch, toRef, computed, effectScope, toValue } from 'vue';
3
- import { computedAsync, useAsyncState, useWindowSize, useElementSize as useElementSize$1, tryOnScopeDispose, onKeyStroke, useLocalStorage, useSessionStorage } from '@vueuse/core';
4
+ import { useAsyncState, useLocalStorage, tryOnScopeDispose, onKeyStroke, useSessionStorage, computedAsync, useWindowSize, useElementSize as useElementSize$1 } from '@vueuse/core';
5
+ export { useWindowSize } from '@vueuse/core';
6
+
7
+ function log(source) {
8
+ return watch(source, (value) => console.log(value));
9
+ }
10
+ function warn(source) {
11
+ return watch(source, (value) => console.warn(value));
12
+ }
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
+ }
4
37
 
5
38
  function create$1() {
6
39
  let APP = null;
@@ -80,16 +113,6 @@ function create() {
80
113
  }
81
114
  const { get: getErrorHandler, set: setErrorHandler, handle: handleError } = create();
82
115
 
83
- function asyncComputed(initial, callback, options) {
84
- const state = computedAsync(callback, initial, {
85
- onError: handleError,
86
- shallow: true,
87
- lazy: false,
88
- ...options
89
- });
90
- return state;
91
- }
92
-
93
116
  function asyncRef(initial, fn, options = {}) {
94
117
  const { immediate = true } = options;
95
118
  const value = useAsyncState(fn, initial, {
@@ -111,38 +134,24 @@ function asyncRef(initial, fn, options = {}) {
111
134
  };
112
135
  }
113
136
 
114
- function log(source) {
115
- return watch(source, (value) => console.log(value));
116
- }
117
- function warn(source) {
118
- return watch(source, (value) => console.warn(value));
119
- }
120
-
121
- function useWindowHeight() {
122
- return useWindowSize().height;
123
- }
124
- function useWindowWidth() {
125
- return useWindowSize().width;
126
- }
127
-
128
- function useElementSize(element) {
129
- return useElementSize$1(toRef(element), { height: 0, width: 0 }, { box: "border-box" });
130
- }
131
- function useHeight(element) {
132
- return useElementSize(element).height;
133
- }
134
- function useHeightDiff(element) {
135
- const height = useHeight(element);
136
- const windowHeight = useWindowHeight();
137
- return computed(() => windowHeight.value - height.value);
138
- }
139
- function useWidth(element) {
140
- return useElementSize(element).width;
141
- }
142
- function useWidthDiff(element) {
143
- const width = useWidth(element);
144
- const windowWidth = useWindowWidth();
145
- return computed(() => windowWidth.value - width.value);
137
+ function localRef(key, initial, options) {
138
+ const defaultValue = { inner: initial };
139
+ const local = useLocalStorage(key, defaultValue, {
140
+ deep: options?.deep ?? true,
141
+ initOnMounted: options?.initOnMounted ?? true,
142
+ onError: options?.onError ?? handleError,
143
+ writeDefaults: options?.writeDefaults ?? true,
144
+ serializer: {
145
+ read: JSON.parse,
146
+ write: JSON.stringify
147
+ }
148
+ });
149
+ return computed({
150
+ get: () => local.value.inner,
151
+ set: (value) => {
152
+ local.value.inner = value;
153
+ }
154
+ });
146
155
  }
147
156
 
148
157
  function onKeyDown(key, handler, options = {}) {
@@ -204,26 +213,6 @@ function onCtrlShiftKeyDown(key, handler, options) {
204
213
  return onKeyDown(key, handler, { ...options, ctrlKey: true, shiftKey: true });
205
214
  }
206
215
 
207
- function localRef(key, initial, options) {
208
- const defaultValue = { inner: initial };
209
- const local = useLocalStorage(key, defaultValue, {
210
- deep: options?.deep ?? true,
211
- initOnMounted: options?.initOnMounted ?? true,
212
- onError: options?.onError ?? handleError,
213
- writeDefaults: options?.writeDefaults ?? true,
214
- serializer: {
215
- read: JSON.parse,
216
- write: JSON.stringify
217
- }
218
- });
219
- return computed({
220
- get: () => local.value.inner,
221
- set: (value) => {
222
- local.value.inner = value;
223
- }
224
- });
225
- }
226
-
227
216
  function sessionRef(key, initial, options) {
228
217
  const defaultValue = { inner: initial };
229
218
  const session = useSessionStorage(key, defaultValue, {
@@ -244,9 +233,46 @@ function sessionRef(key, initial, options) {
244
233
  });
245
234
  }
246
235
 
236
+ function asyncComputed(initial, callback, options) {
237
+ const state = computedAsync(callback, initial, {
238
+ onError: handleError,
239
+ shallow: true,
240
+ lazy: false,
241
+ ...options
242
+ });
243
+ return state;
244
+ }
245
+
246
+ function useWindowHeight() {
247
+ return useWindowSize().height;
248
+ }
249
+ function useWindowWidth() {
250
+ return useWindowSize().width;
251
+ }
252
+
253
+ function useElementSize(element) {
254
+ return useElementSize$1(toRef(element), { height: 0, width: 0 }, { box: "border-box" });
255
+ }
256
+ function useHeight(element) {
257
+ return useElementSize(element).height;
258
+ }
259
+ function useHeightDiff(element) {
260
+ const height = useHeight(element);
261
+ const windowHeight = useWindowHeight();
262
+ return computed(() => windowHeight.value - height.value);
263
+ }
264
+ function useWidth(element) {
265
+ return useElementSize(element).width;
266
+ }
267
+ function useWidthDiff(element) {
268
+ const width = useWidth(element);
269
+ const windowWidth = useWindowWidth();
270
+ return computed(() => windowWidth.value - width.value);
271
+ }
272
+
247
273
  function maybe(value, fn) {
248
274
  const _value = toValue(value);
249
275
  return isNil(_value) ? null : fn(_value);
250
276
  }
251
277
 
252
- 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, useWidth, useWidthDiff, useWindowHeight, useWindowWidth, warn };
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/vue",
3
- "version": "2.0.14",
3
+ "version": "2.2.0",
4
4
  "description": "Vue utilities",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -26,6 +26,7 @@
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
30
  "vue": "^3.5.18"
30
31
  },
31
32
  "devDependencies": {