@esportsplus/reactivity 0.1.18 → 0.1.20

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