perf: make the most of streamed rendering

This commit is contained in:
Harttle
2021-10-01 18:36:57 +08:00
committed by harttle
parent 4358c9630f
commit aea34418de
13 changed files with 65 additions and 71 deletions
+8 -1
View File
@@ -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;
}
+6 -9
View File
@@ -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 -6
View File
@@ -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 -1
View File
@@ -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))