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

replace await/Sync code with callback #164

Closed
harttle opened this issue Oct 23, 2019 · 0 comments
Closed

replace await/Sync code with callback #164

harttle opened this issue Oct 23, 2019 · 0 comments

Comments

@harttle
Copy link
Owner

harttle commented Oct 23, 2019

This will reduce the overall code size, reduce redundency and remove dependency to Promsie (which is trivial).

For example in the builtin/tags/include.ts:

renderSync: 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)
} else {
filepath = new Expression(this.value).valueSync(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] = new Expression(this.with).evaluateSync(ctx)
}
const templates = this.liquid.parseFileSync(filepath, ctx.opts)
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)
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
}

The render() method and renderSync() method are almost the same. The tricky part is Async and Sync functions cannot be easily wrapped so that they expose the same interface. Maybe refactor into callbacks or generators can address this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant