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
+4
View File
@@ -0,0 +1,4 @@
export interface Emitter {
write (html: any): void;
end (): void;
}
+22
View File
@@ -0,0 +1,22 @@
import { stringify, toValue } from '../util/underscore'
export class KeepingTypeEmitter {
public html: 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
} else {
this.html = stringify(this.html) + stringify(html)
}
}
public end () {
return this.html
}
}
+14
View File
@@ -0,0 +1,14 @@
import { stringify } from '../util/underscore'
import { Emitter } from './emitter'
export class SimpleEmitter implements Emitter {
public html: any = '';
public write (html: any) {
this.html += stringify(html)
}
public end () {
return this.html
}
}
+12
View File
@@ -0,0 +1,12 @@
import { stringify } from '../util/underscore'
export class StreamedEmitter {
public html: any = '';
public stream = new (require('stream').PassThrough)()
public write (html: any) {
this.stream.write(stringify(html))
}
public end () {
this.stream.end()
}
}