/
define.js
182 lines (146 loc) · 4.73 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
import property from './property';
import render from './render';
import * as cache from './cache';
import { pascalToDash, deferred } from './utils';
/* istanbul ignore next */
try { process.env.NODE_ENV } catch(e) { var process = { env: { NODE_ENV: 'production' } }; } // eslint-disable-line
const defaultMethod = (host, value) => value;
const execute = fn => fn();
function compile(Hybrid, descriptors) {
Hybrid.hybrids = descriptors;
Hybrid.connects = [];
Object.keys(descriptors).forEach((key) => {
const desc = descriptors[key];
const type = typeof desc;
let config;
if (type === 'function') {
config = key === 'render' ? render(desc) : { get: desc };
} else if (desc === null || type !== 'object' || (type === 'object' && !desc.get && !desc.set && !desc.connect && !desc.observe)) {
config = property(desc);
} else {
config = {
get: desc.get || defaultMethod,
set: desc.set || (!desc.get && defaultMethod) || undefined,
connect: desc.connect,
observe: desc.observe,
};
}
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: process.env.NODE_ENV !== 'production',
});
if (config.connect) {
Hybrid.connects.push(host => config.connect(host, key, () => {
cache.invalidate(host, key);
}));
}
if (config.observe) {
Hybrid.connects.push((host) => {
let lastValue;
return cache.observe(host, key, () => {
const value = host[key];
config.observe(host, value, lastValue);
lastValue = value;
});
});
}
});
}
let update;
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
const 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();
update = (Hybrid, lastHybrids) => {
if (!updateQueue.size) {
deferred.then(() => {
walkInShadow(document.body, (node) => {
if (updateQueue.has(node.constructor)) {
const hybrids = updateQueue.get(node.constructor);
node.disconnectedCallback();
Object.keys(node.constructor.hybrids).forEach((key) => {
cache.invalidate(node, key, node[key] === hybrids[key]);
});
node.connectedCallback();
}
});
updateQueue.clear();
});
}
updateQueue.set(Hybrid, lastHybrids);
};
}
const disconnects = new WeakMap();
function defineElement(tagName, hybridsOrConstructor) {
const type = typeof hybridsOrConstructor;
if (type !== 'object' && type !== 'function') {
throw TypeError(`Second argument must be an object or a function: ${type}`);
}
const CustomElement = window.customElements.get(tagName);
if (type === 'function') {
if (CustomElement !== hybridsOrConstructor) {
return window.customElements.define(tagName, hybridsOrConstructor);
}
return CustomElement;
}
if (CustomElement) {
if (CustomElement.hybrids === hybridsOrConstructor) {
return CustomElement;
}
if (process.env.NODE_ENV !== 'production' && CustomElement.hybrids) {
Object.keys(CustomElement.hybrids).forEach((key) => {
delete CustomElement.prototype[key];
});
const lastHybrids = CustomElement.hybrids;
compile(CustomElement, hybridsOrConstructor);
update(CustomElement, lastHybrids);
return CustomElement;
}
throw Error(`Element '${tagName}' already defined`);
}
class Hybrid extends HTMLElement {
static get name() { return tagName; }
connectedCallback() {
const { connects } = this.constructor;
const list = [];
for (let index = 0; index < connects.length; index += 1) {
const disconnect = connects[index](this);
if (disconnect) list.push(disconnect);
}
disconnects.set(this, list);
}
disconnectedCallback() {
disconnects.get(this).forEach(execute);
}
}
compile(Hybrid, hybridsOrConstructor);
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') {
return defineMap(args[0]);
}
return defineElement(...args);
}