@planet-matrix/mobius-model 0.1.3 → 0.3.0

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 (77) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +21 -0
  3. package/dist/index.d.ts +1 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +2 -2
  6. package/dist/index.js.map +12 -6
  7. package/dist/reactor/index.d.ts +3 -0
  8. package/dist/reactor/index.d.ts.map +1 -0
  9. package/dist/reactor/reactor-core/flags.d.ts.map +1 -0
  10. package/dist/reactor/reactor-core/index.d.ts.map +1 -0
  11. package/dist/reactor/reactor-core/primitive.d.ts +276 -0
  12. package/dist/reactor/reactor-core/primitive.d.ts.map +1 -0
  13. package/dist/{signal/signal-core → reactor/reactor-core}/reactive-system.d.ts +102 -22
  14. package/dist/reactor/reactor-core/reactive-system.d.ts.map +1 -0
  15. package/dist/reactor/reactor-operators/branch.d.ts +19 -0
  16. package/dist/reactor/reactor-operators/branch.d.ts.map +1 -0
  17. package/dist/reactor/reactor-operators/convert.d.ts +30 -0
  18. package/dist/reactor/reactor-operators/convert.d.ts.map +1 -0
  19. package/dist/reactor/reactor-operators/create.d.ts +26 -0
  20. package/dist/reactor/reactor-operators/create.d.ts.map +1 -0
  21. package/dist/reactor/reactor-operators/filter.d.ts +269 -0
  22. package/dist/reactor/reactor-operators/filter.d.ts.map +1 -0
  23. package/dist/reactor/reactor-operators/index.d.ts +8 -0
  24. package/dist/reactor/reactor-operators/index.d.ts.map +1 -0
  25. package/dist/reactor/reactor-operators/join.d.ts +48 -0
  26. package/dist/reactor/reactor-operators/join.d.ts.map +1 -0
  27. package/dist/reactor/reactor-operators/map.d.ts +165 -0
  28. package/dist/reactor/reactor-operators/map.d.ts.map +1 -0
  29. package/dist/reactor/reactor-operators/utility.d.ts +48 -0
  30. package/dist/reactor/reactor-operators/utility.d.ts.map +1 -0
  31. package/package.json +9 -12
  32. package/src/index.ts +1 -1
  33. package/src/reactor/README.md +18 -0
  34. package/src/reactor/index.ts +2 -0
  35. package/src/reactor/reactor-core/primitive.ts +1046 -0
  36. package/src/{signal/signal-core → reactor/reactor-core}/reactive-system.ts +392 -93
  37. package/src/reactor/reactor-operators/branch.ts +66 -0
  38. package/src/reactor/reactor-operators/convert.ts +70 -0
  39. package/src/reactor/reactor-operators/create.ts +66 -0
  40. package/src/reactor/reactor-operators/filter.ts +988 -0
  41. package/src/reactor/reactor-operators/index.ts +7 -0
  42. package/src/reactor/reactor-operators/join.ts +174 -0
  43. package/src/reactor/reactor-operators/map.ts +599 -0
  44. package/src/reactor/reactor-operators/utility.ts +102 -0
  45. package/tests/unit/{signal/computed.spec.ts → reactor/alien-signals-computed.spec.ts} +15 -10
  46. package/tests/unit/reactor/alien-signals-effect-scope.spec.ts +86 -0
  47. package/tests/unit/reactor/alien-signals-effect.spec.ts +395 -0
  48. package/tests/unit/reactor/alien-signals-topology.spec.ts +361 -0
  49. package/tests/unit/reactor/alien-signals-trigger.spec.ts +75 -0
  50. package/tests/unit/reactor/alien-signals-untrack.spec.ts +91 -0
  51. package/tests/unit/reactor/preact-signal.spec.ts +73 -0
  52. package/tests/unit/reactor/reactor-core.spec.ts +219 -0
  53. package/tests/unit/reactor/reactor-operators-branch.spec.ts +33 -0
  54. package/tests/unit/reactor/reactor-operators-convert.spec.ts +31 -0
  55. package/tests/unit/reactor/reactor-operators-create.spec.ts +47 -0
  56. package/tests/unit/reactor/reactor-operators-filter.spec.ts +604 -0
  57. package/tests/unit/reactor/reactor-operators-join.spec.ts +94 -0
  58. package/tests/unit/reactor/reactor-operators-map.spec.ts +327 -0
  59. package/tests/unit/reactor/reactor-operators-utility.spec.ts +55 -0
  60. package/dist/signal/index.d.ts +0 -3
  61. package/dist/signal/index.d.ts.map +0 -1
  62. package/dist/signal/signal-core/flags.d.ts.map +0 -1
  63. package/dist/signal/signal-core/index.d.ts.map +0 -1
  64. package/dist/signal/signal-core/primitive.d.ts +0 -67
  65. package/dist/signal/signal-core/primitive.d.ts.map +0 -1
  66. package/dist/signal/signal-core/reactive-system.d.ts.map +0 -1
  67. package/dist/signal/signal-operators/index.d.ts +0 -4
  68. package/dist/signal/signal-operators/index.d.ts.map +0 -1
  69. package/src/signal/index.ts +0 -2
  70. package/src/signal/signal-core/README.md +0 -4
  71. package/src/signal/signal-core/primitive.ts +0 -275
  72. package/src/signal/signal-operators/index.ts +0 -19
  73. package/tests/unit/signal/effect.spec.ts +0 -108
  74. /package/dist/{signal/signal-core → reactor/reactor-core}/flags.d.ts +0 -0
  75. /package/dist/{signal/signal-core → reactor/reactor-core}/index.d.ts +0 -0
  76. /package/src/{signal/signal-core → reactor/reactor-core}/flags.ts +0 -0
  77. /package/src/{signal/signal-core → reactor/reactor-core}/index.ts +0 -0
@@ -0,0 +1,66 @@
1
+ import type { Signal, ValueReactor } from "../reactor-core/index.ts"
2
+
3
+ import { castValueInitializer } from "./utility.ts"
4
+ import { effect, signal } from "../reactor-core/index.ts"
5
+
6
+ export interface CloneOptions<V> {
7
+ target: ValueReactor<V>;
8
+ }
9
+ /**
10
+ * Creates a Signal that mirrors the value of the target ValueReactor.
11
+ */
12
+ export const clone = <V>(
13
+ options: CloneOptions<V>
14
+ ): Signal<V> => {
15
+ const { target } = options;
16
+
17
+ const result = signal(castValueInitializer<V>(), {
18
+ onDispose: () => {
19
+ eTarget.dispose()
20
+ }
21
+ })
22
+
23
+ const eTarget = effect(() => {
24
+ const value = target.get()
25
+ result.set(value)
26
+ })
27
+
28
+ return result
29
+ }
30
+
31
+ export interface PartitionOptions<V> {
32
+ target: ValueReactor<V>;
33
+ predicate: (value: V) => boolean;
34
+ }
35
+ /**
36
+ * Partitions values from a ValueReactor into two Signals based on a predicate function.
37
+ * The first Signal emits values that satisfy the predicate, while the second Signal
38
+ * emits values that do not satisfy the predicate.
39
+ */
40
+ export const partition = <V>(
41
+ options: PartitionOptions<V>
42
+ ): [Signal<V | undefined>, Signal<V | undefined>] => {
43
+ const { target, predicate } = options;
44
+
45
+ const trueSignal = signal(castValueInitializer<V | undefined>(), {
46
+ onDispose: () => {
47
+ eTarget.dispose()
48
+ }
49
+ })
50
+ const falseSignal = signal(castValueInitializer<V | undefined>(), {
51
+ onDispose: () => {
52
+ eTarget.dispose()
53
+ }
54
+ })
55
+
56
+ const eTarget = effect(() => {
57
+ const value = target.get()
58
+ if (predicate(value) === true) {
59
+ trueSignal.set(value)
60
+ } else {
61
+ falseSignal.set(value)
62
+ }
63
+ })
64
+
65
+ return [trueSignal, falseSignal]
66
+ }
@@ -0,0 +1,70 @@
1
+ import type { Signal, ValueReactor } from "../reactor-core/index.ts"
2
+
3
+ import { signal } from "../reactor-core/index.ts"
4
+ import { subscribe } from "./utility.ts"
5
+
6
+ export interface FromPromiseSyncOptions<V> {
7
+ target: Promise<V>;
8
+ }
9
+ /**
10
+ * Converts a Promise into a Signal that updates its value
11
+ * when the Promise resolves.
12
+ *
13
+ * @see {@link fromPromiseAsync} for an async version.
14
+ */
15
+ export const fromPromiseSync = <V>(
16
+ options: FromPromiseSyncOptions<V>
17
+ ): Signal<V | undefined> => {
18
+ const { target } = options
19
+
20
+ const result = signal<V | undefined>(() => undefined)
21
+ void target.then((value) => {
22
+ return result.set(value)
23
+ })
24
+ return result
25
+ }
26
+
27
+ export interface FromPromiseAsyncOptions<V> {
28
+ target: Promise<V>;
29
+ }
30
+ /**
31
+ * Converts a Promise into a Signal that is initialized
32
+ * with the resolved value of the Promise.
33
+ *
34
+ * @see {@link fromPromiseSync} for a sync version.
35
+ */
36
+ export const fromPromiseAsync = async <V>(
37
+ options: FromPromiseAsyncOptions<V>
38
+ ): Promise<Signal<V>> => {
39
+ const { target } = options
40
+
41
+ const value = await target
42
+ const result = signal<V>(() => value)
43
+ return result
44
+ }
45
+
46
+ export interface ToPromiseOptions<V> {
47
+ target: ValueReactor<V>;
48
+ }
49
+ /**
50
+ * Converts a ValueReactor into a Promise that resolves
51
+ * with the next value emitted by the ValueReactor.
52
+ */
53
+ export const toPromise = async <V>(
54
+ options: ToPromiseOptions<V>
55
+ ): Promise<V> => {
56
+ const { target } = options
57
+
58
+ let _resolve: (value: V) => void
59
+ const promise = new Promise<V>((resolve) => {
60
+ _resolve = resolve
61
+ })
62
+ const unsubscribe = subscribe({
63
+ target,
64
+ subscriber: (value) => {
65
+ unsubscribe()
66
+ _resolve(value)
67
+ }
68
+ })
69
+ return await promise
70
+ }
@@ -0,0 +1,66 @@
1
+ import type { Signal } from "../reactor-core/index.ts"
2
+
3
+ import { signal } from "../reactor-core/index.ts"
4
+
5
+ export interface TimerOptions {
6
+ delay: number
7
+ interval: number
8
+ initialValue?: number
9
+ step?: number
10
+ }
11
+ /**
12
+ * Creates a signal that starts emitting values after a specified delay,
13
+ * and continues to emit incremented values at specified intervals.
14
+ *
15
+ * The value starts at 0, becomes 1 after the delay, and increments by 1.
16
+ */
17
+ export const timer = (
18
+ options: TimerOptions
19
+ ): Signal<number> => {
20
+ const { delay, interval, initialValue = 0, step = 1 } = options
21
+
22
+ const result = signal<number>(() => initialValue, {
23
+ onDispose: () => {
24
+ clearTimeout(timeoutId)
25
+ clearInterval(intervalId)
26
+ }
27
+ })
28
+
29
+ let intervalId: ReturnType<typeof setInterval> | undefined = undefined
30
+ const timeoutId = setTimeout(() => {
31
+ result.set(result.getWithoutTrack() + step)
32
+ intervalId = setInterval(() => {
33
+ result.set(result.getWithoutTrack() + step)
34
+ }, interval)
35
+ }, delay)
36
+
37
+ return result
38
+ }
39
+
40
+ export interface IntervalOptions {
41
+ interval: number
42
+ initialValue?: number
43
+ step?: number
44
+ }
45
+ /**
46
+ * Creates a signal that increments its value at specified intervals.
47
+ *
48
+ * The value starts at 0 and increments by 1 every interval.
49
+ */
50
+ export const interval = (
51
+ options: IntervalOptions
52
+ ): Signal<number> => {
53
+ const { interval, initialValue = 0, step = 1 } = options
54
+
55
+ const result = signal<number>(() => initialValue, {
56
+ onDispose: () => {
57
+ clearInterval(intervalId)
58
+ }
59
+ })
60
+
61
+ const intervalId = setInterval(() => {
62
+ result.set(result.getWithoutTrack() + step)
63
+ }, interval)
64
+
65
+ return result
66
+ }