@esportsplus/reactivity 0.1.3 → 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/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export { default as resource } from './resource';
3
3
  export * as core from './signal';
4
4
  export { effect, root } from './signal';
5
5
  export { default as reactive } from './reactive';
6
+ export * from './types';
package/build/index.js CHANGED
@@ -3,3 +3,4 @@ export { default as resource } from './resource';
3
3
  export * as core from './signal';
4
4
  export { effect, root } from './signal';
5
5
  export { default as reactive } from './reactive';
6
+ export * from './types';
package/build/macro.d.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import CustomFunction from '@esportsplus/custom-function';
2
- import { computed } from './signal';
3
- type Fn<A extends unknown[], R> = Parameters<typeof computed<(...args: A) => R>>[0];
4
- type Options = Parameters<typeof computed>[1];
2
+ import { Computed, Options } from './types';
3
+ type Function<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
5
4
  declare class Macro<A extends unknown[], R> extends CustomFunction {
6
5
  #private;
7
- constructor(fn: Fn<A, R>, options?: Options);
6
+ constructor(fn: Function<A, R>, options?: Options);
8
7
  dispose(): void;
9
8
  reset(): void;
10
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<Awaited<R> extends infer T ? T extends Awaited<R> ? T extends Promise<unknown> ? never : (previous: T) => T : never : never>;
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<A extends Promise<unknown> ? never : (previous: A) => A> | 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, 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: T extends Promise<unknown> ? never : (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: (node: Effect<T>) => void, options?: Omit<Options, 'value'>) => Effect<void>;
26
- declare const read: <T>(node: Signal<T>) => T | ReturnType<T extends Promise<unknown> ? never : (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: () => 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<T extends Promise<unknown> ? never : (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,7 +216,7 @@ 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
  }
@@ -228,7 +228,7 @@ const effect = (fn, options = {}) => {
228
228
  }
229
229
  node.fn = fn;
230
230
  node.task = () => read(node);
231
- node.root.scheduler(node.task);
231
+ read(node);
232
232
  return node;
233
233
  };
234
234
  const read = (node) => {
@@ -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) {
272
- properties.scheduler = scope?.scheduler;
273
- if (properties.scheduler === undefined) {
271
+ if (properties === undefined) {
272
+ if (scope === null) {
274
273
  throw new Error('Reactivity: `root` cannot be created without a task scheduler');
275
274
  }
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: T extends Promise<unknown> ? never : ((previous: T) => T);
7
- value: ReturnType<Computed<T>['fn']>;
8
- } & Omit<Signal<T>, 'fn' | 'value'>;
9
- type Effect<T> = {
10
- fn: (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;
@@ -28,7 +27,7 @@ type Options = {
28
27
  type Root = {
29
28
  scheduler: Scheduler;
30
29
  };
31
- type Scheduler = (fn: (...args: unknown[]) => Promise<unknown> | unknown) => unknown;
30
+ type Scheduler = (fn: Function) => unknown;
32
31
  type State = typeof CHECK | typeof CLEAN | typeof DIRTY | typeof DISPOSED;
33
32
  type Type = typeof COMPUTED | typeof EFFECT | typeof SIGNAL;
34
- export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, 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.3"
19
+ "version": "0.1.6"
20
20
  }
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
@@ -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> = Parameters<typeof computed<(...args: A) => R>>[0];
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
  };
@@ -48,7 +48,7 @@ class ReactiveObject<T extends Object> {
48
48
  });
49
49
  }
50
50
  else {
51
- let node = nodes[key] = signal(input, options);
51
+ let node = nodes[key] = signal<unknown>(input, options);
52
52
 
53
53
  defineProperty(this, key, {
54
54
  enumerable: true,
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
 
@@ -42,8 +40,8 @@ class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFu
42
40
  });
43
41
  });
44
42
  this.#data = signal(options.value as Awaited<R>, options),
45
- this.#input = signal(null, options),
46
- this.#ok = signal(null, options)
43
+ this.#input = signal<A | null>(null, options),
44
+ this.#ok = signal<boolean | null>(null, options)
47
45
  }
48
46
 
49
47
 
@@ -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, 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,8 +286,8 @@ 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;
@@ -301,9 +302,9 @@ const effect = <T>(fn: Effect<T>['fn'], options: Omit<Options, 'value'> = {}) =>
301
302
  node.fn = fn;
302
303
  node.task = () => read(node);
303
304
 
304
- node.root.scheduler(node.task);
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: () => 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
- properties.scheduler = scope?.scheduler;
356
-
357
- if (properties.scheduler === undefined) {
355
+ if (properties === undefined) {
356
+ if (scope === null) {
358
357
  throw new Error('Reactivity: `root` cannot be created without a task scheduler');
359
358
  }
359
+
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: T extends Promise<unknown> ? never : ((previous: T) => T);
10
- value: ReturnType<Computed<T>['fn']>;
11
- } & Omit<Signal<T>, 'fn' | 'value'>;
12
-
13
- type Effect<T> = {
14
- fn: (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
 
@@ -36,11 +35,11 @@ type Root = {
36
35
  scheduler: Scheduler
37
36
  };
38
37
 
39
- type Scheduler = (fn: (...args: unknown[]) => Promise<unknown> | unknown) => unknown;
38
+ type Scheduler = (fn: Function) => unknown;
40
39
 
41
40
  type State = typeof CHECK | typeof CLEAN | typeof DIRTY | typeof DISPOSED;
42
41
 
43
42
  type Type = typeof COMPUTED | typeof EFFECT | typeof SIGNAL;
44
43
 
45
44
 
46
- export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, Root, Scheduler, Signal, State, Type };
45
+ export { Changed, Computed, Effect, Event, Listener, Object, Options, Prettify, Root, Scheduler, Signal, State, SyncFunction, Type };