@@ -33,6 +33,23 @@ function isDeterministic (method, engine, buildState) {
33
33
return true
34
34
}
35
35
36
+ function isSyncDeep ( method , engine , buildState ) {
37
+ if ( Array . isArray ( method ) ) {
38
+ return method . every ( ( i ) => isSyncDeep ( i , engine , buildState ) )
39
+ }
40
+
41
+ if ( method && typeof method === 'object' ) {
42
+ const func = Object . keys ( method ) [ 0 ]
43
+ const lower = method [ func ]
44
+ if ( engine . isData ( method , func ) ) return true
45
+ if ( ! engine . methods [ func ] ) throw new Error ( `Method '${ func } ' was not found in the Logic Engine.` )
46
+ if ( engine . methods [ func ] . traverse === false ) return typeof engine . methods [ func ] [ Sync ] === 'function' ? engine . methods [ func ] [ Sync ] ( lower , buildState ) : engine . methods [ func ] [ Sync ]
47
+ return typeof engine . methods [ func ] [ Sync ] === 'function' ? engine . methods [ func ] [ Sync ] ( lower , buildState ) : engine . methods [ func ] [ Sync ] && isSyncDeep ( lower , engine , buildState )
48
+ }
49
+
50
+ return true
51
+ }
52
+
36
53
const defaultMethods = {
37
54
'+' : ( data ) => {
38
55
if ( typeof data === 'string' ) return + data
@@ -77,7 +94,7 @@ const defaultMethods = {
77
94
method : ( input , context , above , engine ) => {
78
95
if ( ! Array . isArray ( input ) ) throw new InvalidControlInput ( input )
79
96
80
- if ( input . length === 1 ) return engine . run ( input [ 0 ] , context , { above } )
97
+ if ( input . length === 1 ) return ( engine . fallback || engine ) . run ( input [ 0 ] , context , { above } )
81
98
if ( input . length < 2 ) return null
82
99
83
100
input = [ ...input ]
@@ -91,14 +108,15 @@ const defaultMethods = {
91
108
const check = input . shift ( )
92
109
const onTrue = input . shift ( )
93
110
94
- const test = engine . run ( check , context , { above } )
111
+ const test = ( engine . fallback || engine ) . run ( check , context , { above } )
95
112
96
113
// if the condition is true, run the true branch
97
- if ( engine . truthy ( test ) ) return engine . run ( onTrue , context , { above } )
114
+ if ( engine . truthy ( test ) ) return ( engine . fallback || engine ) . run ( onTrue , context , { above } )
98
115
}
99
116
100
- return engine . run ( onFalse , context , { above } )
117
+ return ( engine . fallback || engine ) . run ( onFalse , context , { above } )
101
118
} ,
119
+ [ Sync ] : ( data , buildState ) => isSyncDeep ( data , buildState . engine , buildState ) ,
102
120
deterministic : ( data , buildState ) => {
103
121
return isDeterministic ( data , buildState . engine , buildState )
104
122
} ,
@@ -316,15 +334,15 @@ const defaultMethods = {
316
334
method : ( input , context , above , engine ) => {
317
335
if ( ! Array . isArray ( input ) ) throw new InvalidControlInput ( input )
318
336
let [ selector , mapper , defaultValue ] = input
319
- defaultValue = engine . run ( defaultValue , context , {
337
+ defaultValue = ( engine . fallback || engine ) . run ( defaultValue , context , {
320
338
above
321
339
} )
322
340
selector =
323
- engine . run ( selector , context , {
341
+ ( engine . fallback || engine ) . run ( selector , context , {
324
342
above
325
343
} ) || [ ]
326
344
const func = ( accumulator , current ) => {
327
- return engine . run (
345
+ return ( engine . fallback || engine ) . run (
328
346
mapper ,
329
347
{
330
348
accumulator,
@@ -340,6 +358,7 @@ const defaultMethods = {
340
358
}
341
359
return selector . reduce ( func , defaultValue )
342
360
} ,
361
+ [ Sync ] : ( data , buildState ) => isSyncDeep ( data , buildState . engine , buildState ) ,
343
362
asyncMethod : async ( input , context , above , engine ) => {
344
363
if ( ! Array . isArray ( input ) ) throw new InvalidControlInput ( input )
345
364
let [ selector , mapper , defaultValue ] = input
@@ -385,7 +404,7 @@ const defaultMethods = {
385
404
const item = object [ key ]
386
405
Object . defineProperty ( accumulator , key , {
387
406
enumerable : true ,
388
- value : engine . run ( item , context , { above } )
407
+ value : ( engine . fallback || engine ) . run ( item , context , { above } )
389
408
} )
390
409
return accumulator
391
410
} , { } )
@@ -445,16 +464,17 @@ function createArrayIterativeMethod (name, useTruthy = false) {
445
464
} )
446
465
)
447
466
} ,
467
+ [ Sync ] : ( data , buildState ) => isSyncDeep ( data , buildState . engine , buildState ) ,
448
468
method : ( input , context , above , engine ) => {
449
469
if ( ! Array . isArray ( input ) ) throw new InvalidControlInput ( input )
450
470
let [ selector , mapper ] = input
451
471
selector =
452
- engine . run ( selector , context , {
472
+ ( engine . fallback || engine ) . run ( selector , context , {
453
473
above
454
474
} ) || [ ]
455
475
456
476
return selector [ name ] ( ( i , index ) => {
457
- const result = engine . run ( mapper , i , {
477
+ const result = ( engine . fallback || engine ) . run ( mapper , i , {
458
478
above : [ { item : selector , index } , context , ...above ]
459
479
} )
460
480
return useTruthy ? engine . truthy ( result ) : result
0 commit comments