@@ -179,47 +179,47 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
179
179
}
180
180
181
181
/**
182
- * Renders only when deferred promise is pending ( not yet run).
182
+ * Renders only when deferred promise is waiting (promise has not yet run).
183
183
*
184
184
* @prop {Function|Node } children Function (passing state) or React node
185
- * @prop {boolean } persist Show until we have data, even while loading or when an error occurred
185
+ * @prop {boolean } persist Show until we have data, even while pending ( loading) or when an error occurred
186
186
*/
187
- const Pending = ( { children, persist } ) => (
187
+ const Waiting = ( { children, persist } ) => (
188
188
< Consumer >
189
189
{ state => {
190
190
if ( state . data !== undefined ) return null
191
- if ( ! persist && state . isLoading ) return null
191
+ if ( ! persist && state . isPending ) return null
192
192
if ( ! persist && state . error !== undefined ) return null
193
193
return isFunction ( children ) ? children ( state ) : children || null
194
194
} }
195
195
</ Consumer >
196
196
)
197
197
198
198
if ( PropTypes ) {
199
- Pending . propTypes = {
199
+ Waiting . propTypes = {
200
200
children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) . isRequired ,
201
201
persist : PropTypes . bool ,
202
202
}
203
203
}
204
204
205
205
/**
206
- * Renders only while loading.
206
+ * Renders only while pending (promise is loading) .
207
207
*
208
208
* @prop {Function|Node } children Function (passing state) or React node
209
209
* @prop {boolean } initial Show only on initial load (data is undefined)
210
210
*/
211
- const Loading = ( { children, initial } ) => (
211
+ const Pending = ( { children, initial } ) => (
212
212
< Consumer >
213
213
{ state => {
214
- if ( ! state . isLoading ) return null
214
+ if ( ! state . isPending ) return null
215
215
if ( initial && state . data !== undefined ) return null
216
216
return isFunction ( children ) ? children ( state ) : children || null
217
217
} }
218
218
</ Consumer >
219
219
)
220
220
221
221
if ( PropTypes ) {
222
- Loading . propTypes = {
222
+ Pending . propTypes = {
223
223
children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) . isRequired ,
224
224
initial : PropTypes . bool ,
225
225
}
@@ -229,21 +229,21 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
229
229
* Renders only when promise is resolved.
230
230
*
231
231
* @prop {Function|Node } children Function (passing data and state) or React node
232
- * @prop {boolean } persist Show old data while loading
232
+ * @prop {boolean } persist Show old data while pending (promise is loading)
233
233
*/
234
- const Resolved = ( { children, persist } ) => (
234
+ const Fulfilled = ( { children, persist } ) => (
235
235
< Consumer >
236
236
{ state => {
237
237
if ( state . data === undefined ) return null
238
- if ( ! persist && state . isLoading ) return null
238
+ if ( ! persist && state . isPending ) return null
239
239
if ( ! persist && state . error !== undefined ) return null
240
240
return isFunction ( children ) ? children ( state . data , state ) : children || null
241
241
} }
242
242
</ Consumer >
243
243
)
244
244
245
245
if ( PropTypes ) {
246
- Resolved . propTypes = {
246
+ Fulfilled . propTypes = {
247
247
children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) . isRequired ,
248
248
persist : PropTypes . bool ,
249
249
}
@@ -253,13 +253,13 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
253
253
* Renders only when promise is rejected.
254
254
*
255
255
* @prop {Function|Node } children Function (passing error and state) or React node
256
- * @prop {boolean } persist Show old error while loading
256
+ * @prop {boolean } persist Show old error while pending (promise is loading)
257
257
*/
258
258
const Rejected = ( { children, persist } ) => (
259
259
< Consumer >
260
260
{ state => {
261
261
if ( state . error === undefined ) return null
262
- if ( state . isLoading && ! persist ) return null
262
+ if ( state . isPending && ! persist ) return null
263
263
return isFunction ( children ) ? children ( state . error , state ) : children || null
264
264
} }
265
265
</ Consumer >
@@ -272,16 +272,43 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
272
272
}
273
273
}
274
274
275
- Async . Pending = Pending
276
- Async . Loading = Loading
277
- Async . Resolved = Resolved
278
- Async . Rejected = Rejected
275
+ /**
276
+ * Renders only when promise is fulfilled or rejected.
277
+ *
278
+ * @prop {Function|Node } children Function (passing state) or React node
279
+ * @prop {boolean } persist Show old data or error while pending (promise is loading)
280
+ */
281
+ const Settled = ( { children, persist } ) => (
282
+ < Consumer >
283
+ { state => {
284
+ if ( state . isWaiting ) return null
285
+ if ( state . isPending && ! persist ) return null
286
+ return isFunction ( children ) ? children ( state ) : children || null
287
+ } }
288
+ </ Consumer >
289
+ )
290
+
291
+ if ( PropTypes ) {
292
+ Settled . propTypes = {
293
+ children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) . isRequired ,
294
+ persist : PropTypes . bool ,
295
+ }
296
+ }
297
+
298
+ Waiting . displayName = `${ displayName } .Waiting`
299
+ Pending . displayName = `${ displayName } .Pending`
300
+ Fulfilled . displayName = `${ displayName } .Fulfilled`
301
+ Rejected . displayName = `${ displayName } .Rejected`
302
+ Settled . displayName = `${ displayName } .Settled`
279
303
280
304
Async . displayName = displayName
281
- Async . Pending . displayName = `${ displayName } .Pending`
282
- Async . Loading . displayName = `${ displayName } .Loading`
283
- Async . Resolved . displayName = `${ displayName } .Resolved`
284
- Async . Rejected . displayName = `${ displayName } .Rejected`
305
+ Async . Waiting = Waiting
306
+ Async . Pending = Pending
307
+ Async . Loading = Pending // alias
308
+ Async . Fulfilled = Fulfilled
309
+ Async . Resolved = Fulfilled // alias
310
+ Async . Rejected = Rejected
311
+ Async . Settled = Settled
285
312
286
313
return Async
287
314
}
0 commit comments