@arrai-innovations/reactive-helpers 2.1.1 → 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.
- package/package.json +1 -1
- package/use/index.js +1 -0
- package/use/listCalculated.js +128 -0
- package/use/listInstance.js +7 -1
- package/use/listSubscription.js +7 -0
- package/use/objectInstance.js +6 -0
- package/use/objectSubscription.js +7 -0
package/package.json
CHANGED
package/use/index.js
CHANGED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { isEmpty } from "lodash";
|
|
2
|
+
import { computed, effectScope, onScopeDispose, reactive, toRef, toRefs, watch } from "vue";
|
|
3
|
+
import { keyDiff } from "../utils";
|
|
4
|
+
|
|
5
|
+
// the simpler sibling of useListRelated
|
|
6
|
+
// rules are just keys to functions that will be called with the object
|
|
7
|
+
// and the result will be added as a computed property
|
|
8
|
+
export function useListCalculated({
|
|
9
|
+
parentState,
|
|
10
|
+
calculatedObjectsRules,
|
|
11
|
+
calculatedObjectsPropertyName = "calculatedObjects", // NOT REACTIVE
|
|
12
|
+
}) {
|
|
13
|
+
const state = reactive({
|
|
14
|
+
calculatedObjectsRules,
|
|
15
|
+
calculatedObjectsObjects: {},
|
|
16
|
+
objects: {},
|
|
17
|
+
});
|
|
18
|
+
const calculatedObjectsEffectScopes = {};
|
|
19
|
+
const combinedEffectScopes = {};
|
|
20
|
+
|
|
21
|
+
// don't change calculatedObjectsPropertyName on us or it will break
|
|
22
|
+
const copn = calculatedObjectsPropertyName + "";
|
|
23
|
+
|
|
24
|
+
function parentStateObjectsWatch() {
|
|
25
|
+
const { addedKeys, removedKeys } = keyDiff(
|
|
26
|
+
Object.keys(parentState.objects),
|
|
27
|
+
Object.keys(state.calculatedObjectsObjects)
|
|
28
|
+
);
|
|
29
|
+
for (const removedKey of removedKeys) {
|
|
30
|
+
delete state.calculatedObjectsObjects[removedKey];
|
|
31
|
+
delete state.objects[removedKey];
|
|
32
|
+
if (calculatedObjectsEffectScopes[removedKey]) {
|
|
33
|
+
calculatedObjectsEffectScopes[removedKey].stop();
|
|
34
|
+
delete calculatedObjectsEffectScopes[removedKey];
|
|
35
|
+
}
|
|
36
|
+
if (combinedEffectScopes[removedKey]) {
|
|
37
|
+
combinedEffectScopes[removedKey].stop();
|
|
38
|
+
delete combinedEffectScopes[removedKey];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
for (const addedKey of addedKeys) {
|
|
42
|
+
state.calculatedObjectsObjects[addedKey] = {};
|
|
43
|
+
combinedEffectScopes[addedKey] = effectScope();
|
|
44
|
+
combinedEffectScopes[addedKey].run(() => {
|
|
45
|
+
state.objects[addedKey] = computed(() =>
|
|
46
|
+
reactive({
|
|
47
|
+
...toRefs(state.calculatedObjectsObjects[addedKey]),
|
|
48
|
+
...toRefs(parentState.objects[addedKey]),
|
|
49
|
+
})
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function calculatedObjectsWatch() {
|
|
56
|
+
if (state.calculatedObjectsRules === false) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const calculatedObjectsRulesIsEmpty = isEmpty(state.calculatedObjectsRules);
|
|
60
|
+
for (const objectKey of Object.keys(state.calculatedObjectsObjects)) {
|
|
61
|
+
const calculatedObjectsEffectScope = effectScope();
|
|
62
|
+
calculatedObjectsEffectScope.run(() => {
|
|
63
|
+
const calculatedObjectsObject = state.calculatedObjectsObjects[objectKey];
|
|
64
|
+
const originalObject = parentState.objects[objectKey];
|
|
65
|
+
if (!calculatedObjectsObject[copn]) {
|
|
66
|
+
calculatedObjectsObject[copn] = {};
|
|
67
|
+
}
|
|
68
|
+
let removedRuleKeys, addedRuleKeys;
|
|
69
|
+
if (!calculatedObjectsRulesIsEmpty) {
|
|
70
|
+
({ removedKeys: removedRuleKeys, addedKeys: addedRuleKeys } = keyDiff(
|
|
71
|
+
Object.keys(state.calculatedObjectsRules),
|
|
72
|
+
Object.keys(calculatedObjectsObject[copn])
|
|
73
|
+
));
|
|
74
|
+
} else {
|
|
75
|
+
if (isEmpty(calculatedObjectsObject[copn])) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
removedRuleKeys = Object.keys(calculatedObjectsObject[copn]);
|
|
79
|
+
addedRuleKeys = [];
|
|
80
|
+
}
|
|
81
|
+
for (const removedRuleKey of removedRuleKeys) {
|
|
82
|
+
delete calculatedObjectsObject[copn][removedRuleKey];
|
|
83
|
+
}
|
|
84
|
+
for (const addedRuleKey of addedRuleKeys) {
|
|
85
|
+
calculatedObjectsObject[copn][addedRuleKey] = computed(() => {
|
|
86
|
+
return state.calculatedObjectsRules?.[addedRuleKey]?.(originalObject);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
calculatedObjectsEffectScopes[objectKey] = calculatedObjectsEffectScope;
|
|
91
|
+
}
|
|
92
|
+
parentStateObjectsWatch();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const es = effectScope();
|
|
96
|
+
|
|
97
|
+
es.run(() => {
|
|
98
|
+
state.order = toRef(parentState, "order");
|
|
99
|
+
state.objectsInOrder = computed(() => state.order.map((id) => state.objects[id]));
|
|
100
|
+
|
|
101
|
+
watch(() => Object.keys(parentState.objects), parentStateObjectsWatch, { immediate: true });
|
|
102
|
+
watch(
|
|
103
|
+
[
|
|
104
|
+
() => Object.keys(state.calculatedObjectsObjects),
|
|
105
|
+
() =>
|
|
106
|
+
state.calculatedObjectsRules
|
|
107
|
+
? Object.keys(state.calculatedObjectsRules)
|
|
108
|
+
: state.calculatedObjectsRules,
|
|
109
|
+
],
|
|
110
|
+
calculatedObjectsWatch,
|
|
111
|
+
{ immediate: true }
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
onScopeDispose(() => {
|
|
115
|
+
for (const objectKey of Object.keys(calculatedObjectsEffectScopes)) {
|
|
116
|
+
calculatedObjectsEffectScopes[objectKey].stop();
|
|
117
|
+
}
|
|
118
|
+
for (const objectKey of Object.keys(combinedEffectScopes)) {
|
|
119
|
+
combinedEffectScopes[objectKey].stop();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
state,
|
|
126
|
+
effectScope: es,
|
|
127
|
+
};
|
|
128
|
+
}
|
package/use/listInstance.js
CHANGED
|
@@ -152,6 +152,11 @@ export function useListInstance({ crudArgs, listArgs = {}, retrieveArgs = {} })
|
|
|
152
152
|
return getFakeId(state.objects);
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
+
function clearError() {
|
|
156
|
+
state.errored = false;
|
|
157
|
+
state.error = null;
|
|
158
|
+
}
|
|
159
|
+
|
|
155
160
|
const es = effectScope();
|
|
156
161
|
|
|
157
162
|
es.run(() => {
|
|
@@ -165,10 +170,11 @@ export function useListInstance({ crudArgs, listArgs = {}, retrieveArgs = {} })
|
|
|
165
170
|
updateListObject,
|
|
166
171
|
deleteListObject,
|
|
167
172
|
clearList,
|
|
168
|
-
|
|
173
|
+
clearError,
|
|
169
174
|
getFakeId: ourGetFakeId,
|
|
170
175
|
defaultPageCallback,
|
|
171
176
|
pageCallback: defaultPageCallback,
|
|
177
|
+
effectScope: es,
|
|
172
178
|
};
|
|
173
179
|
return returnedObject;
|
|
174
180
|
}
|
package/use/listSubscription.js
CHANGED
|
@@ -138,6 +138,12 @@ export function useListSubscription({ listInstance, crudArgs, listArgs, retrieve
|
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
+
function clearErrors() {
|
|
142
|
+
state.subscriptionErrored = false;
|
|
143
|
+
state.subscriptionError = null;
|
|
144
|
+
listInstance.clearErrors();
|
|
145
|
+
}
|
|
146
|
+
|
|
141
147
|
const es = effectScope();
|
|
142
148
|
|
|
143
149
|
es.run(() => {
|
|
@@ -191,6 +197,7 @@ export function useListSubscription({ listInstance, crudArgs, listArgs, retrieve
|
|
|
191
197
|
subscribeIntent,
|
|
192
198
|
subscribe: publicSubscribe,
|
|
193
199
|
unsubscribe: publicUnsubscribe,
|
|
200
|
+
clearErrors,
|
|
194
201
|
effectScope: es,
|
|
195
202
|
};
|
|
196
203
|
}
|
package/use/objectInstance.js
CHANGED
|
@@ -209,6 +209,11 @@ export function useObjectInstance({ crudArgs, id, retrieveArgs }) {
|
|
|
209
209
|
});
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
+
function clearError() {
|
|
213
|
+
state.errored = false;
|
|
214
|
+
state.error = null;
|
|
215
|
+
}
|
|
216
|
+
|
|
212
217
|
const es = effectScope();
|
|
213
218
|
|
|
214
219
|
// we could have effects? let's keep the interface to keep our options open to add without major changes.
|
|
@@ -221,6 +226,7 @@ export function useObjectInstance({ crudArgs, id, retrieveArgs }) {
|
|
|
221
226
|
update,
|
|
222
227
|
patch,
|
|
223
228
|
delete: deleteFn,
|
|
229
|
+
clearError,
|
|
224
230
|
effectScope: es,
|
|
225
231
|
};
|
|
226
232
|
}
|
|
@@ -147,6 +147,12 @@ export function useObjectSubscription({ objectInstance, crudArgs, id, retrieveAr
|
|
|
147
147
|
return didUnsubscribe;
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
+
function clearError() {
|
|
151
|
+
state.subscriptionErrored = false;
|
|
152
|
+
state.subscriptionError = null;
|
|
153
|
+
objectInstance.clearError();
|
|
154
|
+
}
|
|
155
|
+
|
|
150
156
|
const es = effectScope();
|
|
151
157
|
|
|
152
158
|
es.run(() => {
|
|
@@ -179,6 +185,7 @@ export function useObjectSubscription({ objectInstance, crudArgs, id, retrieveAr
|
|
|
179
185
|
unsubscribe: publicUnsubscribe,
|
|
180
186
|
updateFromSubscription,
|
|
181
187
|
deleteFromSubscription,
|
|
188
|
+
clearError,
|
|
182
189
|
effectScope: es,
|
|
183
190
|
};
|
|
184
191
|
}
|