-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for async/sync interpreters on same context
This commit refactors the whole codebase to better support sync/async execution. Some important changes: - add support for nested param resolution through @orioro/typing typeSpec utilities - remove function and null ParamResolvers in favor of aforementioned typeSpecs - separate interpreter functions into sub-modules (withtin dir src/interpreter) - fix isExpression method to check whether the interpreterId is actually a string before attempting to get the interpterter function (previous versions were unintentionally using JS type casting which transformed the array `[['$$value']]` into a string `'$$value'`) - modify interpreterList format to support sync/async evaluation on the same context object allowing expression interpreters to evaluate synchronoushly even if they were called in async mode (introduced to support Array.prototype.sort(customComparatorExp)) BREAKING CHANGE: `context.interpreters` format modified
- Loading branch information
Showing
46 changed files
with
2,654 additions
and
2,240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ node_modules | |
dist | ||
coverage | ||
tmp | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
|
||
- $and | Add 'strict' mode option (src/expressions/logical.ts) | ||
- array | $arrayEvery write tests with async conditions (src/expressions/array.ts) | ||
- array | $arraySome write tests with async conditions (src/expressions/array.ts) | ||
- array | Make it possible for the same set of expression interpreters | ||
to be called synchronously or asynchronously. E.g. sort comparator | ||
expression should only support Synchronous (src/expressions/array.ts) | ||
- asyncParamResolver | ONE_OF_TYPES: handle complex cases, e.g. | ||
oneOfTypes(['string', objectType({ key1: 'string', key2: 'number '})]) (src/interpreter/asyncParamResolver.ts) | ||
- logical | Write test to ensure delayed evaluation (src/expressions/logical.ts) | ||
- syncInterpreter | Handle nested object param typeSpec (src/interpreter/syncParamResolver.ts) | ||
- syncInterpreter | Study better ways at validating evlauation results for | ||
tupleType and indefiniteArrayOfType. Currently validation is highly redundant. (src/interpreter/syncParamResolver.ts) | ||
- syncInterpreter | Update Interpreter type: remove function (src/interpreter/syncInterpreter.ts) | ||
- value | Consider adding 'expression' type (src/expressions/value.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { evaluate, evaluateAsync } from '../src/evaluate' | ||
import { interpreterList } from '../src/interpreter/interpreter' | ||
import { | ||
testCases, | ||
asyncResult, | ||
valueLabel, | ||
resultLabel, | ||
variableName, | ||
VariableName, | ||
} from '@orioro/jest-util' | ||
|
||
const _evLabel = ([value, expression], result) => | ||
`${valueLabel(value)} | ${valueLabel(expression)} -> ${resultLabel(result)}` | ||
|
||
export const _prepareEvaluateTestCases = (interpreterSpecs) => { | ||
const interpreters = interpreterList(interpreterSpecs) | ||
|
||
const testSyncCases = (cases) => { | ||
testCases( | ||
cases, | ||
(value, expression) => | ||
evaluate( | ||
{ | ||
interpreters, | ||
scope: { $$VALUE: value }, | ||
}, | ||
expression | ||
), | ||
([value, expression], result) => | ||
`sync - ${_evLabel([value, expression], result)}` | ||
) | ||
} | ||
|
||
const testAsyncCases = cases => { | ||
testCases( | ||
cases.map((_case) => { | ||
const result = _case[_case.length - 1] | ||
const args = _case.slice(0, -1) | ||
|
||
return [...args, asyncResult(result)] | ||
}), | ||
(value, expression) => | ||
evaluateAsync( | ||
{ | ||
interpreters, | ||
scope: { $$VALUE: value }, | ||
}, | ||
expression | ||
), | ||
([value, expression], result) => | ||
`async - ${_evLabel([value, expression], result)}` | ||
) | ||
} | ||
|
||
const testSyncAndAsync = (cases) => { | ||
testSyncCases(cases) | ||
testAsyncCases(cases) | ||
} | ||
|
||
testSyncAndAsync.testSyncCases = testSyncCases | ||
testSyncAndAsync.testAsyncCases = testAsyncCases | ||
|
||
return testSyncAndAsync | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.