@esportsplus/reactivity 0.4.5 → 0.4.7
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/build/index.d.ts +0 -1
- package/build/index.js +0 -1
- package/build/reactive/index.d.ts +2 -2
- package/build/reactive/index.js +1 -1
- package/build/reactive/object.d.ts +1 -2
- package/build/reactive/object.js +19 -9
- package/build/signal.js +4 -4
- package/build/types.d.ts +6 -5
- package/package.json +2 -2
- package/src/index.ts +0 -1
- package/src/reactive/index.ts +3 -5
- package/src/reactive/object.ts +20 -9
- package/src/signal.ts +4 -4
- package/src/types.ts +9 -5
- package/build/resource.d.ts +0 -15
- package/build/resource.js +0 -50
- package/src/resource.ts +0 -68
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Options,
|
|
1
|
+
import { Options, Reactive } from '../types.js';
|
|
2
2
|
type Guard<T> = T extends {
|
|
3
3
|
dispose: any;
|
|
4
4
|
} | {
|
|
@@ -6,5 +6,5 @@ type Guard<T> = T extends {
|
|
|
6
6
|
} ? {
|
|
7
7
|
never: '[ dispose, signals ] are reserved keys';
|
|
8
8
|
} : T extends Record<PropertyKey, unknown> | unknown[] ? T : never;
|
|
9
|
-
declare const _default: <T>(data: Guard<T>, options?: Options) =>
|
|
9
|
+
declare const _default: <T>(data: Guard<T>, options?: Options) => Reactive<T>;
|
|
10
10
|
export default _default;
|
package/build/reactive/index.js
CHANGED
|
@@ -10,7 +10,7 @@ export default (data, options = {}) => {
|
|
|
10
10
|
value = object(data, options);
|
|
11
11
|
}
|
|
12
12
|
else {
|
|
13
|
-
throw new Error(
|
|
13
|
+
throw new Error(`@esportsplus/reactivity: 'reactive' received invalid input - ${JSON.stringify(data)}`);
|
|
14
14
|
}
|
|
15
15
|
return value;
|
|
16
16
|
};
|
|
@@ -4,6 +4,5 @@ type API<T> = Prettify<{
|
|
|
4
4
|
} & {
|
|
5
5
|
dispose: VoidFunction;
|
|
6
6
|
}>;
|
|
7
|
-
|
|
8
|
-
export default _default;
|
|
7
|
+
export default function object<T extends Record<PropertyKey, unknown>>(input: T, options?: Options): API<T>;
|
|
9
8
|
export type { API as ReactiveObject };
|
package/build/reactive/object.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineProperty, isArray, isFunction } from '@esportsplus/utilities';
|
|
1
|
+
import { defineProperty, isArray, isFunction, isObject } from '@esportsplus/utilities';
|
|
2
2
|
import { computed, signal } from '../signal.js';
|
|
3
3
|
import { default as array } from './array.js';
|
|
4
4
|
class ReactiveObject {
|
|
@@ -6,9 +6,9 @@ class ReactiveObject {
|
|
|
6
6
|
constructor(data, options = {}) {
|
|
7
7
|
let signals = this.signals;
|
|
8
8
|
for (let key in data) {
|
|
9
|
-
let
|
|
10
|
-
if (isArray(
|
|
11
|
-
let s = signals[key] = array(
|
|
9
|
+
let value = data[key];
|
|
10
|
+
if (isArray(value)) {
|
|
11
|
+
let s = signals[key] = array(value, options);
|
|
12
12
|
defineProperty(this, key, {
|
|
13
13
|
enumerable: true,
|
|
14
14
|
get() {
|
|
@@ -16,8 +16,8 @@ class ReactiveObject {
|
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
|
-
else if (isFunction(
|
|
20
|
-
let s = signals[key] = computed(
|
|
19
|
+
else if (isFunction(value)) {
|
|
20
|
+
let s = signals[key] = computed(value, options);
|
|
21
21
|
defineProperty(this, key, {
|
|
22
22
|
enumerable: true,
|
|
23
23
|
get() {
|
|
@@ -25,8 +25,17 @@ class ReactiveObject {
|
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
|
+
else if (isObject(value)) {
|
|
29
|
+
let s = signals[key] = new ReactiveObject(value, options);
|
|
30
|
+
defineProperty(this, key, {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get() {
|
|
33
|
+
return s;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
28
37
|
else {
|
|
29
|
-
let s = signals[key] = signal(
|
|
38
|
+
let s = signals[key] = signal(value, options);
|
|
30
39
|
defineProperty(this, key, {
|
|
31
40
|
enumerable: true,
|
|
32
41
|
get() {
|
|
@@ -46,6 +55,7 @@ class ReactiveObject {
|
|
|
46
55
|
}
|
|
47
56
|
}
|
|
48
57
|
}
|
|
49
|
-
export default (input, options = {})
|
|
58
|
+
export default function object(input, options = {}) {
|
|
50
59
|
return new ReactiveObject(input, options);
|
|
51
|
-
}
|
|
60
|
+
}
|
|
61
|
+
;
|
package/build/signal.js
CHANGED
|
@@ -25,7 +25,7 @@ class Reactive {
|
|
|
25
25
|
}
|
|
26
26
|
if (root == null) {
|
|
27
27
|
if (type === EFFECT) {
|
|
28
|
-
throw new Error(
|
|
28
|
+
throw new Error(`@esportsplus/reactivity: 'effect' cannot be created without a reactive root`);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
else if (root.tracking) {
|
|
@@ -95,7 +95,7 @@ class Reactive {
|
|
|
95
95
|
on(event, listener) {
|
|
96
96
|
if (this.state === DIRTY) {
|
|
97
97
|
if (event !== 'cleanup') {
|
|
98
|
-
throw new Error(
|
|
98
|
+
throw new Error(`@esportsplus/reactivity: events set within computed or effects must use the 'cleanup' event name`);
|
|
99
99
|
}
|
|
100
100
|
listener.once = true;
|
|
101
101
|
}
|
|
@@ -124,7 +124,7 @@ class Reactive {
|
|
|
124
124
|
}
|
|
125
125
|
set(value) {
|
|
126
126
|
if (this.type !== SIGNAL && observer !== this) {
|
|
127
|
-
throw new Error(
|
|
127
|
+
throw new Error(`@esportsplus/reactivity: 'set' method is only available on signals`);
|
|
128
128
|
}
|
|
129
129
|
if (this.changed(this.value, value)) {
|
|
130
130
|
this.value = value;
|
|
@@ -258,7 +258,7 @@ const root = (fn, scheduler) => {
|
|
|
258
258
|
scope = o.root;
|
|
259
259
|
}
|
|
260
260
|
if (scope === null) {
|
|
261
|
-
throw new Error('
|
|
261
|
+
throw new Error('@esportsplus/reactivity: `root` cannot be created without a task scheduler');
|
|
262
262
|
}
|
|
263
263
|
scheduler = scope.scheduler;
|
|
264
264
|
}
|
package/build/types.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { Function, NeverAsync, Prettify } from '@esportsplus/utilities';
|
|
|
2
2
|
import { ReactiveArray } from './reactive/array.js';
|
|
3
3
|
import { ReactiveObject } from './reactive/object.js';
|
|
4
4
|
import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL } from './constants.js';
|
|
5
|
-
import { Reactive } from './signal.js';
|
|
6
|
-
type Base<T> = Omit<
|
|
5
|
+
import { Reactive as ReactiveBase } from './signal.js';
|
|
6
|
+
type Base<T> = Omit<ReactiveBase<T>, 'changed' | 'fn' | 'get' | 'scheduler' | 'set' | 'task' | 'tracking'>;
|
|
7
7
|
type Changed = (a: unknown, b: unknown) => boolean;
|
|
8
8
|
type Computed<T> = {
|
|
9
9
|
changed: Changed;
|
|
@@ -18,7 +18,7 @@ type Effect = {
|
|
|
18
18
|
type Infer<T> = T extends (...args: unknown[]) => unknown ? ReturnType<T> : T extends (infer U)[] ? ReactiveArray<U> : T extends ReactiveObject<T> ? ReactiveObject<T> : T extends Record<PropertyKey, unknown> ? {
|
|
19
19
|
[K in keyof T]: T[K];
|
|
20
20
|
} : T;
|
|
21
|
-
type Event = string;
|
|
21
|
+
type Event = 'cleanup' | 'dispose' | 'update' | string;
|
|
22
22
|
type Listener<D> = {
|
|
23
23
|
once?: boolean;
|
|
24
24
|
<V>(data: D, value: V): void;
|
|
@@ -26,11 +26,12 @@ type Listener<D> = {
|
|
|
26
26
|
type Options = {
|
|
27
27
|
changed?: Changed;
|
|
28
28
|
};
|
|
29
|
+
type Reactive<T> = T extends Record<PropertyKey, unknown> ? ReactiveObject<T> : ReactiveArray<T>;
|
|
29
30
|
type Root = {
|
|
30
31
|
scheduler: Scheduler;
|
|
31
32
|
tracking: boolean;
|
|
32
33
|
value: void;
|
|
33
|
-
} & Omit<
|
|
34
|
+
} & Omit<ReactiveBase<void>, 'root'>;
|
|
34
35
|
type Scheduler = (fn: Function) => unknown;
|
|
35
36
|
type Signal<T> = {
|
|
36
37
|
changed: Changed;
|
|
@@ -39,4 +40,4 @@ type Signal<T> = {
|
|
|
39
40
|
} & Base<T>;
|
|
40
41
|
type State = typeof CHECK | typeof CLEAN | typeof DIRTY | typeof DISPOSED;
|
|
41
42
|
type Type = typeof COMPUTED | typeof EFFECT | typeof ROOT | typeof SIGNAL;
|
|
42
|
-
export type { Changed, Computed, Effect, Event, Function, Infer, Listener, NeverAsync, Options, Prettify, ReactiveArray, ReactiveObject, Root, Scheduler, Signal, State, Type };
|
|
43
|
+
export type { Changed, Computed, Effect, Event, Function, Infer, Listener, NeverAsync, Options, Prettify, Reactive, ReactiveArray, ReactiveObject, Root, Scheduler, Signal, State, Type };
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
4
|
"@esportsplus/custom-function": "^0.0.12",
|
|
5
|
-
"@esportsplus/utilities": "^0.
|
|
5
|
+
"@esportsplus/utilities": "^0.15.0"
|
|
6
6
|
},
|
|
7
7
|
"devDependencies": {
|
|
8
8
|
"@esportsplus/typescript": "^0.9.1"
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"private": false,
|
|
13
13
|
"type": "module",
|
|
14
14
|
"types": "build/index.d.ts",
|
|
15
|
-
"version": "0.4.
|
|
15
|
+
"version": "0.4.7",
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "tsc && tsc-alias",
|
|
18
18
|
"-": "-"
|
package/src/index.ts
CHANGED
package/src/reactive/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isArray, isObject } from '@esportsplus/utilities';
|
|
2
|
-
import { Options,
|
|
2
|
+
import { Options, Reactive } from '~/types';
|
|
3
3
|
import { default as array } from './array';
|
|
4
4
|
import { default as object } from './object';
|
|
5
5
|
|
|
@@ -22,10 +22,8 @@ export default <T>(data: Guard<T>, options: Options = {}) => {
|
|
|
22
22
|
value = object(data as { [K in keyof T]: T[K] }, options);
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
|
-
throw new Error(
|
|
25
|
+
throw new Error(`@esportsplus/reactivity: 'reactive' received invalid input - ${JSON.stringify(data)}`);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
return value as T
|
|
29
|
-
? ReactiveObject<T>
|
|
30
|
-
: ReactiveArray<T>;
|
|
28
|
+
return value as Reactive<T>;
|
|
31
29
|
};
|
package/src/reactive/object.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineProperty, isArray, isFunction } from '@esportsplus/utilities';
|
|
1
|
+
import { defineProperty, isArray, isFunction, isObject } from '@esportsplus/utilities';
|
|
2
2
|
import { computed, signal } from '~/signal';
|
|
3
3
|
import { Computed, Infer, Options, Prettify, ReactiveArray, Signal } from '~/types';
|
|
4
4
|
import { default as array } from './array';
|
|
@@ -8,17 +8,17 @@ type API<T> = Prettify< { [K in keyof T]: Infer<T[K]> } & { dispose: VoidFunctio
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
11
|
-
signals: Record<PropertyKey, Computed<any> | ReactiveArray<any> | Signal<any>> = {};
|
|
11
|
+
signals: Record<PropertyKey, Computed<any> | ReactiveArray<any> | ReactiveObject<any> | Signal<any>> = {};
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
constructor(data: T, options: Options = {}) {
|
|
15
15
|
let signals = this.signals;
|
|
16
16
|
|
|
17
17
|
for (let key in data) {
|
|
18
|
-
let
|
|
18
|
+
let value = data[key];
|
|
19
19
|
|
|
20
|
-
if (isArray(
|
|
21
|
-
let s = signals[key] = array(
|
|
20
|
+
if (isArray(value)) {
|
|
21
|
+
let s = signals[key] = array(value, options);
|
|
22
22
|
|
|
23
23
|
defineProperty(this, key, {
|
|
24
24
|
enumerable: true,
|
|
@@ -27,8 +27,8 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
-
else if (isFunction(
|
|
31
|
-
let s = signals[key] = computed(
|
|
30
|
+
else if (isFunction(value)) {
|
|
31
|
+
let s = signals[key] = computed(value as Computed<T>['fn'], options);
|
|
32
32
|
|
|
33
33
|
defineProperty(this, key, {
|
|
34
34
|
enumerable: true,
|
|
@@ -37,8 +37,19 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
37
37
|
}
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
|
+
else if (isObject(value)) {
|
|
41
|
+
// Type issue with factory function below, fix after testing, if this is kept
|
|
42
|
+
let s = signals[key] = new ReactiveObject(value, options);
|
|
43
|
+
|
|
44
|
+
defineProperty(this, key, {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
get() {
|
|
47
|
+
return s;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
40
51
|
else {
|
|
41
|
-
let s = signals[key] = signal(
|
|
52
|
+
let s = signals[key] = signal(value, options);
|
|
42
53
|
|
|
43
54
|
defineProperty(this, key, {
|
|
44
55
|
enumerable: true,
|
|
@@ -64,7 +75,7 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
64
75
|
}
|
|
65
76
|
|
|
66
77
|
|
|
67
|
-
export default <T extends Record<PropertyKey, unknown>>(input: T, options: Options = {})
|
|
78
|
+
export default function object<T extends Record<PropertyKey, unknown>>(input: T, options: Options = {}) {
|
|
68
79
|
return new ReactiveObject(input, options) as API<T>;
|
|
69
80
|
};
|
|
70
81
|
export type { API as ReactiveObject };
|
package/src/signal.ts
CHANGED
|
@@ -37,7 +37,7 @@ class Reactive<T> {
|
|
|
37
37
|
|
|
38
38
|
if (root == null) {
|
|
39
39
|
if (type === EFFECT) {
|
|
40
|
-
throw new Error(
|
|
40
|
+
throw new Error(`@esportsplus/reactivity: 'effect' cannot be created without a reactive root`);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
else if (root.tracking) {
|
|
@@ -124,7 +124,7 @@ class Reactive<T> {
|
|
|
124
124
|
on<T>(event: Event, listener: Listener<T>) {
|
|
125
125
|
if (this.state === DIRTY) {
|
|
126
126
|
if (event !== 'cleanup') {
|
|
127
|
-
throw new Error(
|
|
127
|
+
throw new Error(`@esportsplus/reactivity: events set within computed or effects must use the 'cleanup' event name`);
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
listener.once = true;
|
|
@@ -159,7 +159,7 @@ class Reactive<T> {
|
|
|
159
159
|
|
|
160
160
|
set(value: T): T {
|
|
161
161
|
if (this.type !== SIGNAL && observer !== this) {
|
|
162
|
-
throw new Error(
|
|
162
|
+
throw new Error(`@esportsplus/reactivity: 'set' method is only available on signals`);
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
if (this.changed!(this.value, value)) {
|
|
@@ -339,7 +339,7 @@ const root = <T>(fn: NeverAsync<(instance: Root) => T>, scheduler?: Scheduler) =
|
|
|
339
339
|
}
|
|
340
340
|
|
|
341
341
|
if (scope === null) {
|
|
342
|
-
throw new Error('
|
|
342
|
+
throw new Error('@esportsplus/reactivity: `root` cannot be created without a task scheduler');
|
|
343
343
|
}
|
|
344
344
|
|
|
345
345
|
scheduler = scope.scheduler;
|
package/src/types.ts
CHANGED
|
@@ -2,10 +2,10 @@ import { Function, NeverAsync, Prettify } from '@esportsplus/utilities'
|
|
|
2
2
|
import { ReactiveArray } from './reactive/array';
|
|
3
3
|
import { ReactiveObject } from './reactive/object';
|
|
4
4
|
import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL } from './constants';
|
|
5
|
-
import { Reactive } from './signal';
|
|
5
|
+
import { Reactive as ReactiveBase } from './signal';
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
type Base<T> = Omit<
|
|
8
|
+
type Base<T> = Omit<ReactiveBase<T>, 'changed' | 'fn' | 'get' | 'scheduler' | 'set' | 'task' | 'tracking'>;
|
|
9
9
|
|
|
10
10
|
type Changed = (a: unknown, b: unknown) => boolean;
|
|
11
11
|
|
|
@@ -32,7 +32,7 @@ type Infer<T> =
|
|
|
32
32
|
? { [K in keyof T]: T[K] }
|
|
33
33
|
: T;
|
|
34
34
|
|
|
35
|
-
type Event = string;
|
|
35
|
+
type Event = 'cleanup' | 'dispose' | 'update' | string;
|
|
36
36
|
|
|
37
37
|
type Listener<D> = {
|
|
38
38
|
once?: boolean;
|
|
@@ -44,11 +44,15 @@ type Options = {
|
|
|
44
44
|
changed?: Changed;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
+
type Reactive<T> = T extends Record<PropertyKey, unknown>
|
|
48
|
+
? ReactiveObject<T>
|
|
49
|
+
: ReactiveArray<T>;
|
|
50
|
+
|
|
47
51
|
type Root = {
|
|
48
52
|
scheduler: Scheduler;
|
|
49
53
|
tracking: boolean;
|
|
50
54
|
value: void;
|
|
51
|
-
} & Omit<
|
|
55
|
+
} & Omit<ReactiveBase<void>, 'root'>;
|
|
52
56
|
|
|
53
57
|
type Scheduler = (fn: Function) => unknown;
|
|
54
58
|
|
|
@@ -72,7 +76,7 @@ export type {
|
|
|
72
76
|
NeverAsync,
|
|
73
77
|
Options,
|
|
74
78
|
Prettify,
|
|
75
|
-
ReactiveArray, ReactiveObject, Root,
|
|
79
|
+
Reactive, ReactiveArray, ReactiveObject, Root,
|
|
76
80
|
Scheduler, Signal, State,
|
|
77
81
|
Type
|
|
78
82
|
};
|
package/build/resource.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import CustomFunction from '@esportsplus/custom-function';
|
|
2
|
-
import { Options } from './types.js';
|
|
3
|
-
declare class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
|
|
4
|
-
private arguments;
|
|
5
|
-
private okay;
|
|
6
|
-
private response;
|
|
7
|
-
stop: boolean | null;
|
|
8
|
-
constructor(fn: (...args: A) => R, options?: Options);
|
|
9
|
-
get data(): Awaited<R>;
|
|
10
|
-
get input(): A | null;
|
|
11
|
-
get ok(): boolean | null;
|
|
12
|
-
dispose(): void;
|
|
13
|
-
}
|
|
14
|
-
declare const _default: <A extends unknown[], R extends Promise<unknown>>(fn: (...args: A) => R, options?: Options) => Resource<A, R>;
|
|
15
|
-
export default _default;
|
package/build/resource.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import CustomFunction from '@esportsplus/custom-function';
|
|
2
|
-
import { signal } from './signal.js';
|
|
3
|
-
class Resource extends CustomFunction {
|
|
4
|
-
arguments;
|
|
5
|
-
okay;
|
|
6
|
-
response;
|
|
7
|
-
stop = null;
|
|
8
|
-
constructor(fn, options = {}) {
|
|
9
|
-
super((...args) => {
|
|
10
|
-
this.stop = null;
|
|
11
|
-
this.arguments.set(args);
|
|
12
|
-
this.okay.set(null);
|
|
13
|
-
fn(...args)
|
|
14
|
-
.then((value) => {
|
|
15
|
-
if (this.stop === true) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
this.response.set(value);
|
|
19
|
-
this.okay.set(true);
|
|
20
|
-
})
|
|
21
|
-
.catch(() => {
|
|
22
|
-
if (this.stop === true) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
this.response.set(undefined);
|
|
26
|
-
this.okay.set(false);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
this.response = signal(undefined, options);
|
|
30
|
-
this.arguments = signal(null, options);
|
|
31
|
-
this.okay = signal(null, options);
|
|
32
|
-
}
|
|
33
|
-
get data() {
|
|
34
|
-
return this.response.get();
|
|
35
|
-
}
|
|
36
|
-
get input() {
|
|
37
|
-
return this.arguments.get();
|
|
38
|
-
}
|
|
39
|
-
get ok() {
|
|
40
|
-
return this.okay.get();
|
|
41
|
-
}
|
|
42
|
-
dispose() {
|
|
43
|
-
this.arguments.dispose();
|
|
44
|
-
this.okay.dispose();
|
|
45
|
-
this.response.dispose();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
export default (fn, options = {}) => {
|
|
49
|
-
return new Resource(fn, options);
|
|
50
|
-
};
|
package/src/resource.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import CustomFunction from '@esportsplus/custom-function';
|
|
2
|
-
import { signal } from './signal';
|
|
3
|
-
import { Options, Signal } from './types';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
|
|
7
|
-
private arguments: Signal<A | null>;
|
|
8
|
-
private okay: Signal<boolean | null>;
|
|
9
|
-
private response: Signal<Awaited<R>>;
|
|
10
|
-
|
|
11
|
-
stop: boolean | null = null;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
constructor(fn: (...args: A) => R, options: Options = {}) {
|
|
15
|
-
super((...args: A) => {
|
|
16
|
-
this.stop = null;
|
|
17
|
-
|
|
18
|
-
this.arguments.set(args);
|
|
19
|
-
this.okay.set(null);
|
|
20
|
-
|
|
21
|
-
fn(...args)
|
|
22
|
-
.then((value) => {
|
|
23
|
-
if (this.stop === true) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
this.response.set(value as Awaited<R>);
|
|
28
|
-
this.okay.set(true);
|
|
29
|
-
})
|
|
30
|
-
.catch(() => {
|
|
31
|
-
if (this.stop === true) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
this.response.set(undefined as Awaited<R>);
|
|
36
|
-
this.okay.set(false);
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
this.response = signal(undefined as Awaited<R>, options);
|
|
40
|
-
this.arguments = signal<A | null>(null, options);
|
|
41
|
-
this.okay = signal<boolean | null>(null, options);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
get data() {
|
|
46
|
-
return this.response.get();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get input() {
|
|
50
|
-
return this.arguments.get();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get ok() {
|
|
54
|
-
return this.okay.get();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
dispose() {
|
|
59
|
-
this.arguments.dispose();
|
|
60
|
-
this.okay.dispose();
|
|
61
|
-
this.response.dispose();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
export default <A extends unknown[], R extends Promise<unknown>>(fn: (...args: A) => R, options: Options = {}) => {
|
|
67
|
-
return new Resource(fn, options);
|
|
68
|
-
};
|