Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding globalAwait option #102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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