|
1 |
| -import { observables, pipe, pushObservable, type Operator, type PushObservable, type Subscribable } from "./rx" |
| 1 | +import { observables, pipe, pushObservable, type Operator, type PushObservable, type Subscribable, type Subscriber } from "./rx" |
2 | 2 |
|
3 | 3 | declare const ExecutorIdBrand: unique symbol
|
4 | 4 | export type ExecutorId = string & { readonly [ExecutorIdBrand]: never }
|
@@ -842,6 +842,28 @@ export function providePushObservable<Value>(
|
842 | 842 | })
|
843 | 843 | }
|
844 | 844 |
|
| 845 | +/** |
| 846 | + * Utility to integrate state with submodule ecosystem |
| 847 | + * @param pValue - value that will be used as initial state |
| 848 | + * @param controller - api to control the state |
| 849 | + * @returns |
| 850 | + */ |
| 851 | +export function provideState< |
| 852 | + Value, |
| 853 | + Controller, |
| 854 | + ControllerFn extends (sub: Subscriber<Value>, get: () => Value) => Controller |
| 855 | +>( |
| 856 | + pValue: Value | Executor<Value>, |
| 857 | + controller: ControllerFn | Executor<ControllerFn> |
| 858 | +): Executor<readonly [Subscribable<Value>, Controller]> { |
| 859 | + return map( |
| 860 | + { value: normalize(pValue), controller: normalize(controller) }, |
| 861 | + ({ value, controller }) => { |
| 862 | + return observables.createState<Value, Controller>(value, controller) |
| 863 | + } |
| 864 | + ) |
| 865 | +} |
| 866 | + |
845 | 867 | export type OperatorLike<S, A> = Operator<S, A> | Executor<Operator<S, A>>
|
846 | 868 |
|
847 | 869 | /**
|
|
0 commit comments