3
3
type Executor ,
4
4
type Scope ,
5
5
type ObservableGet ,
6
- provideObservable ,
7
6
combineObservables ,
8
- combine ,
9
7
} from "@submodule/core" ;
10
8
11
9
import React , {
@@ -83,8 +81,9 @@ let cache = [] as CacheEntry[];
83
81
/**
84
82
* Core functionality of submodule. Resolves an executor within the current scope.
85
83
*
86
- * @param executor
87
- * @returns
84
+ * @param executor - The executor to resolve within the current scope.
85
+ * @returns The resolved value of the executor.
86
+ * @throws {Promise } If the executor is not yet resolved, a promise is thrown to suspend the component.
88
87
*/
89
88
export function useResolve < T > ( executor : Executor < T > ) : T {
90
89
const scope = useContext ( scopeContext ) ;
@@ -122,33 +121,33 @@ export function useResolve<T>(executor: Executor<T>): T {
122
121
throw cacheEntry [ 2 ] . promise ;
123
122
}
124
123
125
- export function useObservable < P , V > (
124
+ /**
125
+ * Subscribes to an observable and returns its current value.
126
+ * Optionally transforms the value using the provided transform function.
127
+ *
128
+ * @param executor - The executor to resolve the observable.
129
+ * @param transform - Optional transform function to apply to the observable value.
130
+ * @param params - Additional parameters to pass to the transform function.
131
+ * @returns The current value of the observable, optionally transformed.
132
+ */
133
+ export function useObservable < P , V , Params extends Array < unknown > > (
126
134
executor : Executor < ObservableGet < P > > ,
127
- transform : ( value : P ) => V ,
135
+ transform : ( value : P , ...params : Params ) => V ,
136
+ ...params : Params
128
137
) : V ;
129
138
130
139
export function useObservable < P > ( executor : Executor < ObservableGet < P > > ) : P ;
131
140
132
- /**
133
- * Subscribes to an Observable and returns its current value.
134
- * Uses Suspense for loading states.
135
- *
136
- * @template P The payload type
137
- * @template API The controller API type
138
- * @param executor The Observable instance
139
- * @returns The current value of the Observable
140
- * @throws {Promise } During initial load (for Suspense)
141
- * @throws {Error } If used outside of a ScopeProvider
142
- */
143
- export function useObservable < P , V > (
141
+ export function useObservable < P , V , Params extends Array < unknown > > (
144
142
executor : Executor < ObservableGet < P > > ,
145
- transform ?: ( value : P ) => V ,
143
+ transform ?: ( nextvalue : P , ...params : Params ) => V ,
144
+ ...params : Params
146
145
) : P | V {
147
146
const observable = useResolve ( executor ) ;
148
147
149
148
const subs = useCallback (
150
149
( cb : ( ) => void ) => {
151
- return observable . onValue ( ( next ) => {
150
+ return observable . onValue ( ( ) => {
152
151
queueMicrotask ( cb ) ;
153
152
} ) ;
154
153
} ,
@@ -157,11 +156,38 @@ export function useObservable<P, V>(
157
156
158
157
return useSyncExternalStore (
159
158
subs ,
160
- ( ) => ( transform ? transform ( observable . value ) : observable . value ) ,
161
- ( ) => ( transform ? transform ( observable . value ) : observable . value ) ,
159
+ ( ) =>
160
+ transform ? transform ( observable . value , ...params ) : observable . value ,
161
+ ( ) =>
162
+ transform ? transform ( observable . value , ...params ) : observable . value ,
162
163
) ;
163
164
}
164
165
166
+ /**
167
+ * An utility hook to wrap a transform function inside a useCallback. Supposed to use with useObservable.
168
+ * @param transform Specifies the transform function to use
169
+ * @param params Additional params can be added
170
+ * @returns function that wrapped inside useCallback
171
+ */
172
+ export function useTransformFn < P , V , Params extends Array < unknown > > (
173
+ transform : ( nextValue : P , ...params : Params ) => V ,
174
+ ...params : Params
175
+ ) : ( value : P , ...params : Params ) => V {
176
+ return useCallback (
177
+ ( value : P , ...params : Params ) => transform ( value , ...params ) ,
178
+ [ transform , ...params ] ,
179
+ ) ;
180
+ }
181
+
182
+ /**
183
+ * Combines multiple observables into a single value.
184
+ * Optionally transforms the combined value using the provided transform function.
185
+ *
186
+ * @param upstreams - An object containing the executors for the observables to combine.
187
+ * @param transform - Optional transform function to apply to the combined value.
188
+ * @param initialValue - Optional initial value for the combined value.
189
+ * @returns The combined value of the observables, optionally transformed.
190
+ */
165
191
export function useCombines < Upstreams extends Record < string , unknown > , Value > (
166
192
upstreams : {
167
193
[ K in keyof Upstreams ] : Executor < ObservableGet < Upstreams [ K ] > > ;
0 commit comments