@esportsplus/reactivity 0.1.1 → 0.1.3

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.
Files changed (46) hide show
  1. package/build/reactive/object.d.ts +2 -1
  2. package/build/reactive/object.js +4 -1
  3. package/build/signal.js +11 -6
  4. package/package.json +1 -1
  5. package/src/reactive/object.ts +8 -2
  6. package/src/signal.ts +12 -7
  7. package/build/api/effect.d.ts +0 -7
  8. package/build/api/effect.js +0 -3
  9. package/build/api/index.d.ts +0 -4
  10. package/build/api/index.js +0 -4
  11. package/build/api/macro.d.ts +0 -9
  12. package/build/api/macro.js +0 -8
  13. package/build/api/promise.d.ts +0 -8
  14. package/build/api/promise.js +0 -38
  15. package/build/api/store.d.ts +0 -11
  16. package/build/api/store.js +0 -27
  17. package/build/bench.d.ts +0 -1
  18. package/build/bench.js +0 -46
  19. package/build/context/index.d.ts +0 -5
  20. package/build/context/index.js +0 -3
  21. package/build/context/node.d.ts +0 -7
  22. package/build/context/node.js +0 -21
  23. package/build/context/nodes.d.ts +0 -7
  24. package/build/context/nodes.js +0 -33
  25. package/build/effect.d.ts +0 -3
  26. package/build/effect.js +0 -2
  27. package/build/promise.d.ts +0 -28
  28. package/build/promise.js +0 -45
  29. package/build/reactive/object2.d.ts +0 -10
  30. package/build/reactive/object2.js +0 -65
  31. package/build/reactive/types.d.ts +0 -33
  32. package/build/reactive/types.js +0 -1
  33. package/build/reactive-array.d.ts +0 -1
  34. package/build/reactive-array.js +0 -106
  35. package/build/reactive.d.ts +0 -11
  36. package/build/reactive.js +0 -27
  37. package/build/symbols.d.ts +0 -13
  38. package/build/symbols.js +0 -13
  39. package/build/testing/node.d.ts +0 -13
  40. package/build/testing/node.js +0 -21
  41. package/build/testing/nodes.d.ts +0 -13
  42. package/build/testing/nodes.js +0 -33
  43. package/build/testing/reactive.d.ts +0 -0
  44. package/build/testing/reactive.js +0 -1
  45. package/build/trigger.d.ts +0 -15
  46. package/build/trigger.js +0 -30
@@ -1,7 +1,8 @@
1
1
  import { Computed, Object, Options, Signal } from '../types';
2
2
  import { ReactiveArray, ReactiveObjectArray } from './array';
3
+ type Node = Computed<unknown> | ReactiveArray<unknown> | ReactiveObjectArray<Object> | Signal<unknown>;
3
4
  declare class ReactiveObject<T extends Object> {
4
- nodes: Record<PropertyKey, Computed<unknown> | ReactiveObjectArray<Object> | ReactiveArray<unknown> | Signal<unknown>>;
5
+ nodes: Record<PropertyKey, Node>;
5
6
  constructor(data: T, options?: Options);
6
7
  dispose(): void;
7
8
  reset(): void;
@@ -10,6 +10,7 @@ class ReactiveObject {
10
10
  if (typeof input === 'function') {
11
11
  let node = nodes[key] = computed(input, options);
12
12
  defineProperty(this, key, {
13
+ enumerable: true,
13
14
  get() {
14
15
  return read(node);
15
16
  }
@@ -17,13 +18,14 @@ class ReactiveObject {
17
18
  }
18
19
  else if (isArray(input)) {
19
20
  let node, test = input[0];
20
- if (typeof test === 'object' && test !== null && test.constructor.name === 'Object') {
21
+ if (typeof test === 'object' && test !== null && test?.constructor?.name === 'Object') {
21
22
  node = nodes[key] = new ReactiveObjectArray(input, options);
22
23
  }
23
24
  else {
24
25
  node = nodes[key] = new ReactiveArray(input);
25
26
  }
26
27
  defineProperty(this, key, {
28
+ enumerable: true,
27
29
  get() {
28
30
  node.track();
29
31
  return node;
@@ -33,6 +35,7 @@ class ReactiveObject {
33
35
  else {
34
36
  let node = nodes[key] = signal(input, options);
35
37
  defineProperty(this, key, {
38
+ enumerable: true,
36
39
  get() {
37
40
  return read(node);
38
41
  },
package/build/signal.js CHANGED
@@ -216,12 +216,17 @@ const dispose = (dispose) => {
216
216
  return dispose;
217
217
  };
218
218
  const effect = (fn, options = {}) => {
219
- if (!scope) {
219
+ let node = new Signal(undefined, DIRTY, EFFECT, options);
220
+ if (scope !== null) {
221
+ node.root = scope;
222
+ }
223
+ else if (observer !== null && observer.type === EFFECT && observer.root !== null) {
224
+ node.root = observer.root;
225
+ }
226
+ else {
220
227
  throw new Error('Reactivity: `effects` cannot be created without a reactive root');
221
228
  }
222
- let node = new Signal(undefined, DIRTY, EFFECT, options);
223
229
  node.fn = fn;
224
- node.root = scope;
225
230
  node.task = () => read(node);
226
231
  node.root.scheduler(node.task);
227
232
  return node;
@@ -230,8 +235,8 @@ const read = (node) => {
230
235
  if (node.state === DISPOSED) {
231
236
  return node.value;
232
237
  }
233
- if (observer) {
234
- if (!observers) {
238
+ if (observer !== null) {
239
+ if (observers === null) {
235
240
  if (observer.sources !== null && observer.sources[index] == node) {
236
241
  index++;
237
242
  }
@@ -243,7 +248,7 @@ const read = (node) => {
243
248
  observers.push(node);
244
249
  }
245
250
  }
246
- if (node.fn) {
251
+ if (node.fn !== null) {
247
252
  sync(node);
248
253
  }
249
254
  return node.value;
package/package.json CHANGED
@@ -16,5 +16,5 @@
16
16
  "prepublishOnly": "npm run build"
17
17
  },
18
18
  "types": "build/index.d.ts",
19
- "version": "0.1.1"
19
+ "version": "0.1.3"
20
20
  }
@@ -4,8 +4,11 @@ import { defineProperty, isArray } from '~/utilities';
4
4
  import { ReactiveArray, ReactiveObjectArray } from './array';
5
5
 
6
6
 
7
+ type Node = Computed<unknown> | ReactiveArray<unknown> | ReactiveObjectArray<Object> | Signal<unknown>;
8
+
9
+
7
10
  class ReactiveObject<T extends Object> {
8
- nodes: Record<PropertyKey, Computed<unknown> | ReactiveObjectArray<Object> | ReactiveArray<unknown> | Signal<unknown>> = {};
11
+ nodes: Record<PropertyKey, Node> = {};
9
12
 
10
13
 
11
14
  constructor(data: T, options: Options = {}) {
@@ -18,6 +21,7 @@ class ReactiveObject<T extends Object> {
18
21
  let node = nodes[key] = computed(input as Computed<unknown>['fn'], options);
19
22
 
20
23
  defineProperty(this, key, {
24
+ enumerable: true,
21
25
  get() {
22
26
  return read(node);
23
27
  }
@@ -27,7 +31,7 @@ class ReactiveObject<T extends Object> {
27
31
  let node: ReactiveArray<unknown> | ReactiveObjectArray<Object>,
28
32
  test = input[0];
29
33
 
30
- if (typeof test === 'object' && test !== null && test.constructor.name === 'Object') {
34
+ if (typeof test === 'object' && test !== null && test?.constructor?.name === 'Object') {
31
35
  node = nodes[key] = new ReactiveObjectArray(input, options);
32
36
  }
33
37
  else {
@@ -35,6 +39,7 @@ class ReactiveObject<T extends Object> {
35
39
  }
36
40
 
37
41
  defineProperty(this, key, {
42
+ enumerable: true,
38
43
  get() {
39
44
  node.track();
40
45
 
@@ -46,6 +51,7 @@ class ReactiveObject<T extends Object> {
46
51
  let node = nodes[key] = signal(input, options);
47
52
 
48
53
  defineProperty(this, key, {
54
+ enumerable: true,
49
55
  get() {
50
56
  return read(node);
51
57
  },
package/src/signal.ts CHANGED
@@ -286,14 +286,19 @@ const dispose = <T extends { dispose: () => void }>(dispose?: T[] | T) => {
286
286
  };
287
287
 
288
288
  const effect = <T>(fn: Effect<T>['fn'], options: Omit<Options, 'value'> = {}) => {
289
- if (!scope) {
289
+ let node = new Signal(undefined as any, DIRTY, EFFECT, options);
290
+
291
+ if (scope !== null) {
292
+ node.root = scope;
293
+ }
294
+ else if (observer !== null && observer.type === EFFECT && observer.root !== null) {
295
+ node.root = observer.root;
296
+ }
297
+ else {
290
298
  throw new Error('Reactivity: `effects` cannot be created without a reactive root');
291
299
  }
292
300
 
293
- let node = new Signal(undefined as any, DIRTY, EFFECT, options);
294
-
295
301
  node.fn = fn;
296
- node.root = scope;
297
302
  node.task = () => read(node);
298
303
 
299
304
  node.root.scheduler(node.task);
@@ -306,8 +311,8 @@ const read = <T>(node: Signal<T>): typeof node['value'] => {
306
311
  return node.value;
307
312
  }
308
313
 
309
- if (observer) {
310
- if (!observers) {
314
+ if (observer !== null) {
315
+ if (observers === null) {
311
316
  if (observer.sources !== null && observer.sources[index] == node) {
312
317
  index++;
313
318
  }
@@ -320,7 +325,7 @@ const read = <T>(node: Signal<T>): typeof node['value'] => {
320
325
  }
321
326
  }
322
327
 
323
- if (node.fn) {
328
+ if (node.fn !== null) {
324
329
  sync(node);
325
330
  }
326
331
 
@@ -1,7 +0,0 @@
1
- declare const _default: (fn: (this: import("../types").Context, previous: unknown) => unknown, options?: import("../types").Options | undefined) => {
2
- dispose: () => void;
3
- on: (event: symbol, listener: import("../types").Listener) => void;
4
- once: (event: symbol, listener: import("../types").Listener) => void;
5
- reset: () => void;
6
- };
7
- export default _default;
@@ -1,3 +0,0 @@
1
- import { effect } from '../signal';
2
- import context from '../context';
3
- export default (...args) => context.node({}, effect(...args));
@@ -1,4 +0,0 @@
1
- export { default as effect } from './effect';
2
- export { default as macro } from './macro';
3
- export { default as promise } from './promise';
4
- export { default as store } from './store';
@@ -1,4 +0,0 @@
1
- export { default as effect } from './effect';
2
- export { default as macro } from './macro';
3
- export { default as promise } from './promise';
4
- export { default as store } from './store';
@@ -1,9 +0,0 @@
1
- import { computed } from '../signal';
2
- import { Computed } from '../types';
3
- declare const _default: <T extends <A, R>(...args: A[]) => R>(fn: T extends Promise<unknown> ? never : (this: import("../types").Context, previous: T) => T, options?: Parameters<typeof computed>[1]) => {
4
- dispose: () => void;
5
- on: (event: symbol, listener: import("../types").Listener) => void;
6
- once: (event: symbol, listener: import("../types").Listener) => void;
7
- reset: () => void;
8
- };
9
- export default _default;
@@ -1,8 +0,0 @@
1
- import { computed, read } from '../signal';
2
- import context from '../context';
3
- export default (fn, options = {}) => {
4
- let node = computed(fn, options);
5
- return context.node((...args) => {
6
- return read(node)(...args);
7
- }, node);
8
- };
@@ -1,8 +0,0 @@
1
- import { computed } from '../signal';
2
- declare const _default: (fn: <A, R extends Promise<any>>(...args: A[]) => R, options?: Parameters<typeof computed>[1]) => {
3
- dispose: () => void;
4
- on: (event: symbol, listener: import("../types").Listener) => void;
5
- once: (event: symbol, listener: import("../types").Listener) => void;
6
- reset: () => void;
7
- };
8
- export default _default;
@@ -1,38 +0,0 @@
1
- import { read, root, signal, write } from '../signal';
2
- import context from '../context';
3
- export default (fn, options = {}) => {
4
- let input, nodes = {
5
- data: signal(options?.value, options),
6
- status: signal(undefined, options)
7
- };
8
- function host(...args) {
9
- input = args;
10
- root(() => {
11
- fn(...args)
12
- .then((value) => {
13
- write(nodes.data, value);
14
- })
15
- .catch(() => {
16
- write(nodes.data, undefined);
17
- });
18
- });
19
- }
20
- Object.defineProperties(host, {
21
- data: {
22
- get() {
23
- return read(nodes.data);
24
- }
25
- },
26
- input: {
27
- get() {
28
- return input;
29
- }
30
- },
31
- status: {
32
- get() {
33
- return read(nodes.status);
34
- }
35
- }
36
- });
37
- return context.nodes(host, nodes);
38
- };
@@ -1,11 +0,0 @@
1
- import { computed, signal } from '../signal';
2
- import { Context } from '../types';
3
- type Infer<T> = T extends (...args: unknown[]) => unknown ? ReturnType<T> : T extends Record<PropertyKey, unknown> ? {
4
- [K in keyof T]: T[K];
5
- } : T;
6
- type Never = {
7
- [K in keyof Context]?: never;
8
- };
9
- type Options = Parameters<typeof computed>[1] | Parameters<typeof signal>[1];
10
- declare const _default: <T extends Record<PropertyKey, unknown>>(data: T & Never, options?: Options) => Required<Infer<T> & Context & Partial<Context>> extends infer T_1 ? { [K in keyof T_1]: Required<Infer<T> & Context & Partial<Context>>[K]; } : never;
11
- export default _default;
@@ -1,27 +0,0 @@
1
- import { computed, read, signal, write } from '../signal';
2
- import context from '../context';
3
- export default (data, options = {}) => {
4
- let host = {}, nodes = {};
5
- for (let key in data) {
6
- if (typeof data[key] === 'function') {
7
- nodes[key] = computed(data[key], options);
8
- Object.defineProperty(host, key, {
9
- get() {
10
- return read(nodes[key]);
11
- }
12
- });
13
- }
14
- else {
15
- nodes[key] = signal(data[key], options);
16
- Object.defineProperty(host, key, {
17
- get() {
18
- return read(nodes[key]);
19
- },
20
- set(data) {
21
- write(nodes[key], data);
22
- }
23
- });
24
- }
25
- }
26
- return context.nodes(host, nodes);
27
- };
package/build/bench.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/build/bench.js DELETED
@@ -1,46 +0,0 @@
1
- import object from './reactive';
2
- import object2 from './reactive/object2';
3
- console.time('original');
4
- for (let i = 0, n = 10000; i < n; i++) {
5
- let obj = object({
6
- something: 'else',
7
- what: 0,
8
- does: 'sadsadsadsaasd',
9
- elser: true,
10
- hey: [
11
- {
12
- something: 'else',
13
- what: 0,
14
- does: 'sadsadsadsaasd',
15
- elser: true,
16
- }
17
- ]
18
- });
19
- obj.something;
20
- obj.what;
21
- obj.does;
22
- obj.hey[0].something;
23
- }
24
- console.timeEnd('original');
25
- console.time('lazy');
26
- for (let i = 0, n = 10000; i < n; i++) {
27
- let obj = object2({
28
- something: 'else',
29
- what: 0,
30
- does: 'sadsadsadsaasd',
31
- elser: true,
32
- hey: [
33
- {
34
- something: 'else',
35
- what: 0,
36
- does: 'sadsadsadsaasd',
37
- elser: true,
38
- }
39
- ]
40
- });
41
- obj.something;
42
- obj.what;
43
- obj.does;
44
- obj.hey[0].something;
45
- }
46
- console.timeEnd('lazy');
@@ -1,5 +0,0 @@
1
- declare const _default: {
2
- node: <T>(host: T & Partial<import("../types").Context>, node: import("../signal").default<any>) => Required<T & Partial<import("../types").Context>> extends infer T_1 ? { [K in keyof T_1]: Required<T & Partial<import("../types").Context>>[K]; } : never;
3
- nodes: <T_2>(host: T_2 & Partial<import("../types").Context>, nodes: Record<PropertyKey, import("../signal").default<any>>) => Required<T_2 & Partial<import("../types").Context>> extends infer T_3 ? { [K_1 in keyof T_3]: Required<T_2 & Partial<import("../types").Context>>[K_1]; } : never;
4
- };
5
- export default _default;
@@ -1,3 +0,0 @@
1
- import node from './node';
2
- import nodes from './nodes';
3
- export default { node, nodes };
@@ -1,7 +0,0 @@
1
- import { NODE } from '../constants';
2
- import { Context, Signal } from '../types';
3
- type Internals = {
4
- [NODE]: Signal<any>;
5
- };
6
- declare const _default: <T>(host: T & Partial<Context>, node: Internals[typeof NODE]) => Required<T & Partial<Context>> extends infer T_1 ? { [K in keyof T_1]: Required<T & Partial<Context>>[K]; } : never;
7
- export default _default;
@@ -1,21 +0,0 @@
1
- import { NODE } from '../constants';
2
- function dispose() {
3
- this[NODE].dispose();
4
- }
5
- function on(event, listener) {
6
- this[NODE].on(event, listener);
7
- }
8
- function once(event, listener) {
9
- this[NODE].once(event, listener);
10
- }
11
- function reset() {
12
- this[NODE].reset();
13
- }
14
- export default (host, node) => {
15
- host[NODE] = node;
16
- host.dispose = dispose;
17
- host.on = on;
18
- host.once = once;
19
- host.reset = reset;
20
- return host;
21
- };
@@ -1,7 +0,0 @@
1
- import { NODES } from '../constants';
2
- import { Context, Signal } from '../types';
3
- type Internals = {
4
- [NODES]: Record<PropertyKey, Signal<any>>;
5
- };
6
- declare const _default: <T>(host: T & Partial<Context>, nodes: Internals[typeof NODES]) => Required<T & Partial<Context>> extends infer T_1 ? { [K in keyof T_1]: Required<T & Partial<Context>>[K]; } : never;
7
- export default _default;
@@ -1,33 +0,0 @@
1
- import { NODES } from '../constants';
2
- function dispose() {
3
- let nodes = this[NODES];
4
- for (let key in nodes) {
5
- nodes[key].dispose();
6
- }
7
- }
8
- function on(event, listener) {
9
- let nodes = this[NODES];
10
- for (let key in nodes) {
11
- nodes[key].on(event, listener);
12
- }
13
- }
14
- function once(event, listener) {
15
- let nodes = this[NODES];
16
- for (let key in nodes) {
17
- nodes[key].once(event, listener);
18
- }
19
- }
20
- function reset() {
21
- let nodes = this[NODES];
22
- for (let key in nodes) {
23
- nodes[key].reset();
24
- }
25
- }
26
- export default (host, nodes) => {
27
- host[NODES] = nodes;
28
- host.dispose = dispose;
29
- host.on = on;
30
- host.once = once;
31
- host.reset = reset;
32
- return host;
33
- };
package/build/effect.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { API, Effect, Options, Prettify } from './types';
2
- declare const _default: <T>(fn: (previous: T) => T, options: Options) => Omit<API<T>, "value"> extends infer T_1 ? { [K in keyof T_1]: Omit<API<T>, "value">[K]; } : never;
3
- export default _default;
package/build/effect.js DELETED
@@ -1,2 +0,0 @@
1
- import { effect } from './signal';
2
- export default effect;
@@ -1,28 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { computed } from './signal';
3
- import { API, Signal } from './types';
4
- type Fn = <A, R extends Promise<any>>(...args: A[]) => R;
5
- type Options = Parameters<typeof computed>[1];
6
- type R<T extends Fn> = Omit<ReactivePromise<T>, 'fn' | 'input' | 'nodes'> & {
7
- readonly fn: T;
8
- readonly input: Parameters<T> | null;
9
- readonly nodes: {
10
- data: API<ReturnType<T>>;
11
- ok: API<boolean | null>;
12
- };
13
- };
14
- declare class ReactivePromise<T extends Fn> extends CustomFunction {
15
- fn: T;
16
- input: Parameters<T> | null;
17
- nodes: {
18
- data: Signal<ReturnType<T>>;
19
- ok: Signal<boolean | null>;
20
- };
21
- stop: boolean | null;
22
- constructor(fn: T, options?: Options);
23
- get data(): ReturnType<T> | ReturnType<ReturnType<T> extends infer T_1 ? T_1 extends ReturnType<T> ? T_1 extends Promise<unknown> ? never : (previous: T_1) => T_1 : never : never>;
24
- get ok(): boolean | null;
25
- invoke(...args: Parameters<T>): void;
26
- }
27
- declare const _default: <T extends Fn>(fn: T, options?: Options) => R<T>;
28
- export default _default;
package/build/promise.js DELETED
@@ -1,45 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { read, root, signal, write } from './signal';
3
- class ReactivePromise extends CustomFunction {
4
- fn;
5
- input = null;
6
- nodes;
7
- stop = null;
8
- constructor(fn, options = {}) {
9
- super((...args) => this.invoke(...args));
10
- this.fn = fn;
11
- this.nodes = {
12
- data: signal(options.value, options),
13
- ok: signal(null, options)
14
- };
15
- }
16
- get data() {
17
- return read(this.nodes.data);
18
- }
19
- get ok() {
20
- return read(this.nodes.ok);
21
- }
22
- invoke(...args) {
23
- this.input = args;
24
- this.stop = null;
25
- root(() => {
26
- write(this.nodes.ok, null);
27
- this.fn(...args)
28
- .then((value) => {
29
- if (this.stop === true) {
30
- return;
31
- }
32
- write(this.nodes.data, value);
33
- write(this.nodes.ok, true);
34
- })
35
- .catch(() => {
36
- if (this.stop === true) {
37
- return;
38
- }
39
- write(this.nodes.data, undefined);
40
- write(this.nodes.ok, false);
41
- });
42
- });
43
- }
44
- }
45
- export default (fn, options = {}) => new ReactivePromise(fn, options);
@@ -1,10 +0,0 @@
1
- import { Infer, Key, Node, Options, Values } from './types';
2
- declare class ReactiveObject<K extends Key, V> {
3
- nodes: Record<K, Node<K, V>>;
4
- constructor(data: Values<K, V>, options?: Options);
5
- dispose(): void;
6
- reset(): void;
7
- }
8
- declare const _default: <K extends Key, V>(data: Values<K, V>, options?: Options) => ReactiveObject<K, V> & Infer<Record<K, V>>;
9
- export default _default;
10
- export { ReactiveObject };
@@ -1,65 +0,0 @@
1
- import { computed, read, signal, write } from '../signal';
2
- import { isArray } from '../utilities';
3
- import ReactiveArray from './array';
4
- class ReactiveObject {
5
- nodes = {};
6
- constructor(data, options = {}) {
7
- let nodes = this.nodes;
8
- for (let key in data) {
9
- let node = nodes[key], value = data[key];
10
- if (typeof value === 'function') {
11
- Object.defineProperty(this, key, {
12
- get() {
13
- if (node === undefined) {
14
- node = nodes[key] = computed(value, options);
15
- }
16
- return read(node);
17
- }
18
- });
19
- }
20
- else if (isArray(value) && value[0]?.constructor?.name === 'Object') {
21
- Object.defineProperty(this, key, {
22
- get() {
23
- if (node === undefined) {
24
- node = nodes[key] = new ReactiveArray(value, options);
25
- }
26
- node.track();
27
- return node;
28
- }
29
- });
30
- }
31
- else {
32
- Object.defineProperty(this, key, {
33
- get() {
34
- if (node === undefined) {
35
- node = nodes[key] = signal(value, options);
36
- }
37
- return read(node);
38
- },
39
- set(value) {
40
- if (node === undefined) {
41
- node = nodes[key] = signal(value, options);
42
- }
43
- write(node, value);
44
- }
45
- });
46
- }
47
- }
48
- }
49
- dispose() {
50
- let nodes = this.nodes;
51
- for (let key in nodes) {
52
- nodes[key].dispose();
53
- }
54
- }
55
- reset() {
56
- let nodes = this.nodes;
57
- for (let key in nodes) {
58
- nodes[key].reset();
59
- }
60
- }
61
- }
62
- export default (data, options = {}) => {
63
- return new ReactiveObject(data, options);
64
- };
65
- export { ReactiveObject };
@@ -1,33 +0,0 @@
1
- import { computed, signal } from '../signal';
2
- import { Computed, Signal } from '../types';
3
- import { ReactiveObject } from './object';
4
- import ReactiveArray from './array';
5
- type Events<T> = {
6
- pop: {
7
- item: T;
8
- };
9
- push: {
10
- items: T[];
11
- };
12
- shift: {
13
- item: T;
14
- };
15
- splice: {
16
- deleteCount: number;
17
- items: T[];
18
- start: number;
19
- };
20
- unshift: {
21
- items: T[];
22
- };
23
- };
24
- type Infer<T> = T extends (...args: unknown[]) => unknown ? ReturnType<T> : T extends unknown[] ? Infer<T> : T extends Record<Key, unknown> ? {
25
- [K in keyof T]: T[K];
26
- } : T;
27
- type Key = string | symbol;
28
- type Node<K extends Key, V> = Computed<V> | ReactiveArray<K, V> | Signal<V>;
29
- type Options = Parameters<typeof computed>[1] | Parameters<typeof signal>[1];
30
- type Values<K extends Key, V> = Record<K, V> & {
31
- [K in keyof ReactiveObject<Key, unknown>]?: never;
32
- };
33
- export { Events, Infer, Key, Node, Options, Values };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,106 +0,0 @@
1
- import { read, signal } from './signal';
2
- import { Trigger } from './trigger';
3
- let { isArray } = Array;
4
- function dispose(dispose) {
5
- if (dispose === undefined) {
6
- }
7
- else if (isArray(dispose)) {
8
- for (let i = 0, n = dispose.length; i < n; i++) {
9
- dispose[i].dispose();
10
- }
11
- }
12
- else {
13
- dispose.dispose();
14
- }
15
- return dispose;
16
- }
17
- class Reactive extends Trigger {
18
- nodes;
19
- constructor(nodes) {
20
- super();
21
- this.nodes = nodes;
22
- }
23
- [Symbol.iterator]() {
24
- let i = 0;
25
- return {
26
- next: () => {
27
- if (i < this.nodes.length) {
28
- return {
29
- done: false,
30
- value: read(this.nodes[i++])
31
- };
32
- }
33
- return { done: true };
34
- }
35
- };
36
- }
37
- get length() {
38
- return this.nodes.length;
39
- }
40
- set length(n) {
41
- this.splice(n);
42
- }
43
- get(index) {
44
- if (index in this.nodes) {
45
- return read(this.nodes[index]);
46
- }
47
- return undefined;
48
- }
49
- pop() {
50
- let signal = dispose(this.nodes.pop());
51
- if (signal !== undefined) {
52
- this.dispatch('action', {
53
- signal,
54
- type: 'pop'
55
- });
56
- }
57
- return signal;
58
- }
59
- push(...values) {
60
- let signals = [];
61
- for (let i = 0, n = values.length; i < n; i++) {
62
- signals.push(signal(values[i]));
63
- }
64
- this.nodes.push(...signals);
65
- this.dispatch('action', {
66
- signals,
67
- type: 'push'
68
- });
69
- return this.nodes.length;
70
- }
71
- shift() {
72
- let signal = dispose(this.nodes.shift());
73
- if (signal !== undefined) {
74
- this.dispatch('action', {
75
- signal,
76
- type: 'shift'
77
- });
78
- }
79
- return signal;
80
- }
81
- splice(start, deleteCount = this.nodes.length, ...values) {
82
- let signals = [];
83
- for (let i = 0, n = values.length; i < n; i++) {
84
- signals.push(signal(values[i]));
85
- }
86
- let disposed = dispose(this.nodes.splice(start, deleteCount, ...signals));
87
- this.dispatch('action', {
88
- deleteCount,
89
- signals,
90
- start,
91
- type: 'splice'
92
- });
93
- return disposed;
94
- }
95
- unshift(...values) {
96
- let signals = [];
97
- for (let i = 0, n = values.length; i < n; i++) {
98
- signals.push(signal(values[i]));
99
- }
100
- this.dispatch('action', {
101
- signals,
102
- type: 'unshift'
103
- });
104
- return this.nodes.unshift(...signals);
105
- }
106
- }
@@ -1,11 +0,0 @@
1
- import { computed, signal } from './signal';
2
- import { Context } from './types';
3
- type Infer<T> = T extends (...args: unknown[]) => unknown ? ReturnType<T> : T extends Record<PropertyKey, unknown> ? {
4
- [K in keyof T]: T[K];
5
- } : T;
6
- type Never = {
7
- [K in keyof Context]?: never;
8
- };
9
- type Options = Parameters<typeof computed>[1] | Parameters<typeof signal>[1];
10
- declare const _default: <T extends Record<PropertyKey, unknown>>(data: T & Never, options?: Options) => Required<Infer<T> & Context & Partial<Context>> extends infer T_1 ? { [K in keyof T_1]: Required<Infer<T> & Context & Partial<Context>>[K]; } : never;
11
- export default _default;
package/build/reactive.js DELETED
@@ -1,27 +0,0 @@
1
- import { computed, read, signal, write } from './signal';
2
- import context from './context';
3
- export default (data, options = {}) => {
4
- let host = {}, nodes = {};
5
- for (let key in data) {
6
- if (typeof data[key] === 'function') {
7
- nodes[key] = computed(data[key], options);
8
- Object.defineProperty(host, key, {
9
- get() {
10
- return read(nodes[key]);
11
- }
12
- });
13
- }
14
- else {
15
- nodes[key] = signal(data[key], options);
16
- Object.defineProperty(host, key, {
17
- get() {
18
- return read(nodes[key]);
19
- },
20
- set(value) {
21
- write(nodes[key], value);
22
- }
23
- });
24
- }
25
- }
26
- return context.nodes(host, nodes);
27
- };
@@ -1,13 +0,0 @@
1
- declare const CLEAN = 0;
2
- declare const CHECK = 1;
3
- declare const DIRTY = 2;
4
- declare const DISPOSED = 3;
5
- declare const COMPUTED = 0;
6
- declare const EFFECT = 1;
7
- declare const SIGNAL = 2;
8
- declare const DISPOSE: unique symbol;
9
- declare const RESET: unique symbol;
10
- declare const UPDATE: unique symbol;
11
- declare const NODE: unique symbol;
12
- declare const NODES: unique symbol;
13
- export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, DISPOSE, EFFECT, NODE, NODES, RESET, SIGNAL, UPDATE };
package/build/symbols.js DELETED
@@ -1,13 +0,0 @@
1
- const CLEAN = 0;
2
- const CHECK = 1;
3
- const DIRTY = 2;
4
- const DISPOSED = 3;
5
- const COMPUTED = 0;
6
- const EFFECT = 1;
7
- const SIGNAL = 2;
8
- const DISPOSE = Symbol();
9
- const RESET = Symbol();
10
- const UPDATE = Symbol();
11
- const NODE = Symbol();
12
- const NODES = Symbol();
13
- export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, DISPOSE, EFFECT, NODE, NODES, RESET, SIGNAL, UPDATE };
@@ -1,13 +0,0 @@
1
- import { Event, Listener, Signal } from '../types';
2
- import CustomFunction from '@esportsplus/custom-function';
3
- type Node = Signal<any>;
4
- declare class Context<T extends Function> extends CustomFunction {
5
- signal: Signal<any>;
6
- constructor(host: T, node: Node);
7
- dispose(): void;
8
- on(event: Event, listener: Listener): void;
9
- once(event: Event, listener: Listener): void;
10
- reset(): void;
11
- }
12
- declare const _default: <T extends Function>(fn: T, node: Node) => Context<T>;
13
- export default _default;
@@ -1,21 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- class Context extends CustomFunction {
3
- signal;
4
- constructor(host, node) {
5
- super(host);
6
- this.signal = node;
7
- }
8
- dispose() {
9
- this.signal.dispose();
10
- }
11
- on(event, listener) {
12
- this.signal.on(event, listener);
13
- }
14
- once(event, listener) {
15
- this.signal.once(event, listener);
16
- }
17
- reset() {
18
- this.signal.reset();
19
- }
20
- }
21
- export default (fn, node) => new Context(fn, node);
@@ -1,13 +0,0 @@
1
- import { Event, Listener, Signal } from '../types';
2
- import CustomFunction from '@esportsplus/custom-function';
3
- type Nodes = Record<PropertyKey, Signal<any>>;
4
- declare class Context<T extends Function> extends CustomFunction {
5
- #private;
6
- constructor(host: T, nodes: Nodes);
7
- dispose(): void;
8
- on(event: Event, listener: Listener): void;
9
- once(event: Event, listener: Listener): void;
10
- reset(): void;
11
- }
12
- declare const _default: <T extends Function>(fn: T, nodes?: Nodes) => Context<T>;
13
- export default _default;
@@ -1,33 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- class Context extends CustomFunction {
3
- #nodes;
4
- constructor(host, nodes) {
5
- super(host);
6
- this.#nodes = nodes;
7
- }
8
- dispose() {
9
- let nodes = this.#nodes;
10
- for (let key in nodes) {
11
- nodes[key].dispose();
12
- }
13
- }
14
- on(event, listener) {
15
- let nodes = this.#nodes;
16
- for (let key in nodes) {
17
- nodes[key].on(event, listener);
18
- }
19
- }
20
- once(event, listener) {
21
- let nodes = this.#nodes;
22
- for (let key in nodes) {
23
- nodes[key].once(event, listener);
24
- }
25
- }
26
- reset() {
27
- let nodes = this.#nodes;
28
- for (let key in nodes) {
29
- nodes[key].reset();
30
- }
31
- }
32
- }
33
- export default (fn, nodes = {}) => new Context(fn, nodes);
File without changes
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,15 +0,0 @@
1
- import { Event, Listener, Signal } from './types';
2
- declare class Trigger {
3
- signal: Signal<boolean>;
4
- constructor();
5
- dispatch<T>(event: Event, data?: T): void;
6
- dispose(): void;
7
- on(event: Event, listener: Listener): void;
8
- once(event: Event, listener: Listener): void;
9
- reset(): void;
10
- track(): void;
11
- trigger(): void;
12
- }
13
- declare const _default: () => Trigger;
14
- export default _default;
15
- export { Trigger };
package/build/trigger.js DELETED
@@ -1,30 +0,0 @@
1
- import { read, signal, write } from './signal';
2
- class Trigger {
3
- signal;
4
- constructor() {
5
- this.signal = signal(false);
6
- }
7
- dispatch(event, data) {
8
- this.signal.dispatch(event, data);
9
- }
10
- dispose() {
11
- this.signal.dispose();
12
- }
13
- on(event, listener) {
14
- this.signal.on(event, listener);
15
- }
16
- once(event, listener) {
17
- this.signal.once(event, listener);
18
- }
19
- reset() {
20
- this.signal.reset();
21
- }
22
- track() {
23
- read(this.signal);
24
- }
25
- trigger() {
26
- write(this.signal, !this.signal.value);
27
- }
28
- }
29
- export default () => new Trigger();
30
- export { Trigger };