-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTemplateInstance.ts
51 lines (41 loc) · 1.44 KB
/
TemplateInstance.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import TemplatePart from './TemplatePart.js'
import AttributeTemplatePart from './AttributeTemplatePart.js';
import NodeTemplatePart from './NodeTemplatePart.js';
import InnerTemplatePart from './InnerTemplatePart.js';
type TemplateProcessor = {
create?: (templateInstance: TemplateInstance, state: any) => void,
process: (templateInstance: TemplateInstance, state: any) => void,
}
function isAttributeTemplatePart(part: TemplatePart): part is AttributeTemplatePart {
return part instanceof AttributeTemplatePart
}
function isInnerTemplatePart(part: TemplatePart): part is InnerTemplatePart {
return part instanceof InnerTemplatePart
}
function isNodeTemplatePart(part: TemplatePart): part is NodeTemplatePart {
return part instanceof NodeTemplatePart
}
export default class TemplateInstance extends DocumentFragment {
_parts: Array<TemplatePart>
_processor: TemplateProcessor
constructor(parts: Array<TemplatePart>, processor: TemplateProcessor) {
super()
this._parts = parts
this._processor = processor
}
get parts() {
return this._parts
}
get attributeParts(): Array<AttributeTemplatePart> {
return this._parts.filter(isAttributeTemplatePart)
}
get innerParts(): Array<InnerTemplatePart> {
return this._parts.filter(isInnerTemplatePart)
}
get nodeParts(): Array<NodeTemplatePart> {
return this._parts.filter(isNodeTemplatePart)
}
update(state: any) {
this._processor.process(this, state)
}
}