From 353ceb7f0f356e88d0b86f4a9116019684f20de1 Mon Sep 17 00:00:00 2001 From: SilentVoid13 <51264226+SilentVoid13@users.noreply.github.com> Date: Thu, 1 Apr 2021 15:34:25 +0200 Subject: [PATCH] feat: adding globalAwait option This option adds all data objects into an array and await for it globally. --- src/compile-string.ts | 32 ++++++++++++++++++++++++++++++-- src/config.ts | 3 +++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/compile-string.ts b/src/compile-string.ts index b360c84a..a449eb7a 100644 --- a/src/compile-string.ts +++ b/src/compile-string.ts @@ -26,6 +26,7 @@ export default function compileToString(str: string, config: EtaConfig): string (config.include ? ',include=E.include.bind(E)' : '') + (config.includeFile ? ',includeFile=E.includeFile.bind(E)' : '') + '\nfunction layout(p,d){__l=p;__lP=d}\n' + + (config.globalAwait ? 'let _prs = [];\n' : '') + (config.useWith ? 'with(' + config.varName + '||{}){' : '') + compileScope(buffer, config) + (config.includeFile @@ -66,11 +67,28 @@ export default function compileToString(str: string, config: EtaConfig): string */ function compileScope(buff: Array, config: EtaConfig) { - let i = 0 + let i const buffLength = buff.length let returnStr = '' - for (i; i < buffLength; i++) { + if (config.globalAwait) { + for (i = 0; i < buffLength; i++) { + const currentBlock = buff[i] + + if (typeof currentBlock !== 'string') { + const type = currentBlock.t + + if (type === 'r' || type === 'i') { + const content = currentBlock.val || '' + returnStr += `_prs.push(${content});\n` + } + } + } + returnStr += 'let _rst = await Promise.all(_prs);\n' + } + + let j = 0 + for (i = 0; i < buffLength; i++) { const currentBlock = buff[i] if (typeof currentBlock === 'string') { const str = currentBlock @@ -84,14 +102,23 @@ function compileScope(buff: Array, config: EtaConfig) { if (type === 'r') { // raw + if (config.globalAwait) { + content = `_rst[${j}]` + } + if (config.filter) { content = 'E.filter(' + content + ')' } returnStr += 'tR+=' + content + '\n' + j++ } else if (type === 'i') { // interpolate + if (config.globalAwait) { + content = `_rst[${j}]` + } + if (config.filter) { content = 'E.filter(' + content + ')' } @@ -100,6 +127,7 @@ function compileScope(buff: Array, config: EtaConfig) { content = 'E.e(' + content + ')' } returnStr += 'tR+=' + content + '\n' + j++ // reference } else if (type === 'e') { // execute diff --git a/src/config.ts b/src/config.ts index 563c36ce..27d0eb53 100644 --- a/src/config.ts +++ b/src/config.ts @@ -61,6 +61,9 @@ export interface EtaConfig { /** A filter function applied to every interpolation or raw interpolation */ filter?: Function + /** Adds all variables in big Promise.all and awaits for it */ + globalAwait?: boolean + /** Function to include templates by name */ include?: Function