-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempl-model.js
56 lines (56 loc) · 1.57 KB
/
templ-model.js
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
52
53
54
55
56
import { ObjML } from 'obj-ml/obj-ml.js';
import { xc } from 'xtal-element/lib/XtalCore.js';
import { TemplateInstance } from '@github/template-parts/lib/index.js';
export class TemplModel extends ObjML {
constructor() {
super(...arguments);
this.self = this;
this.propActions = propActions;
this.reactor = new xc.Rx(this);
this.__dontUpdate = false;
}
connectedCallback() {
super.connectedCallback();
xc.mergeProps(this, slicedPropDefs);
}
get value() {
return super.value;
}
set value(nv) {
if (this.templateInstance !== undefined) {
this.templateInstance.update(nv);
}
super.value = nv;
}
onPropChange(n, prop, nv) {
this.reactor.addToQueue(prop, nv);
}
}
TemplModel.is = 'templ-model';
const onTemplateInstance = ({ templateInstance, self }) => {
if (self.__dontUpdate) {
self.__dontUpdate = false;
return;
}
if (self.value !== undefined) {
templateInstance.update(self.value);
}
};
const onTemplate = ({ template, self }) => {
self.__dontUpdate = true;
self.templateInstance = new TemplateInstance(template, self.value || {});
};
const propActions = [onTemplateInstance, onTemplate];
const baseObj = {
type: Object,
dry: true,
async: true,
stopReactionsIfFalsy: true,
};
const propDefMap = {
templateInstance: baseObj,
template: baseObj,
};
const slicedPropDefs = xc.getSlicedPropDefs(propDefMap);
xc.letThereBeProps(TemplModel, slicedPropDefs, 'onPropChange');
xc.define(TemplModel);