@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.
- package/CHANGELOG.md +54 -0
- package/README.md +21 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +12 -6
- package/dist/reactor/index.d.ts +3 -0
- package/dist/reactor/index.d.ts.map +1 -0
- package/dist/reactor/reactor-core/flags.d.ts.map +1 -0
- package/dist/reactor/reactor-core/index.d.ts.map +1 -0
- package/dist/reactor/reactor-core/primitive.d.ts +276 -0
- package/dist/reactor/reactor-core/primitive.d.ts.map +1 -0
- package/dist/{signal/signal-core → reactor/reactor-core}/reactive-system.d.ts +102 -22
- package/dist/reactor/reactor-core/reactive-system.d.ts.map +1 -0
- package/dist/reactor/reactor-operators/branch.d.ts +19 -0
- package/dist/reactor/reactor-operators/branch.d.ts.map +1 -0
- package/dist/reactor/reactor-operators/convert.d.ts +30 -0
- package/dist/reactor/reactor-operators/convert.d.ts.map +1 -0
- package/dist/reactor/reactor-operators/create.d.ts +26 -0
- package/dist/reactor/reactor-operators/create.d.ts.map +1 -0
- package/dist/reactor/reactor-operators/filter.d.ts +269 -0
- package/dist/reactor/reactor-operators/filter.d.ts.map +1 -0
- package/dist/reactor/reactor-operators/index.d.ts +8 -0
- package/dist/reactor/reactor-operators/index.d.ts.map +1 -0
- package/dist/reactor/reactor-operators/join.d.ts +48 -0
- package/dist/reactor/reactor-operators/join.d.ts.map +1 -0
- package/dist/reactor/reactor-operators/map.d.ts +165 -0
- package/dist/reactor/reactor-operators/map.d.ts.map +1 -0
- package/dist/reactor/reactor-operators/utility.d.ts +48 -0
- package/dist/reactor/reactor-operators/utility.d.ts.map +1 -0
- package/package.json +9 -12
- package/src/index.ts +1 -1
- package/src/reactor/README.md +18 -0
- package/src/reactor/index.ts +2 -0
- package/src/reactor/reactor-core/primitive.ts +1046 -0
- package/src/{signal/signal-core → reactor/reactor-core}/reactive-system.ts +392 -93
- package/src/reactor/reactor-operators/branch.ts +66 -0
- package/src/reactor/reactor-operators/convert.ts +70 -0
- package/src/reactor/reactor-operators/create.ts +66 -0
- package/src/reactor/reactor-operators/filter.ts +988 -0
- package/src/reactor/reactor-operators/index.ts +7 -0
- package/src/reactor/reactor-operators/join.ts +174 -0
- package/src/reactor/reactor-operators/map.ts +599 -0
- package/src/reactor/reactor-operators/utility.ts +102 -0
- package/tests/unit/{signal/computed.spec.ts → reactor/alien-signals-computed.spec.ts} +15 -10
- package/tests/unit/reactor/alien-signals-effect-scope.spec.ts +86 -0
- package/tests/unit/reactor/alien-signals-effect.spec.ts +395 -0
- package/tests/unit/reactor/alien-signals-topology.spec.ts +361 -0
- package/tests/unit/reactor/alien-signals-trigger.spec.ts +75 -0
- package/tests/unit/reactor/alien-signals-untrack.spec.ts +91 -0
- package/tests/unit/reactor/preact-signal.spec.ts +73 -0
- package/tests/unit/reactor/reactor-core.spec.ts +219 -0
- package/tests/unit/reactor/reactor-operators-branch.spec.ts +33 -0
- package/tests/unit/reactor/reactor-operators-convert.spec.ts +31 -0
- package/tests/unit/reactor/reactor-operators-create.spec.ts +47 -0
- package/tests/unit/reactor/reactor-operators-filter.spec.ts +604 -0
- package/tests/unit/reactor/reactor-operators-join.spec.ts +94 -0
- package/tests/unit/reactor/reactor-operators-map.spec.ts +327 -0
- package/tests/unit/reactor/reactor-operators-utility.spec.ts +55 -0
- package/dist/signal/index.d.ts +0 -3
- package/dist/signal/index.d.ts.map +0 -1
- package/dist/signal/signal-core/flags.d.ts.map +0 -1
- package/dist/signal/signal-core/index.d.ts.map +0 -1
- package/dist/signal/signal-core/primitive.d.ts +0 -67
- package/dist/signal/signal-core/primitive.d.ts.map +0 -1
- package/dist/signal/signal-core/reactive-system.d.ts.map +0 -1
- package/dist/signal/signal-operators/index.d.ts +0 -4
- package/dist/signal/signal-operators/index.d.ts.map +0 -1
- package/src/signal/index.ts +0 -2
- package/src/signal/signal-core/README.md +0 -4
- package/src/signal/signal-core/primitive.ts +0 -275
- package/src/signal/signal-operators/index.ts +0 -19
- package/tests/unit/signal/effect.spec.ts +0 -108
- /package/dist/{signal/signal-core → reactor/reactor-core}/flags.d.ts +0 -0
- /package/dist/{signal/signal-core → reactor/reactor-core}/index.d.ts +0 -0
- /package/src/{signal/signal-core → reactor/reactor-core}/flags.ts +0 -0
- /package/src/{signal/signal-core → reactor/reactor-core}/index.ts +0 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { AnyValueReactorArray, AnyValueReactorObject, GetValuesInArray, GetValuesInObject } from "./utility.ts"
|
|
2
|
+
import type { Effect, Signal, ValueReactor } from "../reactor-core/index.ts"
|
|
3
|
+
|
|
4
|
+
import { castValueInitializer, getValuesInArray, getValuesInObject } from "./utility.ts"
|
|
5
|
+
import { effect, signal } from "../reactor-core/index.ts"
|
|
6
|
+
|
|
7
|
+
export type CombineLatestArray<T extends AnyValueReactorArray> = Signal<GetValuesInArray<T>>;
|
|
8
|
+
export interface CombineLatestArrayOptions<T extends AnyValueReactorArray> {
|
|
9
|
+
target: T;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Combines multiple ValueReactors into a Signal that emits the latest values
|
|
13
|
+
* from each ValueReactor whenever any of them updates.
|
|
14
|
+
*/
|
|
15
|
+
export const combineLatestArray = <T extends AnyValueReactorArray>(
|
|
16
|
+
options: CombineLatestArrayOptions<T>
|
|
17
|
+
): CombineLatestArray<T> => {
|
|
18
|
+
const { target } = options;
|
|
19
|
+
|
|
20
|
+
const result = signal(castValueInitializer<GetValuesInArray<T>>(), {
|
|
21
|
+
onDispose: () => {
|
|
22
|
+
e.dispose()
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
const e = effect(() => {
|
|
27
|
+
result.set(getValuesInArray({ target }))
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
return result
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type CombineLatestObject<T extends AnyValueReactorObject> = Signal<GetValuesInObject<T>>;
|
|
34
|
+
export interface CombineLatestObjectOptions<T extends AnyValueReactorObject> {
|
|
35
|
+
target: T;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Combines multiple ValueReactors in an object into a Signal that emits the latest values
|
|
39
|
+
* from each ValueReactor whenever any of them updates.
|
|
40
|
+
*/
|
|
41
|
+
export const combineLatestObject = <T extends AnyValueReactorObject>(
|
|
42
|
+
options: CombineLatestObjectOptions<T>
|
|
43
|
+
): CombineLatestObject<T> => {
|
|
44
|
+
const { target } = options;
|
|
45
|
+
|
|
46
|
+
const result = signal(castValueInitializer<GetValuesInObject<T>>(), {
|
|
47
|
+
onDispose: () => {
|
|
48
|
+
e.dispose()
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const e = effect(() => {
|
|
53
|
+
result.set(getValuesInObject({ target }))
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
return result
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type MergeArray<T extends AnyValueReactorArray> = Signal<GetValuesInArray<T>[number]>;
|
|
60
|
+
export interface MergeArrayOptions<T extends AnyValueReactorArray> {
|
|
61
|
+
target: T;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Merges multiple ValueReactors into a Signal that emits values
|
|
65
|
+
* from any of the ValueReactors as they update.
|
|
66
|
+
*/
|
|
67
|
+
export const mergeArray = <T extends AnyValueReactorArray>(
|
|
68
|
+
options: MergeArrayOptions<T>
|
|
69
|
+
): MergeArray<T> => {
|
|
70
|
+
const { target } = options;
|
|
71
|
+
|
|
72
|
+
const result = signal(castValueInitializer<GetValuesInArray<T>[number]>(), {
|
|
73
|
+
onDispose: () => {
|
|
74
|
+
for (const e of effects) {
|
|
75
|
+
e.dispose()
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const effects: Effect[] = [];
|
|
81
|
+
for (const valueReactor of target) {
|
|
82
|
+
const e = effect(() => {
|
|
83
|
+
// oxlint-disable-next-line no-unsafe-type-assertion
|
|
84
|
+
const value = valueReactor.get() as GetValuesInArray<T>[number];
|
|
85
|
+
result.set(value)
|
|
86
|
+
})
|
|
87
|
+
effects.push(e)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return result
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface WithLatestFromOptions<T, U> {
|
|
94
|
+
target: ValueReactor<T>;
|
|
95
|
+
other: ValueReactor<U>;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Combines two ValueReactors into a Signal that emits the latest values
|
|
99
|
+
* from the source ValueReactor along with the latest value from the other ValueReactor.
|
|
100
|
+
*/
|
|
101
|
+
export const withLatestFrom = <T, U>(
|
|
102
|
+
options: WithLatestFromOptions<T, U>
|
|
103
|
+
): Signal<[T, U]> => {
|
|
104
|
+
const { target, other } = options;
|
|
105
|
+
|
|
106
|
+
const result = signal(castValueInitializer<[T, U]>(), {
|
|
107
|
+
onDispose: () => {
|
|
108
|
+
eTarget.dispose()
|
|
109
|
+
eOther.dispose()
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
let lastOther = other.get()
|
|
114
|
+
const eOther = effect(() => {
|
|
115
|
+
const b = other.get()
|
|
116
|
+
lastOther = b
|
|
117
|
+
})
|
|
118
|
+
const eTarget = effect(() => {
|
|
119
|
+
const a = target.get()
|
|
120
|
+
result.set([a, lastOther])
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
return result
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface ZipOptions<VA, VB> {
|
|
127
|
+
a: ValueReactor<VA>;
|
|
128
|
+
b: ValueReactor<VB>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Zips two ValueReactors into a Signal that emits pairs of values.
|
|
132
|
+
* Each time both ValueReactors have new values, a new pair is emitted.
|
|
133
|
+
*/
|
|
134
|
+
export const zip = <VA, VB>(
|
|
135
|
+
options: ZipOptions<VA, VB>
|
|
136
|
+
): Signal<[VA, VB]> => {
|
|
137
|
+
const valueReactorA = options.a;
|
|
138
|
+
const valueReactorB = options.b;
|
|
139
|
+
|
|
140
|
+
let lastA = valueReactorA.getWithoutTrack()
|
|
141
|
+
let lastB = valueReactorB.getWithoutTrack()
|
|
142
|
+
|
|
143
|
+
const result = signal<[VA, VB]>(() => [lastA, lastB], {
|
|
144
|
+
onDispose: () => {
|
|
145
|
+
e.dispose()
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
const queueA: VA[] = []
|
|
150
|
+
const queueB: VB[] = []
|
|
151
|
+
|
|
152
|
+
const e = effect(() => {
|
|
153
|
+
const a = valueReactorA.get()
|
|
154
|
+
const b = valueReactorB.get()
|
|
155
|
+
|
|
156
|
+
if (a !== lastA) {
|
|
157
|
+
queueA.push(a)
|
|
158
|
+
lastA = a
|
|
159
|
+
}
|
|
160
|
+
if (b !== lastB) {
|
|
161
|
+
queueB.push(b)
|
|
162
|
+
lastB = b
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (queueA.length !== 0 && queueB.length !== 0) {
|
|
166
|
+
const va = queueA.shift()
|
|
167
|
+
const vb = queueB.shift()
|
|
168
|
+
// oxlint-disable-next-line no-unsafe-type-assertion
|
|
169
|
+
result.set([va, vb] as [VA, VB])
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
return result
|
|
174
|
+
}
|