perf: use polymophism instead duck test

This commit is contained in:
harttle
2019-03-25 20:11:23 +08:00
parent 64dd057552
commit 82d7673554
18 changed files with 120 additions and 98 deletions
+3 -7
View File
@@ -18,20 +18,16 @@ export default class ParseStream {
this.handlers[name] = cb
return this
}
trigger <T extends Token | ITemplate> (event: string, arg?: T) {
private trigger <T extends Token | ITemplate> (event: string, arg?: T) {
const h = this.handlers[event]
if (typeof h === 'function') {
h(arg)
return true
}
return false
return h ? (h(arg), true) : false
}
start () {
this.trigger('start')
let token: Token | undefined
while (!this.stopRequested && (token = this.tokens.shift())) {
if (this.trigger('token', token)) continue
if (token.type === 'tag' && this.trigger(`tag:${(<TagToken>token).name}`, token)) {
if (TagToken.is(token) && this.trigger(`tag:${token.name}`, token)) {
continue
}
const template = this.parseToken(token, this.tokens)