@@ -36,8 +36,6 @@ export { useAsyncIterState, type AsyncIterStateResult, type AsyncIterableSubject
36
36
*
37
37
* ---
38
38
*
39
- *
40
- *
41
39
* The returned async iterable can be passed over to any level down the component tree and rendered
42
40
* using `<Iterate>`, `useAsyncIter`, and so on. It also contains a `.current.value` property which shows
43
41
* 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
88
86
* ---
89
87
*
90
88
* @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`.
91
92
*
92
93
* @returns a stateful async iterable and a function with which to yield an update, both maintain stable references across re-renders.
93
94
*
94
95
* @see {@link Iterate `<Iterate>` }
95
96
*/
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 > {
97
110
const ref = useRef < {
98
- channel : IterableChannel < TVal > ;
99
- result : AsyncIterStateResult < TVal > ;
111
+ channel : IterableChannel < TVal , TInitVal > ;
112
+ result : AsyncIterStateResult < TVal , TInitVal > ;
100
113
} > ( ) ;
101
114
102
115
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
+
104
121
return {
105
122
channel,
106
123
result : [ channel . values , newVal => channel . put ( newVal ) ] ,
@@ -123,7 +140,7 @@ function useAsyncIterState<TVal>(): AsyncIterStateResult<TVal> {
123
140
*
124
141
* @see {@link useAsyncIterState `useAsyncIterState` }
125
142
*/
126
- type AsyncIterStateResult < TVal > = [
143
+ type AsyncIterStateResult < TVal , TInitVal > = [
127
144
/**
128
145
* A stateful async iterable which yields every updated value following a state update.
129
146
*
@@ -133,11 +150,11 @@ type AsyncIterStateResult<TVal> = [
133
150
* meaning multiple iterators can be consumed (iterated) simultaneously, each one picking up the
134
151
* same values as others the moment they were generated through state updates.
135
152
*/
136
- values : AsyncIterableSubject < TVal > ,
153
+ values : AsyncIterableSubject < TVal , TInitVal > ,
137
154
138
155
/**
139
156
* A function which updates the state, causing the paired async iterable to yield the updated state
140
157
* value and immediately sets its `.current.value` property to the latest state.
141
158
*/
142
- setValue : ( update : TVal | ( ( prevState : TVal | undefined ) => TVal ) ) => void ,
159
+ setValue : ( update : TVal | ( ( prevState : TVal | TInitVal ) => TVal ) ) => void ,
143
160
] ;
0 commit comments