-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: make the most of streamed rendering
- Loading branch information
Showing
13 changed files
with
65 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,6 @@ | |
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"liquidjs": "*" | ||
"liquidjs": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,39 @@ | ||
import BlockMode from '../../context/block-mode' | ||
import { BlockDrop } from '../../drop/block-drop' | ||
import { ParseStream, TagToken, TopLevelToken, Template, Context, TagImpl, Emitter } from '../../types' | ||
import { TagToken, TopLevelToken, Template, Context, TagImpl, Emitter } from '../../types' | ||
|
||
export default { | ||
parse (this: TagImpl, token: TagToken, remainTokens: TopLevelToken[]) { | ||
const match = /\w+/.exec(token.args) | ||
this.block = match ? match[0] : '' | ||
this.tpls = [] as Template[] | ||
const stream: ParseStream = this.liquid.parser.parseStream(remainTokens) | ||
.on('tag:endblock', () => stream.stop()) | ||
this.liquid.parser.parseStream(remainTokens) | ||
.on('tag:endblock', function () { this.stop() }) | ||
.on('template', (tpl: Template) => this.tpls.push(tpl)) | ||
.on('end', () => { | ||
throw new Error(`tag ${token.getText()} not closed`) | ||
}) | ||
stream.start() | ||
.on('end', () => { throw new Error(`tag ${token.getText()} not closed`) }) | ||
.start() | ||
}, | ||
|
||
* render (this: TagImpl, ctx: Context, emitter: Emitter) { | ||
const blockRender = this.getBlockRender(ctx) | ||
yield this.emitHTML(ctx, emitter, blockRender) | ||
if (ctx.getRegister('blockMode') === BlockMode.STORE) { | ||
ctx.getRegister('blocks')[this.block] = blockRender | ||
} else { | ||
yield blockRender(new BlockDrop(), emitter) | ||
} | ||
}, | ||
|
||
getBlockRender (this: TagImpl, ctx: Context) { | ||
const { liquid, tpls } = this | ||
const extendedBlockRender = ctx.getRegister('blocks')[this.block] | ||
const defaultBlockRender = function * (superBlock: BlockDrop) { | ||
const renderChild = ctx.getRegister('blocks')[this.block] | ||
const renderCurrent = function * (superBlock: BlockDrop, emitter: Emitter) { | ||
// add {{ block.super }} support when rendering | ||
ctx.push({ block: superBlock }) | ||
const result = yield liquid.renderer.renderTemplates(tpls, ctx) | ||
yield liquid.renderer.renderTemplates(tpls, ctx, emitter) | ||
ctx.pop() | ||
return result | ||
} | ||
return extendedBlockRender | ||
? (superBlock: BlockDrop) => extendedBlockRender(new BlockDrop(() => defaultBlockRender(superBlock))) | ||
: defaultBlockRender | ||
}, | ||
|
||
* emitHTML (this: TagImpl, ctx: Context, emitter: Emitter, blockRender: (block: BlockDrop) => string) { | ||
if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) { | ||
ctx.getRegister('blocks')[this.block] = blockRender | ||
} else { | ||
emitter.write(yield blockRender(new BlockDrop())) | ||
} | ||
return renderChild | ||
? (superBlock: BlockDrop, emitter: Emitter) => renderChild(new BlockDrop(() => renderCurrent(superBlock, emitter)), emitter) | ||
: renderCurrent | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
export interface Emitter { | ||
/** | ||
* Write a html value into emitter | ||
* @param html string, Drop or other primitive value | ||
*/ | ||
write (html: any): void; | ||
end (): void; | ||
/** | ||
* Buffered string | ||
*/ | ||
buffer: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import { stringify, toValue } from '../util/underscore' | ||
import { Emitter } from '../types' | ||
|
||
export class KeepingTypeEmitter { | ||
public html: any = ''; | ||
export class KeepingTypeEmitter implements Emitter { | ||
public buffer: any = ''; | ||
|
||
public write (html: any) { | ||
html = toValue(html) | ||
// This will only preserve the type if the value is isolated. | ||
// I.E: | ||
// {{ my-port }} -> 42 | ||
// {{ my-host }}:{{ my-port }} -> 'host:42' | ||
if (typeof html !== 'string' && this.html === '') { | ||
this.html = html | ||
if (typeof html !== 'string' && this.buffer === '') { | ||
this.buffer = html | ||
} else { | ||
this.html = stringify(this.html) + stringify(html) | ||
this.buffer = stringify(this.buffer) + stringify(html) | ||
} | ||
} | ||
|
||
public end () { | ||
return this.html | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters