@arrai-innovations/reactive-helpers 1.2.3 → 2.0.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/.lintstagedrc +3 -0
- package/README.md +21 -16
- package/package.json +1 -1
- package/tests/unit/crudPromise.js +23 -0
- package/tests/unit/poll.js +50 -0
- package/tests/unit/use/listInstance.spec.js +47 -89
- package/tests/unit/use/listSort.spec.js +1 -1
- package/tests/unit/use/listSubscription.spec.js +184 -104
- package/tests/unit/use/objectInstance.spec.js +156 -147
- package/tests/unit/use/objectSubscription.spec.js +144 -120
- package/tests/unit/use/watches.spec.js +2 -2
- package/use/listInstance.js +39 -28
- package/use/listSubscription.js +80 -34
- package/use/objectInstance.js +34 -43
- package/use/objectSubscription.js +66 -71
- package/use/paginatedListInstance.js +2 -2
- package/utils/cancellableIntent.js +93 -0
- package/utils/index.js +1 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useListInstance } from "./listInstance";
|
|
2
2
|
|
|
3
|
-
export function usePagedListInstance({ crudArgs,
|
|
4
|
-
const listInstance = useListInstance({ crudArgs,
|
|
3
|
+
export function usePagedListInstance({ crudArgs, listArgs = {}, retrieveArgs = {} }) {
|
|
4
|
+
const listInstance = useListInstance({ crudArgs, listArgs, retrieveArgs });
|
|
5
5
|
|
|
6
6
|
listInstance.state.totalRecords = 0;
|
|
7
7
|
listInstance.state.totalPages = 0;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { effectScope, isReactive, isRef, onScopeDispose, reactive, unref, watch } from "vue";
|
|
2
|
+
import { cloneDeep, isEqual } from "lodash";
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Calls your awaitable function with the arguments you pass in, when the watch arguments change and are all truthy.
|
|
6
|
+
* Watch arguments can be an array or an object.
|
|
7
|
+
* If the promise is not resolved before the watch arguments change again, the previous promise is cancelled.
|
|
8
|
+
*/
|
|
9
|
+
export function useCancellableIntent({ awaitableWithCancel, watchArguments = {}, clearActiveOnResolved = true }) {
|
|
10
|
+
if (!awaitableWithCancel) {
|
|
11
|
+
throw new Error("awaitableWithCancel is required");
|
|
12
|
+
}
|
|
13
|
+
if (typeof awaitableWithCancel !== "function") {
|
|
14
|
+
throw new Error("awaitableWithCancel must be a function");
|
|
15
|
+
}
|
|
16
|
+
const state = reactive({
|
|
17
|
+
active: undefined,
|
|
18
|
+
errored: false,
|
|
19
|
+
error: null,
|
|
20
|
+
clearActiveOnResolved,
|
|
21
|
+
});
|
|
22
|
+
let previousWatchArguments = null,
|
|
23
|
+
cancelFunction = null;
|
|
24
|
+
|
|
25
|
+
const es = effectScope();
|
|
26
|
+
|
|
27
|
+
function stop() {
|
|
28
|
+
// effect scopes are stopped automatically in onUnmounted / a parent onScopeDispose; this is for other use cases
|
|
29
|
+
es.stop();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function cancel() {
|
|
33
|
+
if (cancelFunction) {
|
|
34
|
+
state.active = false;
|
|
35
|
+
const cancelPromise = cancelFunction().catch(console.error);
|
|
36
|
+
cancelFunction = null;
|
|
37
|
+
return cancelPromise;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
es.run(() => {
|
|
43
|
+
watch(
|
|
44
|
+
isReactive(watchArguments) || isRef(watchArguments) ? watchArguments : Object.values(watchArguments),
|
|
45
|
+
() => {
|
|
46
|
+
let newArguments = cloneDeep(watchArguments.map((arg) => unref(arg)));
|
|
47
|
+
if (isEqual(unref(watchArguments), previousWatchArguments)) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
previousWatchArguments = newArguments;
|
|
51
|
+
cancel().catch(console.error);
|
|
52
|
+
if (Object.values(previousWatchArguments).every((v) => unref(v))) {
|
|
53
|
+
state.errored = false;
|
|
54
|
+
state.error = null;
|
|
55
|
+
let awaitablePromise = awaitableWithCancel();
|
|
56
|
+
state.active = true;
|
|
57
|
+
if (awaitablePromise.cancel) {
|
|
58
|
+
cancelFunction = async () => {
|
|
59
|
+
state.active = false;
|
|
60
|
+
cancelFunction = null;
|
|
61
|
+
return awaitablePromise.cancel();
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
awaitablePromise
|
|
65
|
+
.then(() => {
|
|
66
|
+
if (state.clearActiveOnResolved) {
|
|
67
|
+
cancelFunction = null;
|
|
68
|
+
state.active = false;
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
.catch(async (err) => {
|
|
72
|
+
await cancel();
|
|
73
|
+
console.error(err);
|
|
74
|
+
state.errored = true;
|
|
75
|
+
state.error = err;
|
|
76
|
+
throw err;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
deep: true,
|
|
82
|
+
immediate: true,
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
onScopeDispose(cancel);
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
state,
|
|
90
|
+
stop,
|
|
91
|
+
cancel,
|
|
92
|
+
};
|
|
93
|
+
}
|