@esportsplus/reactivity 0.4.8 → 0.6.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.
@@ -0,0 +1,67 @@
1
+ import CustomFunction from '@esportsplus/custom-function';
2
+ import { read, root, signal } from '~/signal';
3
+ import { Signal } from '~/types';
4
+
5
+
6
+ let { set } = signal;
7
+
8
+
9
+ class ReactivePromise<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
10
+ private arguments: Signal<A | null>;
11
+ private okay: Signal<boolean | null>;
12
+ private response: Signal<Awaited<R> | null>;
13
+
14
+ stop: boolean | null = null;
15
+
16
+
17
+ constructor(fn: (...args: A) => R) {
18
+ super((...args: A) => {
19
+ this.stop = null;
20
+
21
+ set(this.arguments, args);
22
+ set(this.okay, null);
23
+
24
+ return root(() => {
25
+ return fn(...args)
26
+ .then((value) => {
27
+ if (this.stop === true) {
28
+ return;
29
+ }
30
+
31
+ set(this.response, value as Awaited<R>);
32
+ set(this.okay, true);
33
+ })
34
+ .catch(() => {
35
+ if (this.stop === true) {
36
+ return;
37
+ }
38
+
39
+ set(this.response, null);
40
+ set(this.okay, false);
41
+ });
42
+ });
43
+ });
44
+
45
+ this.response = signal(null);
46
+ this.arguments = signal(null);
47
+ this.okay = signal(null);
48
+ }
49
+
50
+
51
+ get data() {
52
+ return read(this.response);
53
+ }
54
+
55
+ get input() {
56
+ return read(this.arguments);
57
+ }
58
+
59
+ get ok() {
60
+ return read(this.okay);
61
+ }
62
+ }
63
+
64
+
65
+ export default <A extends unknown[], R extends Promise<unknown>>(fn: (...args: A) => R) => {
66
+ return new ReactivePromise(fn);
67
+ };