feat: stream rendering, closed #361 fixes #360

This commit is contained in:
Harttle
2021-09-30 22:15:09 +08:00
committed by Jun Yang
parent abaf4af465
commit 9012133e07
18 changed files with 147 additions and 53 deletions
+6 -4
View File
@@ -13,7 +13,6 @@ import { FilterMap } from './template/filter/filter-map'
import { LiquidOptions, normalizeStringArray, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
import { FilterImplOptions } from './template/filter/filter-impl-options'
import { toPromise, toValue } from './util/async'
import { Emitter } from './render/emitter'
export * from './util/error'
export * from './types'
@@ -24,7 +23,7 @@ export class Liquid {
public parser: Parser
public filters: FilterMap
public tags: TagMap
private parseFileImpl: (file: string, sync?: boolean) => Iterator<Template[]>
public parseFileImpl: (file: string, sync?: boolean) => Iterator<Template[]>
public constructor (opts: LiquidOptions = {}) {
this.options = applyDefault(normalize(opts))
@@ -45,8 +44,7 @@ export class Liquid {
public _render (tpl: Template[], scope?: object, sync?: boolean): IterableIterator<any> {
const ctx = new Context(scope, this.options, sync)
const emitter = new Emitter(this.options.keepOutputType)
return this.renderer.renderTemplates(tpl, ctx, emitter)
return this.renderer.renderTemplates(tpl, ctx)
}
public async render (tpl: Template[], scope?: object): Promise<any> {
return toPromise(this._render(tpl, scope, false))
@@ -54,6 +52,10 @@ export class Liquid {
public renderSync (tpl: Template[], scope?: object): any {
return toValue(this._render(tpl, scope, true))
}
public renderToNodeStream (tpl: Template[], scope?: object): NodeJS.ReadableStream {
const ctx = new Context(scope, this.options)
return this.renderer.renderTemplatesToNodeStream(tpl, ctx)
}
public _parseAndRender (html: string, scope?: object, sync?: boolean): IterableIterator<any> {
const tpl = this.parse(html)