pref: remove await/async from internal async calls

This commit is contained in:
harttle
2019-10-26 08:57:33 -05:00
committed by Jun Yang
parent d4ad4b8a8c
commit b82fa9ed2b
40 changed files with 416 additions and 380 deletions
+2 -4
View File
@@ -11,9 +11,7 @@ export default {
this.key = match[1]
this.value = match[2]
},
render: async function (ctx: Context) {
ctx.front()[this.key] = ctx.sync
? this.liquid.evalValueSync(this.value, ctx)
: await this.liquid.evalValue(this.value, ctx)
render: function * (ctx: Context) {
ctx.front()[this.key] = yield this.liquid._evalValue(this.value, ctx)
}
} as ITagImplOptions
+2 -5
View File
@@ -14,16 +14,13 @@ export default {
})
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const blocks = ctx.getRegister('blocks')
const childDefined = blocks[this.block]
const r = this.liquid.renderer
const html = childDefined !== undefined
? childDefined
: (ctx.sync
? r.renderTemplatesSync(this.tpls, ctx)
: await r.renderTemplates(this.tpls, ctx)
)
: yield r.renderTemplates(this.tpls, ctx)
if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
blocks[this.block] = html
+1 -1
View File
@@ -1,7 +1,7 @@
import { Emitter, Context, Hash } from '../../types'
export default {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.break = true
}
}
+2 -4
View File
@@ -20,11 +20,9 @@ export default {
})
stream.start()
},
render: async function (ctx: Context) {
render: function * (ctx: Context) {
const r = this.liquid.renderer
const html = ctx.sync
? r.renderTemplatesSync(this.templates, ctx)
: await r.renderTemplates(this.templates, ctx)
const html = yield r.renderTemplates(this.templates, ctx)
ctx.front()[this.variable] = html
}
} as ITagImplOptions
+5 -19
View File
@@ -24,31 +24,17 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = await new Expression(branch.val).value(ctx)
const cond = await new Expression(this.cond).value(ctx)
const val = yield new Expression(branch.val).value(ctx)
const cond = yield new Expression(this.cond).value(ctx)
if (val === cond) {
await r.renderTemplates(branch.templates, ctx, emitter)
yield r.renderTemplates(branch.templates, ctx, emitter)
return
}
}
await r.renderTemplates(this.elseTemplates, ctx, emitter)
},
renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = new Expression(branch.val).valueSync(ctx)
const cond = new Expression(this.cond).valueSync(ctx)
if (val === cond) {
r.renderTemplatesSync(branch.templates, ctx, emitter)
return
}
}
r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
yield r.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
+1 -1
View File
@@ -1,7 +1,7 @@
import { Emitter, Context, Hash } from '../../types'
export default {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.continue = true
}
}
+3 -7
View File
@@ -21,10 +21,8 @@ export default {
assert(this.candidates.length, `empty candidates: ${tagToken.raw}`)
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const group = ctx.sync
? this.group.valueSync(ctx)
: await this.group.value(ctx)
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const group = yield this.group.value(ctx)
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = ctx.getRegister('cycle')
let idx = groups[fingerprint]
@@ -36,9 +34,7 @@ export default {
const candidate = this.candidates[idx]
idx = (idx + 1) % this.candidates.length
groups[fingerprint] = idx
const html = ctx.sync
? new Expression(candidate).valueSync(ctx)
: await new Expression(candidate).value(ctx)
const html = yield new Expression(candidate).value(ctx)
emitter.write(html)
}
} as ITagImplOptions
+4 -10
View File
@@ -36,11 +36,9 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
let collection = ctx.sync
? new Expression(this.collection).valueSync(ctx)
: await new Expression(this.collection).value(ctx)
let collection = yield new Expression(this.collection).value(ctx)
if (!isArray(collection)) {
if (isString(collection) && collection.length > 0) {
@@ -50,9 +48,7 @@ export default {
}
}
if (!isArray(collection) || !collection.length) {
ctx.sync
? r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
: await r.renderTemplates(this.elseTemplates, ctx, emitter)
yield r.renderTemplates(this.elseTemplates, ctx, emitter)
return
}
@@ -66,9 +62,7 @@ export default {
ctx.push(scope)
for (const item of collection) {
scope[this.variable] = item
ctx.sync
? r.renderTemplatesSync(this.templates, ctx, emitter)
: await r.renderTemplates(this.templates, ctx, emitter)
yield r.renderTemplates(this.templates, ctx, emitter)
if (emitter.break) {
emitter.break = false
break
+4 -17
View File
@@ -27,29 +27,16 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
for (const branch of this.branches) {
const cond = await new Expression(branch.cond).value(ctx)
const cond = yield new Expression(branch.cond).value(ctx)
if (isTruthy(cond)) {
await r.renderTemplates(branch.templates, ctx, emitter)
yield r.renderTemplates(branch.templates, ctx, emitter)
return
}
}
await r.renderTemplates(this.elseTemplates, ctx, emitter)
},
renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
for (const branch of this.branches) {
const cond = new Expression(branch.cond).valueSync(ctx)
if (isTruthy(cond)) {
r.renderTemplatesSync(branch.templates, ctx, emitter)
return
}
}
r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
yield r.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
+6 -36
View File
@@ -17,14 +17,14 @@ export default {
match = withRE.exec(token.args)
if (match) this.with = match[1]
},
renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
let filepath
if (ctx.opts.dynamicPartials) {
if (quotedLine.exec(this.value)) {
const template = this.value.slice(1, -1)
filepath = this.liquid.parseAndRenderSync(template, ctx.getAll(), ctx.opts)
filepath = yield this.liquid._parseAndRender(template, ctx.getAll(), ctx.opts, ctx.sync)
} else {
filepath = new Expression(this.value).valueSync(ctx)
filepath = yield new Expression(this.value).value(ctx)
}
} else {
filepath = this.staticValue
@@ -37,41 +37,11 @@ export default {
ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = new Expression(this.with).evaluateSync(ctx)
hash[filepath] = yield new Expression(this.with).evaluate(ctx)
}
const templates = this.liquid.parseFileSync(filepath, ctx.opts)
const templates = yield this.liquid._parseFile(filepath, ctx.opts, ctx.sync)
ctx.push(hash)
this.liquid.renderer.renderTemplatesSync(templates, ctx, emitter)
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let filepath
if (ctx.opts.dynamicPartials) {
if (quotedLine.exec(this.value)) {
const template = this.value.slice(1, -1)
filepath = await this.liquid.parseAndRender(template, ctx.getAll(), ctx.opts)
} else {
filepath = await new Expression(this.value).value(ctx)
}
} else {
filepath = this.staticValue
}
assert(filepath, `cannot include with empty filename`)
const originBlocks = ctx.getRegister('blocks')
const originBlockMode = ctx.getRegister('blockMode')
ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = await new Expression(this.with).evaluate(ctx)
}
const templates = await this.liquid.parseFile(filepath, ctx.opts)
ctx.push(hash)
await this.liquid.renderer.renderTemplates(templates, ctx, emitter)
yield this.liquid.renderer.renderTemplates(templates, ctx, emitter)
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
+5 -14
View File
@@ -19,12 +19,9 @@ export default {
this.tpls = this.liquid.parser.parse(remainTokens)
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const layout = ctx.opts.dynamicPartials
? (ctx.sync
? new Expression(this.layout).valueSync(ctx)
: await new Expression(this.layout).value(ctx)
)
? yield new Expression(this.layout).value(ctx)
: this.staticLayout
assert(layout, `cannot apply layout with empty filename`)
@@ -32,20 +29,14 @@ export default {
ctx.setRegister('blockMode', BlockMode.STORE)
const blocks = ctx.getRegister('blocks')
const r = this.liquid.renderer
const html = ctx.sync
? r.renderTemplatesSync(this.tpls, ctx)
: await r.renderTemplates(this.tpls, ctx)
const html = yield r.renderTemplates(this.tpls, ctx)
if (blocks[''] === undefined) {
blocks[''] = html
}
const templates = ctx.sync
? this.liquid.parseFileSync(layout, ctx.opts)
: await this.liquid.parseFile(layout, ctx.opts)
const templates = yield this.liquid._parseFile(layout, ctx.opts, ctx.sync)
ctx.push(hash)
ctx.setRegister('blockMode', BlockMode.OUTPUT)
const partial = ctx.sync
? r.renderTemplatesSync(templates, ctx)
: await r.renderTemplates(templates, ctx)
const partial = yield r.renderTemplates(templates, ctx)
ctx.pop()
emitter.write(partial)
}
+3 -3
View File
@@ -1,4 +1,4 @@
import { TagToken, Token, ITagImplOptions, Context } from '../../types'
import { Hash, Emitter, TagToken, Token, ITagImplOptions, Context } from '../../types'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
@@ -15,7 +15,7 @@ export default {
})
stream.start()
},
render: function (ctx: Context) {
return this.tokens.map((token: Token) => token.raw).join('')
render: function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.write(this.tokens.map((token: Token) => token.raw).join(''))
}
} as ITagImplOptions
+3 -7
View File
@@ -28,10 +28,8 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = ctx.sync
? new Expression(this.collection).valueSync(ctx) || []
: await new Expression(this.collection).value(ctx) || []
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = (yield new Expression(this.collection).value(ctx)) || []
const offset = hash.offset || 0
const limit = (hash.limit === undefined) ? collection.length : hash.limit
@@ -50,9 +48,7 @@ export default {
emitter.write(`<tr class="row${tablerowloop.row()}">`)
}
emitter.write(`<td class="col${tablerowloop.col()}">`)
ctx.sync
? r.renderTemplatesSync(this.templates, ctx, emitter)
: await r.renderTemplates(this.templates, ctx, emitter)
yield r.renderTemplates(this.templates, ctx, emitter)
emitter.write('</td>')
}
if (collection.length) emitter.write('</tr>')
+3 -11
View File
@@ -20,18 +20,10 @@ export default {
stream.start()
},
renderSync: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
const cond = new Expression(this.cond).valueSync(ctx)
isFalsy(cond)
? r.renderTemplatesSync(this.templates, ctx, emitter)
: r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
const cond = await new Expression(this.cond).value(ctx)
await (isFalsy(cond)
const cond = yield new Expression(this.cond).value(ctx)
yield (isFalsy(cond)
? r.renderTemplates(this.templates, ctx, emitter)
: r.renderTemplates(this.elseTemplates, ctx, emitter))
}