-
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(async): add support for async expressions and modify expression …
…interpreter definition api Previously the expression interpreter structure allowed only for evaluation of synchronous expressions. This commit modifies the way interpreters are prepared in order to support both synchronous and asynchronous execution. BREAKING CHANGE: remove interpreter method, add syncInterpreter, syncInterpreterList, asyncInterpreter and asyncInterpreterList methods
- Loading branch information
Showing
29 changed files
with
700 additions
and
446 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
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,82 @@ | ||
import { testCases, asyncResult } from '@orioro/jest-util' | ||
// import { validateType } from '@orioro/typing' | ||
|
||
import { ALL_EXPRESSIONS } from './' | ||
|
||
import { asyncInterpreterList } from './interpreter' | ||
|
||
import { evaluate } from './evaluate' | ||
|
||
const wait = (ms, result) => | ||
new Promise((resolve) => setTimeout(resolve.bind(null, result), ms)) | ||
|
||
const $asyncLoadStr = [() => wait(100, 'async-str'), []] | ||
const $asyncLoadNum = [() => wait(100, 9), []] | ||
const $asyncLoadArr = [() => wait(100, ['str-1', 'str-2', 'str-3']), []] | ||
const $asyncLoadObj = [ | ||
() => | ||
wait(100, { | ||
key1: 'value1', | ||
key2: 'value2', | ||
}), | ||
[], | ||
] | ||
const $asyncLoadTrue = [() => wait(100, true), []] | ||
const $asyncLoadFalse = [() => wait(100, false), []] | ||
|
||
const interpreters = asyncInterpreterList({ | ||
...ALL_EXPRESSIONS, | ||
$asyncLoadStr, | ||
$asyncLoadNum, | ||
$asyncLoadArr, | ||
$asyncLoadObj, | ||
$asyncLoadTrue, | ||
$asyncLoadFalse, | ||
}) | ||
|
||
describe('async - immediate async expression', () => { | ||
testCases( | ||
[ | ||
['$asyncLoadStr', asyncResult('async-str')], | ||
['$asyncLoadNum', asyncResult(9)], | ||
['$asyncLoadArr', asyncResult(['str-1', 'str-2', 'str-3'])], | ||
['$asyncLoadObj', asyncResult({ key1: 'value1', key2: 'value2' })], | ||
['$asyncLoadTrue', asyncResult(true)], | ||
['$asyncLoadFalse', asyncResult(false)], | ||
], | ||
(expression) => | ||
evaluate({ interpreters, scope: { $$VALUE: null } }, [expression]) | ||
) | ||
}) | ||
|
||
describe('async - nested async expression', () => { | ||
test('simple scenario - string concat', () => { | ||
return expect( | ||
evaluate( | ||
{ | ||
interpreters, | ||
scope: { | ||
$$VALUE: 'value-', | ||
}, | ||
}, | ||
['$stringConcat', ['$asyncLoadStr']] | ||
) | ||
).resolves.toEqual('value-async-str') | ||
}) | ||
}) | ||
|
||
describe('async - syncronous expressions only get converted to async as well', () => { | ||
test('simple scenario - string concat', () => { | ||
return expect( | ||
evaluate( | ||
{ | ||
interpreters, | ||
scope: { | ||
$$VALUE: 'value-', | ||
}, | ||
}, | ||
['$stringConcat', 'sync-value'] | ||
) | ||
).resolves.toEqual('value-sync-value') | ||
}) | ||
}) |
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
Oops, something went wrong.