@arrai-innovations/reactive-helpers 2.2.0 → 2.3.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.
- package/package.json +1 -1
- package/use/index.js +1 -0
- package/use/listCalculated.js +8 -0
- package/use/listRelated.js +4 -1
- package/use/objectCalculated.js +88 -0
package/package.json
CHANGED
package/use/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export * from "./listInstance.js";
|
|
|
4
4
|
export * from "./listRelated.js";
|
|
5
5
|
export * from "./listSort.js";
|
|
6
6
|
export * from "./listSubscription.js";
|
|
7
|
+
export * from "./objectCalculated.js";
|
|
7
8
|
export * from "./objectInstance.js";
|
|
8
9
|
export * from "./objectSubscription.js";
|
|
9
10
|
export * from "./paginatedListInstance.js";
|
package/use/listCalculated.js
CHANGED
|
@@ -2,6 +2,14 @@ import { isEmpty } from "lodash";
|
|
|
2
2
|
import { computed, effectScope, onScopeDispose, reactive, toRef, toRefs, watch } from "vue";
|
|
3
3
|
import { keyDiff } from "../utils";
|
|
4
4
|
|
|
5
|
+
export function useListCalculateds(instances, args) {
|
|
6
|
+
for (const [key, value] of Object.entries(args)) {
|
|
7
|
+
useListCalculated({
|
|
8
|
+
parentState: instances[key].state,
|
|
9
|
+
...value,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
}
|
|
5
13
|
// the simpler sibling of useListRelated
|
|
6
14
|
// rules are just keys to functions that will be called with the object
|
|
7
15
|
// and the result will be added as a computed property
|
package/use/listRelated.js
CHANGED
|
@@ -4,7 +4,10 @@ import { keyDiff } from "../utils/keyDiff";
|
|
|
4
4
|
|
|
5
5
|
export function useListRelateds(instances, args) {
|
|
6
6
|
for (const [key, value] of Object.entries(args)) {
|
|
7
|
-
useListRelated({
|
|
7
|
+
useListRelated({
|
|
8
|
+
parentState: instances[key].state,
|
|
9
|
+
...value,
|
|
10
|
+
});
|
|
8
11
|
}
|
|
9
12
|
}
|
|
10
13
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { computed, effectScope, onScopeDispose, reactive, toRef, toRefs, watch } from "vue";
|
|
2
|
+
import { keyDiff } from "../utils";
|
|
3
|
+
|
|
4
|
+
export function useObjectCalculateds(instances, args) {
|
|
5
|
+
for (const [key, value] of Object.entries(args)) {
|
|
6
|
+
useObjectCalculated({
|
|
7
|
+
parentState: instances[key].state,
|
|
8
|
+
...value,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
// the single object version of useListCalculated
|
|
13
|
+
export function useObjectCalculated({
|
|
14
|
+
parentState,
|
|
15
|
+
calculatedObjectRules,
|
|
16
|
+
calculatedObjectPropertyName = "calculatedObject", // NOT REACTIVE
|
|
17
|
+
}) {
|
|
18
|
+
const state = reactive({
|
|
19
|
+
calculatedObjectRules,
|
|
20
|
+
calculatedObjectObject: {},
|
|
21
|
+
object: {},
|
|
22
|
+
});
|
|
23
|
+
const calculatedObjectEffectScopes = {};
|
|
24
|
+
const calculatedObjectOriginalFunctions = {};
|
|
25
|
+
|
|
26
|
+
// don't change calculatedObjectPropertyName on us or it will break
|
|
27
|
+
const copn = calculatedObjectPropertyName + "";
|
|
28
|
+
|
|
29
|
+
const es = effectScope();
|
|
30
|
+
|
|
31
|
+
es.run(() => {
|
|
32
|
+
state.object = computed(() =>
|
|
33
|
+
reactive({
|
|
34
|
+
[copn]: toRef(state, "calculatedObjectObject"),
|
|
35
|
+
...toRefs(parentState.object),
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
watch(
|
|
40
|
+
[() => Object.keys(state.calculatedObjectRules)],
|
|
41
|
+
() => {
|
|
42
|
+
const { addedKeys, removedKeys, changedKeys } = keyDiff(
|
|
43
|
+
state.calculatedObjectRules,
|
|
44
|
+
calculatedObjectOriginalFunctions
|
|
45
|
+
);
|
|
46
|
+
for (const removedKey of removedKeys) {
|
|
47
|
+
delete calculatedObjectOriginalFunctions[removedKey];
|
|
48
|
+
delete state.calculatedObjectObject[removedKey];
|
|
49
|
+
if (calculatedObjectEffectScopes[removedKey]) {
|
|
50
|
+
calculatedObjectEffectScopes[removedKey].stop();
|
|
51
|
+
delete calculatedObjectEffectScopes[removedKey];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const addedKey of addedKeys) {
|
|
55
|
+
calculatedObjectOriginalFunctions[addedKey] = state.calculatedObjectRules[addedKey];
|
|
56
|
+
calculatedObjectEffectScopes[addedKey] = effectScope();
|
|
57
|
+
calculatedObjectEffectScopes[addedKey].run(() => {
|
|
58
|
+
state.calculatedObjectObject[addedKey] = computed(() =>
|
|
59
|
+
calculatedObjectOriginalFunctions[addedKey](state.object)
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
for (const changedKey of changedKeys) {
|
|
64
|
+
calculatedObjectOriginalFunctions[changedKey] = state.calculatedObjectRules[changedKey];
|
|
65
|
+
calculatedObjectEffectScopes[changedKey].stop();
|
|
66
|
+
calculatedObjectEffectScopes[changedKey] = effectScope();
|
|
67
|
+
calculatedObjectEffectScopes[changedKey].run(() => {
|
|
68
|
+
state.calculatedObjectObject[changedKey] = computed(() =>
|
|
69
|
+
calculatedObjectOriginalFunctions[changedKey](state.object)
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
immediate: true,
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
onScopeDispose(() => {
|
|
80
|
+
for (const key in calculatedObjectEffectScopes) {
|
|
81
|
+
calculatedObjectEffectScopes[key].stop();
|
|
82
|
+
}
|
|
83
|
+
for (const key in calculatedObjectOriginalFunctions) {
|
|
84
|
+
delete calculatedObjectOriginalFunctions[key];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|