@@ -126,25 +126,9 @@ export function trackedFunction<Return>(...passedArgs: UseFunctionArgs<Return>)
126
126
}
127
127
128
128
return resource < State < Return > > ( context , ( hooks ) => {
129
- let state = new State ( initialValue ) ;
129
+ let state = new State ( fn , hooks , initialValue ) ;
130
130
131
- ( async ( ) => {
132
- try {
133
- let notQuiteValue = fn ( hooks ) ;
134
- let promise = Promise . resolve ( notQuiteValue ) ;
135
-
136
- waitForPromise ( promise ) ;
137
-
138
- let result = await promise ;
139
-
140
- state . error = undefined ;
141
- state . resolvedValue = result ;
142
- } catch ( e ) {
143
- state . error = e ;
144
- } finally {
145
- state . isResolved = true ;
146
- }
147
- } ) ( ) ;
131
+ state . retry ( ) ;
148
132
149
133
return state ;
150
134
} ) ;
@@ -158,10 +142,18 @@ export class State<Value> {
158
142
@tracked resolvedValue ?: Value ;
159
143
@tracked error ?: unknown ;
160
144
161
- constructor ( public initialValue ?: Value ) { }
145
+ #fn: ResourceFn < Value > ;
146
+ #hooks: Hooks ;
147
+ #initialValue: Value | undefined ;
148
+
149
+ constructor ( fn : ResourceFn < Value > , hooks : Hooks , initialValue ?: Value ) {
150
+ this . #fn = fn ;
151
+ this . #hooks = hooks ;
152
+ this . #initialValue = initialValue ;
153
+ }
162
154
163
155
get value ( ) {
164
- return this . resolvedValue || this . initialValue || null ;
156
+ return this . resolvedValue || this . # initialValue || null ;
165
157
}
166
158
167
159
get isPending ( ) {
@@ -175,6 +167,33 @@ export class State<Value> {
175
167
get isError ( ) {
176
168
return Boolean ( this . error ) ;
177
169
}
170
+
171
+ /**
172
+ * Will re-invoke the function passed to `trackedFunction`
173
+ * this will also re-set some properties on the `State` instance.
174
+ * This is the same `State` instance as before, as the `State` instance
175
+ * is tied to the `fn` passed to `trackedFunction`
176
+ *
177
+ * `error` or `resolvedValue` will remain as they were previously
178
+ * until this promise resolves, and then they'll be updated to the new values.
179
+ */
180
+ retry = async ( ) => {
181
+ try {
182
+ let notQuiteValue = this . #fn( this . #hooks) ;
183
+ let promise = Promise . resolve ( notQuiteValue ) ;
184
+
185
+ waitForPromise ( promise ) ;
186
+
187
+ let result = await promise ;
188
+
189
+ this . error = undefined ;
190
+ this . resolvedValue = result ;
191
+ } catch ( e ) {
192
+ this . error = e ;
193
+ } finally {
194
+ this . isResolved = true ;
195
+ }
196
+ } ;
178
197
}
179
198
180
199
/**
0 commit comments