@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,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
|
+
}
|