@esportsplus/reactivity 0.1.3 → 0.1.5
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 +1 -0
- package/build/index.js +1 -0
- package/build/macro.d.ts +2 -1
- package/build/resource.d.ts +2 -2
- package/build/signal.d.ts +6 -6
- package/build/signal.js +4 -4
- package/build/types.d.ts +4 -3
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/macro.ts +1 -1
- package/src/reactive/object.ts +1 -1
- package/src/resource.ts +2 -2
- package/src/signal.ts +7 -7
- package/src/types.ts +5 -3
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
package/build/macro.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import CustomFunction from '@esportsplus/custom-function';
|
|
2
2
|
import { computed } from './signal';
|
|
3
|
-
|
|
3
|
+
import { Computed } from './types';
|
|
4
|
+
type Fn<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
|
|
4
5
|
type Options = Parameters<typeof computed>[1];
|
|
5
6
|
declare class Macro<A extends unknown[], R> extends CustomFunction {
|
|
6
7
|
#private;
|
package/build/resource.d.ts
CHANGED
|
@@ -6,9 +6,9 @@ declare class Resource<A extends unknown[], R extends Promise<unknown>> extends
|
|
|
6
6
|
#private;
|
|
7
7
|
stop: boolean | null;
|
|
8
8
|
constructor(fn: Fn<A, R>, options?: Options);
|
|
9
|
-
get data(): Awaited<R> | ReturnType<Awaited<R
|
|
9
|
+
get data(): Awaited<R> | ReturnType<import("./types").PreventPromise<Awaited<R>, (previous: Awaited<R>) => Awaited<R>>>;
|
|
10
10
|
get ok(): boolean | null;
|
|
11
|
-
get input(): A | ReturnType<A
|
|
11
|
+
get input(): A | ReturnType<import("./types").PreventPromise<A, (previous: A | null) => A | null>> | null;
|
|
12
12
|
dispose(): void;
|
|
13
13
|
reset(): void;
|
|
14
14
|
}
|
package/build/signal.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Changed, Computed, Effect, Event, Listener, Options, Root, Scheduler, State, Type } from './types';
|
|
1
|
+
import { Changed, Computed, Effect, Event, Listener, PreventPromise, Options, Root, Scheduler, State, Type } from './types';
|
|
2
2
|
declare class Signal<T> {
|
|
3
3
|
changed: Changed | null;
|
|
4
4
|
fn: Computed<T>['fn'] | null;
|
|
@@ -18,19 +18,19 @@ declare class Signal<T> {
|
|
|
18
18
|
once(event: Event, listener: Listener<any>): void;
|
|
19
19
|
reset(): void;
|
|
20
20
|
}
|
|
21
|
-
declare const computed: <T>(fn: T
|
|
21
|
+
declare const computed: <T>(fn: PreventPromise<T, (previous: T) => T>, options?: Options) => Computed<T>;
|
|
22
22
|
declare const dispose: <T extends {
|
|
23
23
|
dispose: () => void;
|
|
24
24
|
}>(dispose?: T | T[] | undefined) => T | T[] | undefined;
|
|
25
|
-
declare const effect: <T>(fn: (node: Effect<T>) => void
|
|
26
|
-
declare const read: <T>(node: Signal<T>) => T | ReturnType<T
|
|
25
|
+
declare const effect: <T>(fn: PreventPromise<T, (node: Effect<T>) => void>, options?: Omit<Options, 'value'>) => Effect<void>;
|
|
26
|
+
declare const read: <T>(node: Signal<T>) => T | ReturnType<PreventPromise<T, (previous: T) => T>>;
|
|
27
27
|
declare const reset: <T extends {
|
|
28
28
|
reset: () => void;
|
|
29
29
|
}>(reset?: T | T[] | undefined) => T | T[] | undefined;
|
|
30
|
-
declare const root: <T>(fn: () => T
|
|
30
|
+
declare const root: <T>(fn: PreventPromise<T, () => T>, properties?: {
|
|
31
31
|
scheduler?: Scheduler;
|
|
32
32
|
}) => T;
|
|
33
33
|
declare const signal: <T>(data: T, options?: Omit<Options, 'value'>) => Signal<T>;
|
|
34
|
-
declare const write: <T>(node: Signal<T>, value: unknown) => T | ReturnType<T
|
|
34
|
+
declare const write: <T>(node: Signal<T>, value: unknown) => T | ReturnType<PreventPromise<T, (previous: T) => T>>;
|
|
35
35
|
export default Signal;
|
|
36
36
|
export { computed, dispose, effect, read, reset, root, signal, write };
|
package/build/signal.js
CHANGED
|
@@ -220,7 +220,7 @@ const effect = (fn, options = {}) => {
|
|
|
220
220
|
if (scope !== null) {
|
|
221
221
|
node.root = scope;
|
|
222
222
|
}
|
|
223
|
-
else if (observer !== null && observer.type === EFFECT
|
|
223
|
+
else if (observer !== null && observer.type === EFFECT) {
|
|
224
224
|
node.root = observer.root;
|
|
225
225
|
}
|
|
226
226
|
else {
|
|
@@ -228,7 +228,7 @@ const effect = (fn, options = {}) => {
|
|
|
228
228
|
}
|
|
229
229
|
node.fn = fn;
|
|
230
230
|
node.task = () => read(node);
|
|
231
|
-
|
|
231
|
+
read(node);
|
|
232
232
|
return node;
|
|
233
233
|
};
|
|
234
234
|
const read = (node) => {
|
|
@@ -269,10 +269,10 @@ const reset = (reset) => {
|
|
|
269
269
|
const root = (fn, properties = {}) => {
|
|
270
270
|
let o = observer, s = scope;
|
|
271
271
|
if (properties.scheduler === undefined) {
|
|
272
|
-
|
|
273
|
-
if (properties.scheduler === undefined) {
|
|
272
|
+
if (scope === null) {
|
|
274
273
|
throw new Error('Reactivity: `root` cannot be created without a task scheduler');
|
|
275
274
|
}
|
|
275
|
+
properties.scheduler = scope.scheduler;
|
|
276
276
|
}
|
|
277
277
|
observer = null;
|
|
278
278
|
scope = properties;
|
package/build/types.d.ts
CHANGED
|
@@ -3,11 +3,11 @@ import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, SIGNAL } from './const
|
|
|
3
3
|
import Signal from './signal';
|
|
4
4
|
type Changed = (a: unknown, b: unknown) => boolean;
|
|
5
5
|
type Computed<T> = {
|
|
6
|
-
fn: T
|
|
6
|
+
fn: PreventPromise<T, (previous: T) => T>;
|
|
7
7
|
value: ReturnType<Computed<T>['fn']>;
|
|
8
8
|
} & Omit<Signal<T>, 'fn' | 'value'>;
|
|
9
9
|
type Effect<T> = {
|
|
10
|
-
fn: (node: Effect<T>) => void
|
|
10
|
+
fn: PreventPromise<T, (node: Effect<T>) => void>;
|
|
11
11
|
root: NonNullable<Signal<T>['root']>;
|
|
12
12
|
task: NonNullable<Signal<T>['task']>;
|
|
13
13
|
value: void;
|
|
@@ -25,10 +25,11 @@ type Options = {
|
|
|
25
25
|
changed?: Changed;
|
|
26
26
|
value?: unknown;
|
|
27
27
|
};
|
|
28
|
+
type PreventPromise<T, R> = T extends Promise<unknown> ? never : R;
|
|
28
29
|
type Root = {
|
|
29
30
|
scheduler: Scheduler;
|
|
30
31
|
};
|
|
31
32
|
type Scheduler = (fn: (...args: unknown[]) => Promise<unknown> | unknown) => unknown;
|
|
32
33
|
type State = typeof CHECK | typeof CLEAN | typeof DIRTY | typeof DISPOSED;
|
|
33
34
|
type Type = typeof COMPUTED | typeof EFFECT | typeof SIGNAL;
|
|
34
|
-
export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, Root, Scheduler, Signal, State, Type };
|
|
35
|
+
export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, PreventPromise, Root, Scheduler, Signal, State, Type };
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -2,4 +2,5 @@ export { default as macro } from './macro';
|
|
|
2
2
|
export { default as resource } from './resource';
|
|
3
3
|
export * as core from './signal';
|
|
4
4
|
export { effect, root } from './signal';
|
|
5
|
-
export { default as reactive } from './reactive';
|
|
5
|
+
export { default as reactive } from './reactive';
|
|
6
|
+
export * from './types';
|
package/src/macro.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { computed, read } from './signal';
|
|
|
3
3
|
import { Computed } from './types';
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
type Fn<A extends unknown[], R> =
|
|
6
|
+
type Fn<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
|
|
7
7
|
|
|
8
8
|
type Options = Parameters<typeof computed>[1];
|
|
9
9
|
|
package/src/reactive/object.ts
CHANGED
package/src/resource.ts
CHANGED
|
@@ -42,8 +42,8 @@ class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFu
|
|
|
42
42
|
});
|
|
43
43
|
});
|
|
44
44
|
this.#data = signal(options.value as Awaited<R>, options),
|
|
45
|
-
this.#input = signal(null, options),
|
|
46
|
-
this.#ok = signal(null, options)
|
|
45
|
+
this.#input = signal<A | null>(null, options),
|
|
46
|
+
this.#ok = signal<boolean | null>(null, options)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
|
package/src/signal.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, SIGNAL } from './constants';
|
|
2
|
-
import { Changed, Computed, Effect, Event, Listener, Options, Root, Scheduler, State, Type } from './types';
|
|
2
|
+
import { Changed, Computed, Effect, Event, Listener, PreventPromise, Options, Root, Scheduler, State, Type } from './types';
|
|
3
3
|
import { isArray } from './utilities';
|
|
4
4
|
|
|
5
5
|
|
|
@@ -291,7 +291,7 @@ const effect = <T>(fn: Effect<T>['fn'], options: Omit<Options, 'value'> = {}) =>
|
|
|
291
291
|
if (scope !== null) {
|
|
292
292
|
node.root = scope;
|
|
293
293
|
}
|
|
294
|
-
else if (observer !== null && observer.type === EFFECT
|
|
294
|
+
else if (observer !== null && observer.type === EFFECT) {
|
|
295
295
|
node.root = observer.root;
|
|
296
296
|
}
|
|
297
297
|
else {
|
|
@@ -301,7 +301,7 @@ const effect = <T>(fn: Effect<T>['fn'], options: Omit<Options, 'value'> = {}) =>
|
|
|
301
301
|
node.fn = fn;
|
|
302
302
|
node.task = () => read(node);
|
|
303
303
|
|
|
304
|
-
|
|
304
|
+
read(node);
|
|
305
305
|
|
|
306
306
|
return node as Effect<void>;
|
|
307
307
|
};
|
|
@@ -347,16 +347,16 @@ const reset = <T extends { reset: () => void }>(reset?: T[] | T) => {
|
|
|
347
347
|
return reset;
|
|
348
348
|
};
|
|
349
349
|
|
|
350
|
-
const root = <T>(fn: () => T
|
|
350
|
+
const root = <T>(fn: PreventPromise<T, () => T>, properties: { scheduler?: Scheduler } = {}) => {
|
|
351
351
|
let o = observer,
|
|
352
352
|
s = scope;
|
|
353
353
|
|
|
354
354
|
if (properties.scheduler === undefined) {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
if (properties.scheduler === undefined) {
|
|
355
|
+
if (scope === null) {
|
|
358
356
|
throw new Error('Reactivity: `root` cannot be created without a task scheduler');
|
|
359
357
|
}
|
|
358
|
+
|
|
359
|
+
properties.scheduler = scope.scheduler;
|
|
360
360
|
}
|
|
361
361
|
|
|
362
362
|
observer = null;
|
package/src/types.ts
CHANGED
|
@@ -6,12 +6,12 @@ import Signal from './signal';
|
|
|
6
6
|
type Changed = (a: unknown, b: unknown) => boolean;
|
|
7
7
|
|
|
8
8
|
type Computed<T> = {
|
|
9
|
-
fn: T
|
|
9
|
+
fn: PreventPromise<T, (previous: T) => T>;
|
|
10
10
|
value: ReturnType<Computed<T>['fn']>;
|
|
11
11
|
} & Omit<Signal<T>, 'fn' | 'value'>;
|
|
12
12
|
|
|
13
13
|
type Effect<T> = {
|
|
14
|
-
fn: (node: Effect<T>) => void
|
|
14
|
+
fn: PreventPromise<T, (node: Effect<T>) => void>;
|
|
15
15
|
root: NonNullable<Signal<T>['root']>;
|
|
16
16
|
task: NonNullable<Signal<T>['task']>
|
|
17
17
|
value: void;
|
|
@@ -32,6 +32,8 @@ type Options = {
|
|
|
32
32
|
value?: unknown;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
type PreventPromise<T, R> = T extends Promise<unknown> ? never : R;
|
|
36
|
+
|
|
35
37
|
type Root = {
|
|
36
38
|
scheduler: Scheduler
|
|
37
39
|
};
|
|
@@ -43,4 +45,4 @@ type State = typeof CHECK | typeof CLEAN | typeof DIRTY | typeof DISPOSED;
|
|
|
43
45
|
type Type = typeof COMPUTED | typeof EFFECT | typeof SIGNAL;
|
|
44
46
|
|
|
45
47
|
|
|
46
|
-
export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, Root, Scheduler, Signal, State, Type };
|
|
48
|
+
export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, PreventPromise, Root, Scheduler, Signal, State, Type };
|