1
1
/**
2
- * rubico v2.6.1
2
+ * rubico v2.6.2
3
3
* https://github.com/a-synchronous/rubico
4
4
* (c) 2019-2024 Richard Tong
5
5
* rubico may be freely distributed under the MIT license.
6
6
*/
7
7
8
8
const isPromise = value => value != null && typeof value . then == 'function'
9
9
10
+ const isArray = Array . isArray
11
+
10
12
const areAnyValuesPromises = function ( values ) {
11
- const length = values . length
12
- let index = - 1
13
- while ( ++ index < length ) {
14
- const value = values [ index ]
13
+ if ( isArray ( values ) ) {
14
+ const length = values . length
15
+ let index = - 1
16
+ while ( ++ index < length ) {
17
+ const value = values [ index ]
18
+ if ( isPromise ( value ) ) {
19
+ return true
20
+ }
21
+ }
22
+ return false
23
+ }
24
+
25
+ for ( const key in values ) {
26
+ const value = values [ key ]
15
27
if ( isPromise ( value ) ) {
16
28
return true
17
29
}
18
30
}
19
31
return false
20
32
}
21
33
34
+ const areAllValuesNonfunctions = function ( values ) {
35
+ if ( isArray ( values ) ) {
36
+ const length = values . length
37
+ let index = - 1
38
+ while ( ++ index < length ) {
39
+ if ( typeof values [ index ] == 'function' ) {
40
+ return false
41
+ }
42
+ }
43
+ return true
44
+ }
45
+
46
+ for ( const key in values ) {
47
+ if ( typeof values [ key ] == 'function' ) {
48
+ return false
49
+ }
50
+ }
51
+ return true
52
+ }
53
+
22
54
const promiseAll = Promise . all . bind ( Promise )
23
55
24
- const isArray = Array . isArray
56
+ const promiseObjectAllExecutor = object => function executor ( resolve ) {
57
+ const result = { }
58
+ let numPromises = 0
59
+ for ( const key in object ) {
60
+ const value = object [ key ]
61
+ if ( isPromise ( value ) ) {
62
+ numPromises += 1
63
+ value . then ( ( key => function ( res ) {
64
+ result [ key ] = res
65
+ numPromises -= 1
66
+ if ( numPromises == 0 ) {
67
+ resolve ( result )
68
+ }
69
+ } ) ( key ) )
70
+ } else {
71
+ result [ key ] = value
72
+ }
73
+ }
74
+ if ( numPromises == 0 ) {
75
+ resolve ( result )
76
+ }
77
+ }
78
+
79
+ const promiseObjectAll = object => new Promise ( promiseObjectAllExecutor ( object ) )
25
80
26
81
const __ = Symbol . for ( 'placeholder' )
27
82
@@ -71,7 +126,8 @@ const functionArrayAll = function (funcs, args) {
71
126
result = Array ( funcsLength )
72
127
let funcsIndex = - 1 , isAsync = false
73
128
while ( ++ funcsIndex < funcsLength ) {
74
- const resultItem = funcs [ funcsIndex ] ( ...args )
129
+ const f = funcs [ funcsIndex ]
130
+ const resultItem = typeof f == 'function' ? f ( ...args ) : f
75
131
if ( isPromise ( resultItem ) ) {
76
132
isAsync = true
77
133
}
@@ -195,7 +251,8 @@ const always = value => function getter() { return value }
195
251
const functionObjectAll = function ( funcs , args ) {
196
252
const result = { } , promises = [ ]
197
253
for ( const key in funcs ) {
198
- const resultItem = funcs [ key ] ( ...args )
254
+ const f = funcs [ key ]
255
+ const resultItem = typeof f == 'function' ? f ( ...args ) : f
199
256
if ( isPromise ( resultItem ) ) {
200
257
promises . push ( resultItem . then ( curry3 ( objectSet , result , key , __ ) ) )
201
258
} else {
@@ -205,7 +262,48 @@ const functionObjectAll = function (funcs, args) {
205
262
return promises . length == 0 ? result : promiseAll ( promises ) . then ( always ( result ) )
206
263
}
207
264
265
+ const _allValues = function ( values ) {
266
+ if ( isArray ( values ) ) {
267
+ return areAnyValuesPromises ( values )
268
+ ? promiseAll ( values )
269
+ : values
270
+ }
271
+ return areAnyValuesPromises ( values )
272
+ ? promiseObjectAll ( values )
273
+ : values
274
+ }
275
+
208
276
const all = function ( ...args ) {
277
+ if ( args . length == 1 ) {
278
+ const resolversOrValues = args [ 0 ]
279
+ if ( isPromise ( resolversOrValues ) ) {
280
+ return resolversOrValues . then ( _allValues )
281
+ }
282
+ if ( areAllValuesNonfunctions ( resolversOrValues ) ) {
283
+ return _allValues ( resolversOrValues )
284
+ }
285
+ return isArray ( resolversOrValues )
286
+ ? curryArgs2 ( functionArrayAll , resolversOrValues , __ )
287
+ : curryArgs2 ( functionObjectAll , resolversOrValues , __ )
288
+ }
289
+
290
+ const resolversOrValues = args [ args . length - 1 ]
291
+ const argValues = args . slice ( 0 , - 1 )
292
+
293
+ if ( areAnyValuesPromises ( argValues ) ) {
294
+ return isArray ( resolversOrValues )
295
+ ? promiseAll ( argValues )
296
+ . then ( curry2 ( functionArrayAll , resolversOrValues , __ ) )
297
+ : promiseAll ( argValues )
298
+ . then ( curry2 ( functionObjectAll , resolversOrValues , __ ) )
299
+ }
300
+
301
+ return isArray ( resolversOrValues )
302
+ ? functionArrayAll ( resolversOrValues , argValues )
303
+ : functionObjectAll ( resolversOrValues , argValues )
304
+
305
+ /*
306
+ ////////////////////////////////////////////////////////////////
209
307
const funcs = args.pop()
210
308
if (args.length == 0) {
211
309
return isArray(funcs)
@@ -222,6 +320,7 @@ const all = function (...args) {
222
320
return isArray(funcs)
223
321
? functionArrayAll(funcs, args)
224
322
: functionObjectAll(funcs, args)
323
+ */
225
324
}
226
325
227
326
all . series = function allSeries ( ...args ) {
0 commit comments