Skip to content

Commit

Permalink
feat: adding globalAwait option
Browse files Browse the repository at this point in the history
This option adds all data objects into an array and await for it
globally.
  • Loading branch information
SilentVoid13 committed Apr 5, 2021
1 parent 7102b66 commit 353ceb7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,11 +67,28 @@ export default function compileToString(str: string, config: EtaConfig): string
*/

function compileScope(buff: Array<AstObject>, 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
Expand All @@ -84,14 +102,23 @@ function compileScope(buff: Array<AstObject>, 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 + ')'
}
Expand All @@ -100,6 +127,7 @@ function compileScope(buff: Array<AstObject>, config: EtaConfig) {
content = 'E.e(' + content + ')'
}
returnStr += 'tR+=' + content + '\n'
j++
// reference
} else if (type === 'e') {
// execute
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 353ceb7

Please sign in to comment.