-
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.
Merge pull request #6 from orioro/async
Async
- Loading branch information
Showing
33 changed files
with
808 additions
and
558 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ node_modules | |
*.bundle.js | ||
dist | ||
coverage | ||
tmp |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { validateType } from '@orioro/typing' | ||
|
||
import { | ||
Expression, | ||
ExpressionInterpreterList, | ||
EvaluationContext, | ||
} from './types' | ||
|
||
/** | ||
* @function isExpression | ||
* @param {ExpressionInterpreterList} | ||
*/ | ||
export const isExpression = ( | ||
interpreters: ExpressionInterpreterList, | ||
candidateExpression: any // eslint-disable-line @typescript-eslint/explicit-module-boundary-types | ||
): boolean => | ||
Array.isArray(candidateExpression) && | ||
typeof interpreters[candidateExpression[0]] === 'function' | ||
|
||
const _maybeExpression = (value) => | ||
Array.isArray(value) && | ||
typeof value[0] === 'string' && | ||
value[0].startsWith('$') | ||
|
||
const _ellipsis = (str, maxlen = 50) => | ||
str.length > maxlen ? str.substr(0, maxlen - 1).concat('...') : str | ||
|
||
const _evaluateDev = ( | ||
context: EvaluationContext, | ||
expOrValue: Expression | any | ||
): any => { | ||
if ( | ||
!isExpression(context.interpreters, expOrValue) && | ||
_maybeExpression(expOrValue) | ||
) { | ||
console.warn( | ||
`Possible missing expression error: ${_ellipsis( | ||
JSON.stringify(expOrValue) | ||
)}. No interpreter was found for '${expOrValue[0]}'` | ||
) | ||
} | ||
|
||
return _evaluate(context, expOrValue) | ||
} | ||
|
||
const _evaluate = ( | ||
context: EvaluationContext, | ||
expOrValue: Expression | any | ||
): any => { | ||
if (!isExpression(context.interpreters, expOrValue)) { | ||
return expOrValue | ||
} | ||
|
||
const [interpreterId, ...interpreterArgs] = expOrValue | ||
const interpreter = context.interpreters[interpreterId] | ||
|
||
return interpreter(context, ...interpreterArgs) | ||
} | ||
|
||
/** | ||
* @function evaluate | ||
* @param {EvaluationContext} context | ||
* @param {Expression | *} expOrValue | ||
* @returns {*} | ||
*/ | ||
export const evaluate = | ||
process && process.env && process.env.NODE_ENV !== 'production' | ||
? _evaluateDev | ||
: _evaluate | ||
|
||
/** | ||
* @function evaluateTyped | ||
* @param {String | string[]} expectedTypes | ||
* @param {EvaluationContext} context | ||
* @param {Expression | any} expOrValue | ||
* @returns {*} | ||
*/ | ||
export const evaluateTyped = ( | ||
expectedTypes: string | string[], | ||
context: EvaluationContext, | ||
expOrValue: Expression | any | ||
): any => { | ||
const value = evaluate(context, expOrValue) | ||
validateType(expectedTypes, value) | ||
return value | ||
} | ||
|
||
/** | ||
* @function evaluateTypedAsync | ||
* @param {String | string[]} expectedTypes | ||
* @param {EvaluationContext} context | ||
* @param {Expression | any} expOrValue | ||
* @returns {Promise<*>} | ||
*/ | ||
export const evaluateTypedAsync = ( | ||
expectedTypes: string | string[], | ||
context: EvaluationContext, | ||
expOrValue: Expression | any | ||
): Promise<any> => | ||
Promise.resolve(evaluate(context, expOrValue)).then((value) => { | ||
validateType(expectedTypes, value) | ||
return value | ||
}) |
Oops, something went wrong.