@@ -36,8 +36,6 @@ export { useAsyncIterState, type AsyncIterStateResult, type AsyncIterableSubject
3636 *
3737 * ---
3838 *
39- *
40- *
4139 * The returned async iterable can be passed over to any level down the component tree and rendered
4240 * using `<Iterate>`, `useAsyncIter`, and so on. It also contains a `.current.value` property which shows
4341 * the current up to date state value at all times. Use this any case you just need to read the immediate
@@ -88,19 +86,38 @@ export { useAsyncIterState, type AsyncIterStateResult, type AsyncIterableSubject
8886 * ---
8987 *
9088 * @template TVal the type of state to be set and yielded by returned iterable.
89+ * @template TInitVal The type of the starting value for the state iterable's `.current.value` property.
90+ *
91+ * @param initialValue Any optional starting value for the state iterable's `.current.value` property, defaults to `undefined`.
9192 *
9293 * @returns a stateful async iterable and a function with which to yield an update, both maintain stable references across re-renders.
9394 *
9495 * @see {@link Iterate `<Iterate>` }
9596 */
96- function useAsyncIterState < TVal > ( ) : AsyncIterStateResult < TVal > {
97+ function useAsyncIterState < TVal > ( ) : AsyncIterStateResult < TVal , undefined > ;
98+
99+ function useAsyncIterState < TVal > (
100+ initialValue : TVal | ( ( ) => TVal )
101+ ) : AsyncIterStateResult < TVal , TVal > ;
102+
103+ function useAsyncIterState < TVal , TInitVal = undefined > (
104+ initialValue : TInitVal | ( ( ) => TInitVal )
105+ ) : AsyncIterStateResult < TVal , TInitVal > ;
106+
107+ function useAsyncIterState < TVal , TInitVal > (
108+ initialValue ?: TInitVal | ( ( ) => TInitVal )
109+ ) : AsyncIterStateResult < TVal , TInitVal > {
97110 const ref = useRef < {
98- channel : IterableChannel < TVal > ;
99- result : AsyncIterStateResult < TVal > ;
111+ channel : IterableChannel < TVal , TInitVal > ;
112+ result : AsyncIterStateResult < TVal , TInitVal > ;
100113 } > ( ) ;
101114
102115 ref . current ??= ( ( ) => {
103- const channel = new IterableChannel < TVal > ( ) ;
116+ const initialValueDetermined =
117+ typeof initialValue !== 'function' ? initialValue : ( initialValue as ( ) => TInitVal ) ( ) ;
118+
119+ const channel = new IterableChannel < TVal , TInitVal > ( initialValueDetermined as TInitVal ) ;
120+
104121 return {
105122 channel,
106123 result : [ channel . values , newVal => channel . put ( newVal ) ] ,
@@ -123,7 +140,7 @@ function useAsyncIterState<TVal>(): AsyncIterStateResult<TVal> {
123140 *
124141 * @see {@link useAsyncIterState `useAsyncIterState` }
125142 */
126- type AsyncIterStateResult < TVal > = [
143+ type AsyncIterStateResult < TVal , TInitVal > = [
127144 /**
128145 * A stateful async iterable which yields every updated value following a state update.
129146 *
@@ -133,11 +150,11 @@ type AsyncIterStateResult<TVal> = [
133150 * meaning multiple iterators can be consumed (iterated) simultaneously, each one picking up the
134151 * same values as others the moment they were generated through state updates.
135152 */
136- values : AsyncIterableSubject < TVal > ,
153+ values : AsyncIterableSubject < TVal , TInitVal > ,
137154
138155 /**
139156 * A function which updates the state, causing the paired async iterable to yield the updated state
140157 * value and immediately sets its `.current.value` property to the latest state.
141158 */
142- setValue : ( update : TVal | ( ( prevState : TVal | undefined ) => TVal ) ) => void ,
159+ setValue : ( update : TVal | ( ( prevState : TVal | TInitVal ) => TVal ) ) => void ,
143160] ;
0 commit comments