@@ -70,6 +70,12 @@ var TimeoutError = /** @class */ (function (_super) {
70
70
}
71
71
return TimeoutError ;
72
72
} ( Error ) ) ;
73
+ function resolveAfter ( ms ) {
74
+ return new Promise ( function ( resolve ) {
75
+ setTimeout ( resolve , ms ) ;
76
+ } ) ;
77
+ }
78
+ var hasWarnedDeprecatedWait = false ;
73
79
function asyncUtils ( addResolver ) {
74
80
var nextUpdatePromise ;
75
81
function waitForNextUpdate ( options ) {
@@ -102,39 +108,45 @@ function asyncUtils(addResolver) {
102
108
} ) ;
103
109
} ) ;
104
110
}
105
- function wait ( callback , options ) {
106
- if ( options === void 0 ) { options = { timeout : 0 , suppressErrors : true } ; }
111
+ function waitFor ( callback , _a ) {
112
+ var _b = _a === void 0 ? { } : _a , interval = _b . interval , timeout = _b . timeout , _c = _b . suppressErrors , suppressErrors = _c === void 0 ? true : _c ;
107
113
return __awaiter ( this , void 0 , void 0 , function ( ) {
108
114
var checkResult , waitForResult ;
109
115
var _this = this ;
110
- return __generator ( this , function ( _a ) {
111
- switch ( _a . label ) {
116
+ return __generator ( this , function ( _d ) {
117
+ switch ( _d . label ) {
112
118
case 0 :
113
119
checkResult = function ( ) {
114
120
try {
115
121
var callbackResult = callback ( ) ;
116
122
return callbackResult || callbackResult === undefined ;
117
123
}
118
124
catch ( err ) {
119
- if ( ! options . suppressErrors ) {
125
+ if ( ! suppressErrors ) {
120
126
throw err ;
121
127
}
122
128
}
123
129
} ;
124
130
waitForResult = function ( ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
125
- var initialTimeout , startTime , err_1 ;
131
+ var initialTimeout , startTime , nextCheck , err_1 ;
126
132
return __generator ( this , function ( _a ) {
127
133
switch ( _a . label ) {
128
134
case 0 :
129
- initialTimeout = options . timeout ;
135
+ initialTimeout = timeout ;
130
136
_a . label = 1 ;
131
137
case 1 :
132
138
if ( ! true ) return [ 3 /*break*/ , 6 ] ;
133
139
startTime = Date . now ( ) ;
134
140
_a . label = 2 ;
135
141
case 2 :
136
142
_a . trys . push ( [ 2 , 4 , , 5 ] ) ;
137
- return [ 4 /*yield*/ , waitForNextUpdate ( { timeout : options . timeout } ) ] ;
143
+ nextCheck = interval
144
+ ? Promise . race ( [
145
+ waitForNextUpdate ( { timeout : timeout } ) ,
146
+ resolveAfter ( interval ) ,
147
+ ] )
148
+ : waitForNextUpdate ( { timeout : timeout } ) ;
149
+ return [ 4 /*yield*/ , nextCheck ] ;
138
150
case 3 :
139
151
_a . sent ( ) ;
140
152
if ( checkResult ( ) ) {
@@ -144,11 +156,11 @@ function asyncUtils(addResolver) {
144
156
case 4 :
145
157
err_1 = _a . sent ( ) ;
146
158
if ( err_1 . timeout ) {
147
- throw new TimeoutError ( "wait " , { timeout : initialTimeout } ) ;
159
+ throw new TimeoutError ( "waitFor " , { timeout : initialTimeout } ) ;
148
160
}
149
161
throw err_1 ;
150
162
case 5 :
151
- options . timeout -= Date . now ( ) - startTime ;
163
+ timeout -= Date . now ( ) - startTime ;
152
164
return [ 3 /*break*/ , 1 ] ;
153
165
case 6 : return [ 2 /*return*/ ] ;
154
166
}
@@ -157,8 +169,8 @@ function asyncUtils(addResolver) {
157
169
if ( ! ! checkResult ( ) ) return [ 3 /*break*/ , 2 ] ;
158
170
return [ 4 /*yield*/ , waitForResult ( ) ] ;
159
171
case 1 :
160
- _a . sent ( ) ;
161
- _a . label = 2 ;
172
+ _d . sent ( ) ;
173
+ _d . label = 2 ;
162
174
case 2 : return [ 2 /*return*/ ] ;
163
175
}
164
176
} ) ;
@@ -175,7 +187,7 @@ function asyncUtils(addResolver) {
175
187
_a . label = 1 ;
176
188
case 1 :
177
189
_a . trys . push ( [ 1 , 3 , , 4 ] ) ;
178
- return [ 4 /*yield*/ , wait ( function ( ) { return selector ( ) !== initialValue ; } , __assign ( { suppressErrors : false } , options ) ) ] ;
190
+ return [ 4 /*yield*/ , waitFor ( function ( ) { return selector ( ) !== initialValue ; } , __assign ( { suppressErrors : false } , options ) ) ] ;
179
191
case 2 :
180
192
_a . sent ( ) ;
181
193
return [ 3 /*break*/ , 4 ] ;
@@ -190,104 +202,40 @@ function asyncUtils(addResolver) {
190
202
} ) ;
191
203
} ) ;
192
204
}
205
+ function wait ( callback , options ) {
206
+ if ( options === void 0 ) { options = { timeout : 0 , suppressErrors : true } ; }
207
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
208
+ var err_3 ;
209
+ return __generator ( this , function ( _a ) {
210
+ switch ( _a . label ) {
211
+ case 0 :
212
+ if ( ! hasWarnedDeprecatedWait ) {
213
+ hasWarnedDeprecatedWait = true ;
214
+ console . warn ( "`wait` has been deprecated. Use `waitFor` instead: https://react-hooks-testing-library.com/reference/api#waitfor." ) ;
215
+ }
216
+ _a . label = 1 ;
217
+ case 1 :
218
+ _a . trys . push ( [ 1 , 3 , , 4 ] ) ;
219
+ return [ 4 /*yield*/ , waitFor ( callback , options ) ] ;
220
+ case 2 :
221
+ _a . sent ( ) ;
222
+ return [ 3 /*break*/ , 4 ] ;
223
+ case 3 :
224
+ err_3 = _a . sent ( ) ;
225
+ if ( err_3 . timeout ) {
226
+ throw new TimeoutError ( "wait" , { timeout : options . timeout } ) ;
227
+ }
228
+ throw err_3 ;
229
+ case 4 : return [ 2 /*return*/ ] ;
230
+ }
231
+ } ) ;
232
+ } ) ;
233
+ }
193
234
return {
194
- waitForValueToChange : waitForValueToChange ,
195
- waitForNextUpdate : waitForNextUpdate ,
196
235
wait : wait ,
236
+ waitFor : waitFor ,
237
+ waitForNextUpdate : waitForNextUpdate ,
238
+ waitForValueToChange : waitForValueToChange ,
197
239
} ;
198
240
}
199
241
exports . default = asyncUtils ;
200
- // function asyncUtils(addResolver: (r: ResolverType) => void) {
201
- // let nextUpdatePromise: Promise<any> | null = null;
202
- // const waitForNextUpdate = async (
203
- // options: TimeoutOptions = { timeout: 0 }
204
- // ) => {
205
- // if (!nextUpdatePromise) {
206
- // nextUpdatePromise = new Promise((resolve, reject) => {
207
- // let timeoutId: NodeJS.Timeout;
208
- // if (options.timeout && options.timeout > 0) {
209
- // timeoutId = setTimeout(
210
- // () => reject(new TimeoutError("waitForNextUpdate", options)),
211
- // options.timeout
212
- // );
213
- // }
214
- // addResolver(() => {
215
- // clearTimeout(timeoutId);
216
- // nextUpdatePromise = null;
217
- // resolve();
218
- // });
219
- // });
220
- // await act(() => {
221
- // if (nextUpdatePromise) {
222
- // return nextUpdatePromise;
223
- // }
224
- // return;
225
- // });
226
- // }
227
- // await nextUpdatePromise;
228
- // };
229
- // const wait = async (
230
- // callback: () => any,
231
- // { timeout, suppressErrors }: WaitOptions = {
232
- // timeout: 0,
233
- // suppressErrors: true,
234
- // }
235
- // ) => {
236
- // const checkResult = () => {
237
- // try {
238
- // const callbackResult = callback();
239
- // return callbackResult || callbackResult === undefined;
240
- // } catch (e) {
241
- // if (!suppressErrors) {
242
- // throw e;
243
- // }
244
- // }
245
- // };
246
- // const waitForResult = async () => {
247
- // const initialTimeout = timeout;
248
- // while (true) {
249
- // const startTime = Date.now();
250
- // try {
251
- // await waitForNextUpdate({ timeout });
252
- // if (checkResult()) {
253
- // return;
254
- // }
255
- // } catch (e) {
256
- // if (e.timeout) {
257
- // throw new TimeoutError("wait", { timeout: initialTimeout });
258
- // }
259
- // throw e;
260
- // }
261
- // timeout -= Date.now() - startTime;
262
- // }
263
- // };
264
- // if (!checkResult()) {
265
- // await waitForResult();
266
- // }
267
- // };
268
- // const waitForValueToChange = async (
269
- // selector: () => any,
270
- // options: TimeoutOptions = {
271
- // timeout: 0,
272
- // }
273
- // ) => {
274
- // const initialValue = selector();
275
- // try {
276
- // await wait(() => selector() !== initialValue, {
277
- // suppressErrors: false,
278
- // ...options,
279
- // });
280
- // } catch (e) {
281
- // if (e.timeout) {
282
- // throw new TimeoutError("waitForValueToChange", options);
283
- // }
284
- // throw e;
285
- // }
286
- // };
287
- // return {
288
- // wait,
289
- // waitForNextUpdate,
290
- // waitForValueToChange,
291
- // };
292
- // }
293
- // export default asyncUtils;
0 commit comments