mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
perf: make the most of streamed rendering
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
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)
|
||||
@@ -9,14 +10,10 @@ export class KeepingTypeEmitter {
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,9 @@ import { stringify } from '../util/underscore'
|
||||
import { Emitter } from './emitter'
|
||||
|
||||
export class SimpleEmitter implements Emitter {
|
||||
public html: any = '';
|
||||
public buffer = '';
|
||||
|
||||
public write (html: any) {
|
||||
this.html += stringify(html)
|
||||
}
|
||||
|
||||
public end () {
|
||||
return this.html
|
||||
this.buffer += stringify(html)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { stringify } from '../util/underscore'
|
||||
|
||||
export class StreamedEmitter {
|
||||
public html: any = '';
|
||||
public buffer = '';
|
||||
public stream = new (require('stream').PassThrough)()
|
||||
public write (html: any) {
|
||||
this.stream.write(stringify(html))
|
||||
|
||||
Reference in New Issue
Block a user