@esportsplus/reactivity 0.1.5 → 0.1.6

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/macro.d.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import CustomFunction from '@esportsplus/custom-function';
2
- import { computed } from './signal';
3
- import { Computed } from './types';
4
- type Fn<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
5
- type Options = Parameters<typeof computed>[1];
2
+ import { Computed, Options } from './types';
3
+ type Function<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
6
4
  declare class Macro<A extends unknown[], R> extends CustomFunction {
7
5
  #private;
8
- constructor(fn: Fn<A, R>, options?: Options);
6
+ constructor(fn: Function<A, R>, options?: Options);
9
7
  dispose(): void;
10
8
  reset(): void;
11
9
  }
@@ -1,16 +1,15 @@
1
1
  import CustomFunction from '@esportsplus/custom-function';
2
- import { computed } from './signal';
3
- type Fn<A extends unknown[], R extends Promise<unknown>> = (...args: A) => R;
4
- type Options = Parameters<typeof computed>[1];
2
+ import { Options } from './types';
3
+ type Function<A extends unknown[], R extends Promise<unknown>> = (...args: A) => R;
5
4
  declare class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
6
5
  #private;
7
6
  stop: boolean | null;
8
- constructor(fn: Fn<A, R>, options?: Options);
9
- get data(): Awaited<R> | ReturnType<import("./types").PreventPromise<Awaited<R>, (previous: Awaited<R>) => Awaited<R>>>;
7
+ constructor(fn: Function<A, R>, options?: Options);
8
+ get data(): Awaited<R>;
9
+ get input(): A | null;
10
10
  get ok(): boolean | null;
11
- get input(): A | ReturnType<import("./types").PreventPromise<A, (previous: A | null) => A | null>> | null;
12
11
  dispose(): void;
13
12
  reset(): void;
14
13
  }
15
- declare const _default: <A extends unknown[], R extends Promise<unknown>>(fn: Fn<A, R>, options?: Options) => Resource<A, R>;
14
+ declare const _default: <A extends unknown[], R extends Promise<unknown>>(fn: Function<A, R>, options?: Options) => Resource<A, R>;
16
15
  export default _default;
package/build/resource.js CHANGED
@@ -33,12 +33,12 @@ class Resource extends CustomFunction {
33
33
  get data() {
34
34
  return read(this.#data);
35
35
  }
36
- get ok() {
37
- return read(this.#ok);
38
- }
39
36
  get input() {
40
37
  return read(this.#input);
41
38
  }
39
+ get ok() {
40
+ return read(this.#ok);
41
+ }
42
42
  dispose() {
43
43
  this.#data.dispose();
44
44
  this.#input.dispose();
package/build/signal.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Changed, Computed, Effect, Event, Listener, PreventPromise, Options, Root, Scheduler, State, Type } from './types';
1
+ import { Changed, Computed, Effect, Event, Listener, Options, Root, Scheduler, State, SyncFunction, Type } from './types';
2
2
  declare class Signal<T> {
3
3
  changed: Changed | null;
4
- fn: Computed<T>['fn'] | null;
4
+ fn: Computed<T>['fn'] | Effect['fn'] | null;
5
5
  listeners: Record<Event, (Listener<any> | null)[]> | null;
6
6
  observers: Signal<T>[] | null;
7
7
  root: Root | null;
@@ -10,7 +10,7 @@ declare class Signal<T> {
10
10
  task: Parameters<Scheduler>[0] | null;
11
11
  type: Type;
12
12
  updating: boolean | null;
13
- value: Computed<T>['value'] | T;
13
+ value: T;
14
14
  constructor(data: T, state: Signal<T>['state'], type: Signal<T>['type'], options?: Options);
15
15
  dispatch<D>(event: Event, data?: D): void;
16
16
  dispose(): void;
@@ -18,19 +18,17 @@ 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: PreventPromise<T, (previous: T) => T>, options?: Options) => Computed<T>;
21
+ declare const computed: <T>(fn: ((previous: T) => T) extends infer T_1 ? T_1 extends (previous: T) => T ? T_1 extends (...args: unknown[]) => unknown ? SyncFunction<((previous: T) => T) extends infer T_2 ? T_2 extends (previous: T) => T ? T_2 extends (...args: unknown[]) => unknown ? ReturnType<T_2> : T_2 : never : never, (((previous: T) => T) extends infer T_2 ? T_2 extends (previous: T) => T ? T_2 extends (...args: unknown[]) => unknown ? ReturnType<T_2> : T_2 : never : never) extends infer T_3 ? T_3 extends (((previous: T) => T) extends infer T_2 ? T_2 extends (previous: T) => T ? T_2 extends (...args: unknown[]) => unknown ? ReturnType<T_2> : T_2 : never : never) ? T_3 extends (...args: unknown[]) => unknown ? ReturnType<T_3> : T_3 : never : never> extends never ? never : T_1 : T_1 : never : never, 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: 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>>;
25
+ declare const effect: (fn: Effect['fn'], options?: Omit<Options, 'value'>) => Effect;
26
+ declare const read: <T>(node: Signal<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: PreventPromise<T, () => T>, properties?: {
31
- scheduler?: Scheduler;
32
- }) => T;
30
+ declare const root: <T>(fn: SyncFunction<T, T extends (...args: unknown[]) => unknown ? ReturnType<T> : T> extends never ? never : () => T, properties?: Root) => T;
33
31
  declare const signal: <T>(data: T, options?: Omit<Options, 'value'>) => Signal<T>;
34
- declare const write: <T>(node: Signal<T>, value: unknown) => T | ReturnType<PreventPromise<T, (previous: T) => T>>;
32
+ declare const write: <T>(node: Signal<T>, value: unknown) => T;
35
33
  export default Signal;
36
34
  export { computed, dispose, effect, read, reset, root, signal, write };
package/build/signal.js CHANGED
@@ -216,11 +216,11 @@ const dispose = (dispose) => {
216
216
  return dispose;
217
217
  };
218
218
  const effect = (fn, options = {}) => {
219
- let node = new Signal(undefined, DIRTY, EFFECT, options);
219
+ let node = new Signal(void 0, DIRTY, EFFECT, 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 && observer.root !== null) {
224
224
  node.root = observer.root;
225
225
  }
226
226
  else {
@@ -266,13 +266,13 @@ const reset = (reset) => {
266
266
  }
267
267
  return reset;
268
268
  };
269
- const root = (fn, properties = {}) => {
269
+ const root = (fn, properties) => {
270
270
  let o = observer, s = scope;
271
- if (properties.scheduler === undefined) {
271
+ if (properties === undefined) {
272
272
  if (scope === null) {
273
273
  throw new Error('Reactivity: `root` cannot be created without a task scheduler');
274
274
  }
275
- properties.scheduler = scope.scheduler;
275
+ properties = scope;
276
276
  }
277
277
  observer = null;
278
278
  scope = properties;
package/build/types.d.ts CHANGED
@@ -1,17 +1,16 @@
1
- import { Prettify } from '@esportsplus/typescript';
1
+ import { Function, Prettify, SyncFunction } from '@esportsplus/typescript';
2
2
  import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, SIGNAL } from './constants';
3
3
  import Signal from './signal';
4
4
  type Changed = (a: unknown, b: unknown) => boolean;
5
5
  type Computed<T> = {
6
- fn: PreventPromise<T, (previous: T) => T>;
7
- value: ReturnType<Computed<T>['fn']>;
8
- } & Omit<Signal<T>, 'fn' | 'value'>;
9
- type Effect<T> = {
10
- fn: PreventPromise<T, (node: Effect<T>) => void>;
11
- root: NonNullable<Signal<T>['root']>;
12
- task: NonNullable<Signal<T>['task']>;
6
+ fn: SyncFunction<(previous: T) => T>;
7
+ } & Omit<Signal<T>, 'fn'>;
8
+ type Effect = {
9
+ fn: SyncFunction<(node: Effect) => void>;
10
+ root: Root;
11
+ task: Function;
13
12
  value: void;
14
- } & Omit<Signal<T>, 'fn' | 'root' | 'task' | 'value'>;
13
+ } & Omit<Signal<void>, 'fn' | 'root' | 'task' | 'value'>;
15
14
  type Event = string;
16
15
  type Listener<D> = {
17
16
  once?: boolean;
@@ -25,11 +24,10 @@ type Options = {
25
24
  changed?: Changed;
26
25
  value?: unknown;
27
26
  };
28
- type PreventPromise<T, R> = T extends Promise<unknown> ? never : R;
29
27
  type Root = {
30
28
  scheduler: Scheduler;
31
29
  };
32
- type Scheduler = (fn: (...args: unknown[]) => Promise<unknown> | unknown) => unknown;
30
+ type Scheduler = (fn: Function) => unknown;
33
31
  type State = typeof CHECK | typeof CLEAN | typeof DIRTY | typeof DISPOSED;
34
32
  type Type = typeof COMPUTED | typeof EFFECT | typeof SIGNAL;
35
- export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, PreventPromise, Root, Scheduler, Signal, State, Type };
33
+ export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, Root, Scheduler, Signal, State, SyncFunction, Type };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "@esportsplus/custom-function": "^0.0.1"
5
5
  },
6
6
  "devDependencies": {
7
- "@esportsplus/typescript": "^0.0.3"
7
+ "@esportsplus/typescript": "^0.0.6"
8
8
  },
9
9
  "main": "build/index.js",
10
10
  "name": "@esportsplus/reactivity",
@@ -16,5 +16,5 @@
16
16
  "prepublishOnly": "npm run build"
17
17
  },
18
18
  "types": "build/index.d.ts",
19
- "version": "0.1.5"
19
+ "version": "0.1.6"
20
20
  }
package/src/macro.ts CHANGED
@@ -1,18 +1,16 @@
1
1
  import CustomFunction from '@esportsplus/custom-function';
2
2
  import { computed, read } from './signal';
3
- import { Computed } from './types';
3
+ import { Computed, Options } from './types';
4
4
 
5
5
 
6
- type Fn<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
7
-
8
- type Options = Parameters<typeof computed>[1];
6
+ type Function<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
9
7
 
10
8
 
11
9
  class Macro<A extends unknown[], R> extends CustomFunction {
12
- #factory: Computed< ReturnType<Fn<A,R>> >;
10
+ #factory: Computed< ReturnType<Function<A,R>> >;
13
11
 
14
12
 
15
- constructor(fn: Fn<A,R>, options: Options = {}) {
13
+ constructor(fn: Function<A,R>, options: Options = {}) {
16
14
  super((...args: A) => {
17
15
  return read(this.#factory)(...args);
18
16
  });
@@ -30,6 +28,6 @@ class Macro<A extends unknown[], R> extends CustomFunction {
30
28
  }
31
29
 
32
30
 
33
- export default <A extends unknown[], R>(fn: Fn<A,R>, options: Options = {}) => {
31
+ export default <A extends unknown[], R>(fn: Function<A,R>, options: Options = {}) => {
34
32
  return new Macro(fn, options);
35
33
  };
package/src/resource.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import CustomFunction from '@esportsplus/custom-function';
2
- import { computed, read, signal, write } from './signal';
3
- import { Signal } from './types';
2
+ import { read, signal, write } from './signal';
3
+ import { Options, Signal } from './types';
4
4
 
5
5
 
6
- type Fn<A extends unknown[], R extends Promise<unknown>> = (...args: A) => R;
7
-
8
- type Options = Parameters<typeof computed>[1];
6
+ type Function<A extends unknown[], R extends Promise<unknown>> = (...args: A) => R;
9
7
 
10
8
 
11
9
  class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
@@ -16,7 +14,7 @@ class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFu
16
14
  stop: boolean | null = null;
17
15
 
18
16
 
19
- constructor(fn: Fn<A,R>, options: Options = {}) {
17
+ constructor(fn: Function<A,R>, options: Options = {}) {
20
18
  super((...args: A) => {
21
19
  this.stop = null;
22
20
 
@@ -51,14 +49,14 @@ class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFu
51
49
  return read(this.#data);
52
50
  }
53
51
 
54
- get ok() {
55
- return read(this.#ok);
56
- }
57
-
58
52
  get input() {
59
53
  return read(this.#input);
60
54
  }
61
55
 
56
+ get ok() {
57
+ return read(this.#ok);
58
+ }
59
+
62
60
 
63
61
  dispose() {
64
62
  this.#data.dispose();
@@ -74,6 +72,6 @@ class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFu
74
72
  }
75
73
 
76
74
 
77
- export default <A extends unknown[], R extends Promise<unknown>>(fn: Fn<A,R>, options: Options = {}) => {
75
+ export default <A extends unknown[], R extends Promise<unknown>>(fn: Function<A,R>, options: Options = {}) => {
78
76
  return new Resource(fn, options);
79
77
  };
package/src/signal.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, SIGNAL } from './constants';
2
- import { Changed, Computed, Effect, Event, Listener, PreventPromise, Options, Root, Scheduler, State, Type } from './types';
2
+ import { Changed, Computed, Effect, Event, Listener, Options, Root, Scheduler, State, SyncFunction, Type } from './types';
3
3
  import { isArray } from './utilities';
4
4
 
5
5
 
6
-
7
6
  let index = 0,
8
7
  observer: Signal<any> | null = null,
9
8
  observers: Signal<any>[] | null = null,
@@ -12,7 +11,7 @@ let index = 0,
12
11
 
13
12
  class Signal<T> {
14
13
  changed: Changed | null = null;
15
- fn: Computed<T>['fn'] | null = null;
14
+ fn: Computed<T>['fn'] | Effect['fn'] | null = null;
16
15
  listeners: Record<Event, (Listener<any> | null)[]> | null = null;
17
16
  observers: Signal<T>[] | null = null;
18
17
  root: Root | null = null;
@@ -21,7 +20,7 @@ class Signal<T> {
21
20
  task: Parameters<Scheduler>[0] | null = null;
22
21
  type: Type;
23
22
  updating: boolean | null = null;
24
- value: Computed<T>['value'] | T;
23
+ value: T;
25
24
 
26
25
 
27
26
  constructor(data: T, state: Signal<T>['state'], type: Signal<T>['type'], options: Options = {}) {
@@ -209,7 +208,9 @@ function update<T>(node: Signal<T>) {
209
208
  node.dispatch('update');
210
209
  node.updating = true;
211
210
 
212
- let value = node.fn!.call(node, node.value);
211
+ let value = (
212
+ node as typeof node extends Effect ? Effect : Computed<T>
213
+ ).fn.call(node, node.value);
213
214
 
214
215
  node.updating = null;
215
216
 
@@ -263,11 +264,11 @@ function update<T>(node: Signal<T>) {
263
264
 
264
265
 
265
266
  const computed = <T>(fn: Computed<T>['fn'], options: Options = {}) => {
266
- let node = new Signal(options.value as any, DIRTY, COMPUTED, options);
267
+ let node = new Signal(options.value as T, DIRTY, COMPUTED, options) as Computed<T>;
267
268
 
268
269
  node.fn = fn;
269
270
 
270
- return node as Computed<T>;
271
+ return node;
271
272
  };
272
273
 
273
274
  const dispose = <T extends { dispose: () => void }>(dispose?: T[] | T) => {
@@ -285,13 +286,13 @@ const dispose = <T extends { dispose: () => void }>(dispose?: T[] | T) => {
285
286
  return dispose;
286
287
  };
287
288
 
288
- const effect = <T>(fn: Effect<T>['fn'], options: Omit<Options, 'value'> = {}) => {
289
- let node = new Signal(undefined as any, DIRTY, EFFECT, options);
289
+ const effect = (fn: Effect['fn'], options: Omit<Options, 'value'> = {}) => {
290
+ let node = new Signal(void 0, DIRTY, EFFECT, options) as Effect;
290
291
 
291
292
  if (scope !== null) {
292
293
  node.root = scope;
293
294
  }
294
- else if (observer !== null && observer.type === EFFECT) {
295
+ else if (observer !== null && observer.type === EFFECT && observer.root !== null) {
295
296
  node.root = observer.root;
296
297
  }
297
298
  else {
@@ -303,7 +304,7 @@ const effect = <T>(fn: Effect<T>['fn'], options: Omit<Options, 'value'> = {}) =>
303
304
 
304
305
  read(node);
305
306
 
306
- return node as Effect<void>;
307
+ return node;
307
308
  };
308
309
 
309
310
  const read = <T>(node: Signal<T>): typeof node['value'] => {
@@ -347,20 +348,20 @@ const reset = <T extends { reset: () => void }>(reset?: T[] | T) => {
347
348
  return reset;
348
349
  };
349
350
 
350
- const root = <T>(fn: PreventPromise<T, () => T>, properties: { scheduler?: Scheduler } = {}) => {
351
+ const root = <T>(fn: SyncFunction<() => T>, properties?: Root) => {
351
352
  let o = observer,
352
353
  s = scope;
353
354
 
354
- if (properties.scheduler === undefined) {
355
+ if (properties === undefined) {
355
356
  if (scope === null) {
356
357
  throw new Error('Reactivity: `root` cannot be created without a task scheduler');
357
358
  }
358
359
 
359
- properties.scheduler = scope.scheduler;
360
+ properties = scope;
360
361
  }
361
362
 
362
363
  observer = null;
363
- scope = properties as Root;
364
+ scope = properties;
364
365
 
365
366
  let result = fn();
366
367
 
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Prettify } from '@esportsplus/typescript'
1
+ import { Function, Prettify, SyncFunction } from '@esportsplus/typescript'
2
2
  import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, SIGNAL } from './constants';
3
3
  import Signal from './signal';
4
4
 
@@ -6,16 +6,15 @@ import Signal from './signal';
6
6
  type Changed = (a: unknown, b: unknown) => boolean;
7
7
 
8
8
  type Computed<T> = {
9
- fn: PreventPromise<T, (previous: T) => T>;
10
- value: ReturnType<Computed<T>['fn']>;
11
- } & Omit<Signal<T>, 'fn' | 'value'>;
12
-
13
- type Effect<T> = {
14
- fn: PreventPromise<T, (node: Effect<T>) => void>;
15
- root: NonNullable<Signal<T>['root']>;
16
- task: NonNullable<Signal<T>['task']>
9
+ fn: SyncFunction<(previous: T) => T>;
10
+ } & Omit<Signal<T>, 'fn'>;
11
+
12
+ type Effect = {
13
+ fn: SyncFunction<(node: Effect) => void>;
14
+ root: Root;
15
+ task: Function
17
16
  value: void;
18
- } & Omit<Signal<T>, 'fn' | 'root' | 'task' | 'value'>;
17
+ } & Omit<Signal<void>, 'fn' | 'root' | 'task' | 'value'>;
19
18
 
20
19
  type Event = string;
21
20
 
@@ -32,17 +31,15 @@ type Options = {
32
31
  value?: unknown;
33
32
  };
34
33
 
35
- type PreventPromise<T, R> = T extends Promise<unknown> ? never : R;
36
-
37
34
  type Root = {
38
35
  scheduler: Scheduler
39
36
  };
40
37
 
41
- type Scheduler = (fn: (...args: unknown[]) => Promise<unknown> | unknown) => unknown;
38
+ type Scheduler = (fn: Function) => unknown;
42
39
 
43
40
  type State = typeof CHECK | typeof CLEAN | typeof DIRTY | typeof DISPOSED;
44
41
 
45
42
  type Type = typeof COMPUTED | typeof EFFECT | typeof SIGNAL;
46
43
 
47
44
 
48
- export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, PreventPromise, Root, Scheduler, Signal, State, Type };
45
+ export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, Root, Scheduler, Signal, State, SyncFunction, Type };