Skip to content

Commit e99944c

Browse files
author
Duke
committed
fix: pumped to next version of submodule
1 parent 9004991 commit e99944c

File tree

3 files changed

+50
-24
lines changed

3 files changed

+50
-24
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"react": "^18.0.0 || ^19.0.0"
88
},
99
"peerDependencies": {
10-
"@submodule/core": "^11.2.0",
10+
"@submodule/core": "^11.3.1",
1111
"react": "^18.0.0 || ^19.0.0"
1212
},
1313
"tsup": {
@@ -49,7 +49,7 @@
4949
},
5050
"devDependencies": {
5151
"@testing-library/react": "^16.1.0",
52-
"@submodule/core": "^11.2.0",
52+
"@submodule/core": "^11.3.1",
5353
"tsup": "^8.3.5",
5454
"typescript": "^5.7.2",
5555
"vitest": "^2.1.8"

src/index.tsx

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import {
33
type Executor,
44
type Scope,
55
type ObservableGet,
6-
provideObservable,
76
combineObservables,
8-
combine,
97
} from "@submodule/core";
108

119
import React, {
@@ -83,8 +81,9 @@ let cache = [] as CacheEntry[];
8381
/**
8482
* Core functionality of submodule. Resolves an executor within the current scope.
8583
*
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.
8887
*/
8988
export function useResolve<T>(executor: Executor<T>): T {
9089
const scope = useContext(scopeContext);
@@ -122,33 +121,33 @@ export function useResolve<T>(executor: Executor<T>): T {
122121
throw cacheEntry[2].promise;
123122
}
124123

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>>(
126134
executor: Executor<ObservableGet<P>>,
127-
transform: (value: P) => V,
135+
transform: (value: P, ...params: Params) => V,
136+
...params: Params
128137
): V;
129138

130139
export function useObservable<P>(executor: Executor<ObservableGet<P>>): P;
131140

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>>(
144142
executor: Executor<ObservableGet<P>>,
145-
transform?: (value: P) => V,
143+
transform?: (nextvalue: P, ...params: Params) => V,
144+
...params: Params
146145
): P | V {
147146
const observable = useResolve(executor);
148147

149148
const subs = useCallback(
150149
(cb: () => void) => {
151-
return observable.onValue((next) => {
150+
return observable.onValue(() => {
152151
queueMicrotask(cb);
153152
});
154153
},
@@ -157,11 +156,38 @@ export function useObservable<P, V>(
157156

158157
return useSyncExternalStore(
159158
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,
162163
);
163164
}
164165

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+
*/
165191
export function useCombines<Upstreams extends Record<string, unknown>, Value>(
166192
upstreams: {
167193
[K in keyof Upstreams]: Executor<ObservableGet<Upstreams[K]>>;

0 commit comments

Comments
 (0)