@milaboratories/uikit 2.3.7 → 2.3.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milaboratories/uikit",
3
- "version": "2.3.7",
3
+ "version": "2.3.9",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
@@ -24,7 +24,7 @@
24
24
  "d3": "^7.9.0",
25
25
  "resize-observer-polyfill": "^1.5.1",
26
26
  "@milaboratories/helpers": "^1.6.17",
27
- "@platforma-sdk/model": "^1.39.8"
27
+ "@platforma-sdk/model": "^1.39.18"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@vitejs/plugin-vue": "^5.2.3",
@@ -0,0 +1,58 @@
1
+ import { isJsonEqual } from '@milaboratories/helpers';
2
+ import {
3
+ computed,
4
+ ref,
5
+ watch,
6
+ type ComputedGetter,
7
+ type ComputedSetter,
8
+ type ComputedRef,
9
+ type WritableComputedRef,
10
+ } from 'vue';
11
+
12
+ export function computedCached<T>(options: {
13
+ get: ComputedGetter<T>;
14
+ set: ComputedSetter<T>;
15
+ deep?: boolean;
16
+ }): WritableComputedRef<T>;
17
+ export function computedCached<T>(options: {
18
+ get: ComputedGetter<T>;
19
+ deep?: boolean;
20
+ }): ComputedRef<T>;
21
+ export function computedCached<T>(getter: ComputedGetter<T>): ComputedRef<T>;
22
+ export function computedCached<T>(options: ComputedGetter<T> | {
23
+ get: ComputedGetter<T>;
24
+ set?: ComputedSetter<T>;
25
+ deep?: boolean;
26
+ }) {
27
+ if (typeof options === 'function') {
28
+ options = {
29
+ get: options,
30
+ };
31
+ }
32
+ const { get: getter, set: setter, deep } = options;
33
+
34
+ const cachedValue = ref<T>(getter());
35
+ watch(
36
+ getter,
37
+ (newValue) => {
38
+ if (!isJsonEqual(newValue, cachedValue.value)) {
39
+ cachedValue.value = newValue;
40
+ }
41
+ },
42
+ { deep },
43
+ );
44
+
45
+ if (setter) {
46
+ return computed({
47
+ get: () => cachedValue.value,
48
+ set: (newValue) => {
49
+ if (!isJsonEqual(newValue, cachedValue.value)) {
50
+ cachedValue.value = newValue;
51
+ setter(newValue);
52
+ }
53
+ },
54
+ });
55
+ } else {
56
+ return computed(() => cachedValue.value);
57
+ }
58
+ }
@@ -0,0 +1,45 @@
1
+ import { isJsonEqual } from '@milaboratories/helpers';
2
+ import {
3
+ ref,
4
+ watch,
5
+ type WatchCallback,
6
+ type WatchHandle,
7
+ type WatchSource,
8
+ } from 'vue';
9
+
10
+ type MaybeUndefined<T, I> = I extends true ? T | undefined : T;
11
+ export interface WatchCachedOptions<Immediate = boolean> {
12
+ immediate?: Immediate;
13
+ deep?: boolean;
14
+ // when `once` is needed, caching is useless, use plain watch instead
15
+ }
16
+ export function watchCached<T, Immediate extends Readonly<boolean> = false>(
17
+ source: WatchSource<T>,
18
+ cb: WatchCallback<T, MaybeUndefined<T, Immediate>>,
19
+ options?: WatchCachedOptions<Immediate>,
20
+ ): WatchHandle {
21
+ const cachedValue = ref<T>();
22
+ const handle = watch(
23
+ source,
24
+ (newValue) => {
25
+ if (!isJsonEqual(newValue, cachedValue.value)) {
26
+ cachedValue.value = newValue;
27
+ }
28
+ },
29
+ {
30
+ deep: options?.deep,
31
+ immediate: true, // always initialize cachedValue
32
+ },
33
+ );
34
+ watch<T, Immediate>(
35
+ () => cachedValue.value as T, // `as T` is safe as we always initialize cachedValue
36
+ cb, // separate watch so that `onWatcherCleanup` would only be triggerred here
37
+ {
38
+ // standard vue `WatchOptions` conform to `WatchCachedOptions` interface,
39
+ // so construct new options to remove unsupported entries
40
+ deep: options?.deep,
41
+ immediate: options?.immediate,
42
+ },
43
+ );
44
+ return handle; // stopping first handle would effectively stop the second one
45
+ }
package/src/index.ts CHANGED
@@ -110,6 +110,8 @@ export { useQuery } from './composition/useQuery.ts';
110
110
  export { useDraggable } from './composition/useDraggable';
111
111
  export { useComponentProp } from './composition/useComponentProp';
112
112
  export * from './composition/useWatchFetch';
113
+ export * from './composition/watchCached';
114
+ export * from './composition/computedCached';
113
115
 
114
116
  /**
115
117
  * Utils/Partials