@esportsplus/reactivity 0.1.8 → 0.1.10

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.
@@ -4,5 +4,6 @@ declare const DIRTY = 2;
4
4
  declare const DISPOSED = 3;
5
5
  declare const COMPUTED = 0;
6
6
  declare const EFFECT = 1;
7
- declare const SIGNAL = 2;
8
- export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, SIGNAL };
7
+ declare const ROOT = 2;
8
+ declare const SIGNAL = 3;
9
+ export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL };
@@ -4,5 +4,6 @@ const DIRTY = 2;
4
4
  const DISPOSED = 3;
5
5
  const COMPUTED = 0;
6
6
  const EFFECT = 1;
7
- const SIGNAL = 2;
8
- export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, SIGNAL };
7
+ const ROOT = 2;
8
+ const SIGNAL = 3;
9
+ export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL };
@@ -0,0 +1,4 @@
1
+ declare const _default: <T extends {
2
+ dispose: VoidFunction;
3
+ }>(dispose?: T | T[] | null | undefined) => T | T[] | null | undefined;
4
+ export default _default;
@@ -0,0 +1,14 @@
1
+ import { isArray } from './utilities';
2
+ export default (dispose) => {
3
+ if (dispose == null) {
4
+ }
5
+ else if (isArray(dispose)) {
6
+ for (let i = 0, n = dispose.length; i < n; i++) {
7
+ dispose[i].dispose();
8
+ }
9
+ }
10
+ else {
11
+ dispose.dispose();
12
+ }
13
+ return dispose;
14
+ };
package/build/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export { default as macro } from './macro';
2
2
  export { default as resource } from './resource';
3
- export * as core from './signal';
4
- export { effect, root } from './signal';
5
3
  export { default as reactive } from './reactive';
4
+ export { computed, dispose, effect, reset, root, signal } from './signal';
6
5
  export * from './types';
package/build/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  export { default as macro } from './macro';
2
2
  export { default as resource } from './resource';
3
- export * as core from './signal';
4
- export { effect, root } from './signal';
5
3
  export { default as reactive } from './reactive';
4
+ export { computed, dispose, effect, reset, root, signal } from './signal';
6
5
  export * from './types';
package/build/macro.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import CustomFunction from '@esportsplus/custom-function';
2
- import { Computed, Options } from './types';
2
+ import { Computed } from './signal';
3
+ import { Options } from './types';
3
4
  type Function<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
4
5
  declare class Macro<A extends unknown[], R> extends CustomFunction {
5
6
  #private;
@@ -7,5 +8,5 @@ declare class Macro<A extends unknown[], R> extends CustomFunction {
7
8
  dispose(): void;
8
9
  reset(): void;
9
10
  }
10
- declare const _default: <A extends unknown[], R>(fn: (previous: (...args: A) => R) => (...args: A) => R, options?: Options) => Macro<A, R>;
11
+ declare const _default: <A extends unknown[], R>(fn: () => (...args: A) => R, options?: Options) => Macro<A, R>;
11
12
  export default _default;
package/build/macro.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import CustomFunction from '@esportsplus/custom-function';
2
- import { computed, read } from './signal';
2
+ import { Computed } from './signal';
3
3
  class Macro extends CustomFunction {
4
4
  #factory;
5
5
  constructor(fn, options = {}) {
6
6
  super((...args) => {
7
- return read(this.#factory)(...args);
7
+ return this.#factory.get()(...args);
8
8
  });
9
- this.#factory = computed(fn, options);
9
+ this.#factory = new Computed(fn, options);
10
10
  }
11
11
  dispose() {
12
12
  this.#factory.dispose();
@@ -54,7 +54,7 @@ declare class ReactiveObjectArray<T extends Object> extends ReactiveArray<Node<T
54
54
  push(...values: T[]): number;
55
55
  shift(): Node<T> | undefined;
56
56
  sort(): never;
57
- splice(start: number, deleteCount?: number, ...values: T[]): Node<T> | Node<T>[] | undefined;
57
+ splice(start: number, deleteCount?: number, ...values: T[]): Node<T> | Node<T>[] | null | undefined;
58
58
  unshift(...values: T[]): number;
59
59
  }
60
60
  export { ReactiveArray, ReactiveObjectArray };
@@ -1,4 +1,5 @@
1
- import { dispose, read, signal, write } from '../signal';
1
+ import { dispose } from '../signal';
2
+ import { Signal } from '../types';
2
3
  import { ReactiveObject } from './object';
3
4
  function factory(data, options = {}) {
4
5
  let signals = [];
@@ -14,7 +15,7 @@ class ReactiveArray extends Array {
14
15
  #signal;
15
16
  constructor(data) {
16
17
  super(...data);
17
- this.#signal = signal(false);
18
+ this.#signal = new Signal(false);
18
19
  }
19
20
  set length(n) {
20
21
  if (n > this.length) {
@@ -23,7 +24,7 @@ class ReactiveArray extends Array {
23
24
  this.splice(n);
24
25
  }
25
26
  trigger() {
26
- write(this.#signal, !this.#signal.value);
27
+ this.#signal.set(!this.#signal.value);
27
28
  }
28
29
  dispatch(event, data) {
29
30
  this.#signal.dispatch(event, data);
@@ -93,7 +94,7 @@ class ReactiveArray extends Array {
93
94
  return removed;
94
95
  }
95
96
  track() {
96
- read(this.#signal);
97
+ this.#signal.get();
97
98
  }
98
99
  unshift(...items) {
99
100
  let length = super.unshift(...items);
@@ -1,4 +1,4 @@
1
1
  import { ReactiveObject } from './object';
2
- export default (data, options = {}) => {
2
+ export default (data, options) => {
3
3
  return new ReactiveObject(data, options);
4
4
  };
@@ -1,4 +1,4 @@
1
- import { computed, read, signal, write } from '../signal';
1
+ import { Computed, Signal } from '../types';
2
2
  import { defineProperty, isArray } from '../utilities';
3
3
  import { ReactiveArray, ReactiveObjectArray } from './array';
4
4
  class ReactiveObject {
@@ -8,11 +8,11 @@ class ReactiveObject {
8
8
  for (let key in data) {
9
9
  let input = data[key];
10
10
  if (typeof input === 'function') {
11
- let node = nodes[key] = computed(input, options);
11
+ let node = nodes[key] = new Computed(input, options);
12
12
  defineProperty(this, key, {
13
13
  enumerable: true,
14
14
  get() {
15
- return read(node);
15
+ return node.get();
16
16
  }
17
17
  });
18
18
  }
@@ -33,14 +33,14 @@ class ReactiveObject {
33
33
  });
34
34
  }
35
35
  else {
36
- let node = nodes[key] = signal(input, options);
36
+ let node = nodes[key] = new Signal(input, options);
37
37
  defineProperty(this, key, {
38
38
  enumerable: true,
39
39
  get() {
40
- return read(node);
40
+ return node.get();
41
41
  },
42
42
  set(value) {
43
- write(node, value);
43
+ node.set(value);
44
44
  }
45
45
  });
46
46
  }
@@ -0,0 +1,4 @@
1
+ declare const _default: <T extends {
2
+ reset: VoidFunction;
3
+ }>(reset?: T | T[] | null | undefined) => T | T[] | null | undefined;
4
+ export default _default;
package/build/reset.js ADDED
@@ -0,0 +1,14 @@
1
+ import { isArray } from './utilities';
2
+ export default (reset) => {
3
+ if (reset == null) {
4
+ }
5
+ else if (isArray(reset)) {
6
+ for (let i = 0, n = reset.length; i < n; i++) {
7
+ reset[i].reset();
8
+ }
9
+ }
10
+ else {
11
+ reset.reset();
12
+ }
13
+ return reset;
14
+ };
package/build/resource.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import CustomFunction from '@esportsplus/custom-function';
2
- import { read, signal, write } from './signal';
2
+ import { Signal } from './signal';
3
3
  class Resource extends CustomFunction {
4
4
  #data;
5
5
  #input;
@@ -8,36 +8,36 @@ class Resource extends CustomFunction {
8
8
  constructor(fn, options = {}) {
9
9
  super((...args) => {
10
10
  this.stop = null;
11
- write(this.#input, args);
12
- write(this.#ok, null);
11
+ this.#input.set(args);
12
+ this.#ok.set(null);
13
13
  fn(...args)
14
14
  .then((value) => {
15
15
  if (this.stop === true) {
16
16
  return;
17
17
  }
18
- write(this.#data, value);
19
- write(this.#ok, true);
18
+ this.#data.set(value);
19
+ this.#ok.set(true);
20
20
  })
21
21
  .catch(() => {
22
22
  if (this.stop === true) {
23
23
  return;
24
24
  }
25
- write(this.#data, undefined);
26
- write(this.#ok, false);
25
+ this.#data.set(undefined);
26
+ this.#ok.set(false);
27
27
  });
28
28
  });
29
- this.#data = signal(options.value, options),
30
- this.#input = signal(null, options),
31
- this.#ok = signal(null, options);
29
+ this.#data = new Signal(undefined, options);
30
+ this.#input = new Signal(null, options);
31
+ this.#ok = new Signal(null, options);
32
32
  }
33
33
  get data() {
34
- return read(this.#data);
34
+ return this.#data.get();
35
35
  }
36
36
  get input() {
37
- return read(this.#input);
37
+ return this.#input.get();
38
38
  }
39
39
  get ok() {
40
- return read(this.#ok);
40
+ return this.#ok.get();
41
41
  }
42
42
  dispose() {
43
43
  this.#data.dispose();
package/build/signal.d.ts CHANGED
@@ -1,34 +1,56 @@
1
- import { Changed, Computed, Effect, Event, Listener, NeverAsync, Options, Root, Scheduler, State, Type } from './types';
2
- declare class Signal<T> {
3
- changed: Changed | null;
4
- fn: Computed<T>['fn'] | Effect['fn'] | null;
1
+ import { Changed, Event, Function, Listener, NeverAsync, Options, State, Type } from './types';
2
+ declare class Core<T> {
5
3
  listeners: Record<Event, (Listener<any> | null)[]> | null;
6
- observers: Signal<T>[] | null;
4
+ observers: Core<any>[] | null;
7
5
  root: Root | null;
8
- sources: Signal<T>[] | null;
6
+ sources: Core<any>[] | null;
9
7
  state: State;
10
- task: Parameters<Scheduler>[0] | null;
11
- type: Type;
12
- updating: boolean | null;
8
+ updating: boolean;
13
9
  value: T;
14
- constructor(data: T, state: Signal<T>['state'], type: Signal<T>['type'], options?: Options);
10
+ constructor(state: State, value: T);
11
+ get type(): Type | never;
15
12
  dispatch<D>(event: Event, data?: D): void;
16
13
  dispose(): void;
17
- on(event: Event, listener: Listener<any>): void;
18
- once(event: Event, listener: Listener<any>): void;
14
+ on<T>(event: Event, listener: Listener<T>): void;
15
+ once<T>(event: Event, listener: Listener<T>): void;
16
+ }
17
+ declare class Computed<T> extends Core<T> {
18
+ changed: Changed;
19
+ fn: NeverAsync<() => T>;
20
+ constructor(fn: Computed<T>['fn'], options?: Options);
21
+ get type(): Type;
22
+ get(): T;
23
+ reset(): void;
24
+ }
25
+ declare class Effect extends Core<null> {
26
+ fn: NeverAsync<(node: Effect) => void>;
27
+ task: Function;
28
+ constructor(fn: Effect['fn']);
29
+ get type(): Type;
30
+ reset(): void;
31
+ }
32
+ declare class Root extends Core<null> {
33
+ scheduler: (fn: Function) => unknown;
34
+ tracking: boolean;
35
+ constructor(scheduler: Root['scheduler'], tracking: boolean);
36
+ get type(): Type;
37
+ }
38
+ declare class Signal<T> extends Core<T> {
39
+ changed: Changed;
40
+ constructor(data: T, options?: Options);
41
+ get type(): Type;
42
+ get(): T;
19
43
  reset(): void;
44
+ set(value: T): T;
20
45
  }
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 ? (...args: Parameters<T_1>) => ReturnType<T_1> extends NeverAsync<T_1> ? NeverAsync<T_1> & ReturnType<T_1> : NeverAsync<ReturnType<T_1>> : T_1 : never : never, options?: Options) => Computed<T>;
46
+ declare const computed: <T>(fn: () => T extends unknown ? T : NeverAsync<T>, options?: Options) => Computed<T>;
22
47
  declare const dispose: <T extends {
23
- dispose: () => void;
24
- }>(dispose?: T | T[] | undefined) => T | T[] | undefined;
25
- declare const effect: (fn: Effect['fn'], options?: Omit<Options, 'value'>) => Effect;
26
- declare const read: <T>(node: Signal<T>) => T;
48
+ dispose: VoidFunction;
49
+ }>(dispose?: T | T[] | null | undefined) => T | T[] | null | undefined;
50
+ declare const effect: (fn: Effect['fn']) => Effect;
27
51
  declare const reset: <T extends {
28
- reset: () => void;
29
- }>(reset?: T | T[] | undefined) => T | T[] | undefined;
30
- declare function root<T>(fn: NeverAsync<() => T>, properties?: Root): T extends () => T extends any ? T : NeverAsync<T> ? T : NeverAsync<T>;
31
- declare const signal: <T>(data: T, options?: Omit<Options, 'value'>) => Signal<T>;
32
- declare const write: <T>(node: Signal<T>, value: unknown) => T;
33
- export default Signal;
34
- export { computed, dispose, effect, read, reset, root, signal, write };
52
+ reset: VoidFunction;
53
+ }>(reset?: T | T[] | null | undefined) => T | T[] | null | undefined;
54
+ declare const root: <T>(fn: (root: Root) => T, scheduler?: Root['scheduler']) => T;
55
+ declare const signal: <T>(value: T, options?: Options) => Signal<T>;
56
+ export { computed, dispose, effect, reset, root, signal, Computed, Effect, Signal };