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
+14 -28
View File
@@ -1,29 +1,15 @@
import { stringify, toValue } from '../util/underscore'
export class Emitter {
public html: any = '';
public break = false;
public continue = false;
private keepOutputType? = false;
constructor (keepOutputType: boolean|undefined) {
this.keepOutputType = keepOutputType
}
public write (html: any) {
if (this.keepOutputType === true) {
html = toValue(html)
} else {
html = stringify(html)
}
// This will only preserve the type if the value is isolated.
// I.E:
// {{ my-port }} -> 42
// {{ my-host }}:{{ my-port }} -> 'host:42'
if (this.keepOutputType === true && typeof html !== 'string' && this.html === '') {
this.html = html
} else {
this.html = stringify(this.html) + stringify(html)
}
}
export interface Emitter {
/**
* Write a html value into emitter
* @param html string, Drop or other primitive value
*/
write (html: any): void;
/**
* Notify the emitter render has ended
*/
end (): void;
/**
* Collect rendered string value immediately
*/
collect (): string;
}