forked from hybridsjs/hybrids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefine.js
224 lines (179 loc) · 5.21 KB
/
define.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import property from "./property.js";
import render from "./render.js";
import * as cache from "./cache.js";
import { pascalToDash, deferred } from "./utils.js";
const defaultMethod = (host, value) => value;
export const callbacksMap = new WeakMap();
const propsMap = new WeakMap();
function translate(key, desc) {
const type = typeof desc;
let config;
if (type === "function") {
switch (key) {
case "render":
config = render(desc);
break;
case "content":
config = render(desc, { shadowRoot: false });
break;
default:
config = { get: desc };
}
} else if (type !== "object" || desc === null || Array.isArray(desc)) {
config = property(desc);
} else {
config = {
get: desc.get || defaultMethod,
set: desc.set || (!desc.get && defaultMethod) || undefined,
connect: desc.connect,
observe: desc.observe,
};
}
return config;
}
function compile(Hybrid, descriptors) {
Hybrid.hybrids = descriptors;
const callbacks = [];
const props = Object.keys(descriptors);
callbacksMap.set(Hybrid, callbacks);
propsMap.set(Hybrid, props);
props.forEach(key => {
const config = translate(key, descriptors[key]);
Object.defineProperty(Hybrid.prototype, key, {
get: function get() {
return cache.get(this, key, config.get);
},
set:
config.set &&
function set(newValue) {
cache.set(this, key, config.set, newValue);
},
enumerable: true,
configurable: true,
});
if (config.observe) {
callbacks.unshift(host =>
cache.observe(host, key, config.get, config.observe),
);
}
if (config.connect) {
callbacks.push(host =>
config.connect(host, key, options => {
cache.invalidate(host, key, {
force: options && options.force === true,
});
}),
);
}
});
}
function walkInShadow(node, fn) {
fn(node);
Array.from(node.children).forEach(el => walkInShadow(el, fn));
if (node.shadowRoot) {
Array.from(node.shadowRoot.children).forEach(el => walkInShadow(el, fn));
}
}
const updateQueue = new Map();
function update(Hybrid, lastHybrids) {
if (!updateQueue.size) {
deferred.then(() => {
walkInShadow(document.body, node => {
if (updateQueue.has(node.constructor)) {
const prevHybrids = updateQueue.get(node.constructor);
const hybrids = node.constructor.hybrids;
node.disconnectedCallback();
Object.keys(hybrids).forEach(key => {
const type = typeof hybrids[key];
const clearValue =
type !== "object" &&
type !== "function" &&
hybrids[key] !== prevHybrids[key];
cache.invalidate(node, key, { clearValue });
});
node.connectedCallback();
}
});
updateQueue.clear();
});
}
updateQueue.set(Hybrid, lastHybrids);
}
const disconnects = new WeakMap();
export function defineElement(tagName, hybrids) {
const type = typeof hybrids;
if (!hybrids || type !== "object") {
throw TypeError(`Second argument must be an object: ${type}`);
}
if (tagName !== null) {
const CustomElement = window.customElements.get(tagName);
if (CustomElement) {
if (CustomElement.hybrids === hybrids) {
return CustomElement;
}
if (CustomElement.hybrids) {
Object.keys(CustomElement.hybrids).forEach(key => {
delete CustomElement.prototype[key];
});
const lastHybrids = CustomElement.hybrids;
compile(CustomElement, hybrids);
update(CustomElement, lastHybrids);
return CustomElement;
}
return window.customElements.define(tagName, HTMLElement);
}
}
class Hybrid extends HTMLElement {
constructor() {
super();
const props = propsMap.get(Hybrid);
for (let index = 0; index < props.length; index += 1) {
const key = props[index];
if (hasOwnProperty.call(this, key)) {
const value = this[key];
delete this[key];
this[key] = value;
}
}
cache.suspend(this);
}
connectedCallback() {
cache.unsuspend(this);
const callbacks = callbacksMap.get(Hybrid);
const list = [];
for (let index = 0; index < callbacks.length; index += 1) {
const cb = callbacks[index](this);
if (cb) list.push(cb);
}
disconnects.set(this, list);
}
disconnectedCallback() {
const list = disconnects.get(this);
for (let index = 0; index < list.length; index += 1) {
list[index]();
}
cache.suspend(this);
}
}
compile(Hybrid, hybrids);
if (tagName !== null) {
Object.defineProperty(Hybrid, "name", {
get: () => tagName,
});
customElements.define(tagName, Hybrid);
}
return Hybrid;
}
function defineMap(elements) {
return Object.keys(elements).reduce((acc, key) => {
const tagName = pascalToDash(key);
acc[key] = defineElement(tagName, elements[key]);
return acc;
}, {});
}
export default function define(...args) {
if (typeof args[0] === "object" && args[0] !== null) {
return defineMap(args[0]);
}
return defineElement(...args);
}