-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcomponent.js
More file actions
382 lines (321 loc) · 10.4 KB
/
component.js
File metadata and controls
382 lines (321 loc) · 10.4 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import {
EMPTY,
InstanceCache,
Props,
Transaction,
VTree,
State,
ActiveRenderState,
} from './util/types';
import {
$$render,
$$vTree,
$$unsubscribe,
$$type,
$$hooks,
} from './util/symbols';
import diff from './util/binding';
import middleware from './lifecycle/middleware';
const { Internals } = diff;
const { isArray } = Array;
const { setPrototypeOf, defineProperty, keys, assign } = Object;
const RenderDebounce = new WeakMap();
/**
* @param {Props} defaultProps
* @returns {string[]}
*/
const getObserved = ({ defaultProps }) =>
defaultProps ? keys(defaultProps) : EMPTY.ARR;
/**
* Creates the props object for Web components.
*
* @param {any} domNode
* @param {Props} existingProps
*/
const createProps = (domNode, existingProps = {}) => {
return getObserved(domNode.constructor).reduce((props, attr) => ({
[attr]: (
domNode.hasAttribute(attr) ? domNode.getAttribute(attr) : domNode[attr]
) || props[attr] || domNode.constructor.defaultProps[attr],
...props,
}), existingProps);
};
/**
* Creates the state object for Web components.
*
* @param {Component} domNode
* @param {State} newState
*/
const createState = (domNode, newState) => assign({}, domNode.state, newState);
/**
* Recreates the VTree used for diffing a stateful component. In the case of
* Web components, this will use the Shadow Root instead of creating a virtual
* fragment.
*
* @param {Component} instance - {Component} instance
* @param {Boolean} isWebComponent - Is this a web component?
* @return {VTree | HTMLElement} Recreated VTree including component references
*/
const createMountPoint = (instance, isWebComponent) => {
if (isWebComponent) {
return /** @type {any} */(instance).shadowRoot;
}
else {
return instance[$$vTree];
}
};
/**
* Represents a vanilla JavaScript Component. This is a lightweight version of
* what you'd find in most modern web application libraries. It provides a
* React-inspired API.
*/
export default class Component {
static get observedAttributes() {
return getObserved(this).map(key => key.toLowerCase());
}
/**
* Allow tests to unbind this task, you would not typically need to do this in
* a web application, as this code loads once and is not reloaded.
*
* @type {Function | null}
*/
static [$$unsubscribe] = null;
/**
* Adds the Component middleware, but first unregisters any previous
* Component middleware.
*
* @returns {void}
*/
static subscribeMiddleware() {
const unsubscribe = Component[$$unsubscribe];
unsubscribe && unsubscribe();
Component[$$unsubscribe] = diff.use(middleware());
}
/**
* Removes the Component middleware and clears the internal caches.
*
* @returns {void}
*/
static unsubscribeMiddleware() {
const unsubscribe = Component[$$unsubscribe];
unsubscribe && unsubscribe();
InstanceCache.clear();
}
/**
* Creates a lightweight stateful JavaScript component. Takes the static
* default props and applies them to the incoming initial props.
*
* @param {Props} initialProps
*/
constructor(initialProps) {
let instance = this;
// This is how we detect if this is a Web Component or not. The class
// must be registered ahead of time within the customElements global
// registry. This will allow us to construct the HTMLElement and attach the
// shadow root.
try {
instance = Reflect.construct(HTMLElement, [], new.target);
/** @type {any} */ (instance).attachShadow({ mode: 'open' });
/** @type {any} */ (instance)[$$type] = 'web';
} catch {
// Not a Web Component.
/** @type {any} */ (instance)[$$type] = 'class';
}
const props = instance.props = assign({}, initialProps);
const { defaultProps = EMPTY.OBJ } = /** @type {any} */ (instance.constructor);
// Merge default props into props object.
if (typeof defaultProps === 'object' && !isArray(defaultProps)) {
keys(defaultProps || EMPTY.OBJ).forEach(prop => {
if (prop in props && props[prop] !== undefined) {
return;
}
instance.props[prop] = defaultProps[prop];
});
}
return instance;
}
/**
* @param {Props=} _props
* @param {State=} _state
*
* @returns {VTree[] | VTree | any}
*/
render(_props, _state) {
return undefined;
}
/**
* Changes the component state synchronously by shallow merging the incoming
* state into the previous state. Rendering will then be scheduled to the
* next reachable tick. If this is called multiple times on a single tick
* all that will happen is the state will be updated by no rendering will
* occur until the next tick.
*
* You can await this method to know when rendering has completed.
*
* @param {State} state
*/
setState(state) {
this.state = assign({}, this.state, state);
if (!RenderDebounce.has(this)) {
RenderDebounce.set(this, new Promise(resolve => setTimeout(() => {
RenderDebounce.delete(this);
this.componentWillReceiveProps(this.props, this.state);
if (this.shouldComponentUpdate(this.props, this.state)) {
resolve(this[$$render]());
}
else {
resolve(null);
}
})));
}
return RenderDebounce.get(this);
}
/**
* Schedule a render to happen, bypassing shouldComponentUpdate. Bound to the
* same tick logic as setState, allowing this to be called as many times as
* you want.
*/
forceUpdate() {
if (!RenderDebounce.has(this)) {
RenderDebounce.set(this, new Promise(resolve => setTimeout(() => {
RenderDebounce.delete(this);
this.componentWillReceiveProps(this.props, this.state);
resolve(this[$$render]());
})));
}
return RenderDebounce.get(this);
}
/** @type {State} */
state = {};
/** @type {'web'|'class'} */
[$$type] = 'class';
/** @type {VTree | null} */
[$$vTree] = null;
/** @type {{ fns: Function[], i: number }} */
[$$hooks] = { fns: [], i: 0 };
/**
* Stateful render. Used when a component changes and needs to re-render
* itself and the underlying tree it contains. This is triggered on
* `setState` and `forceUpdate` calls with class components and `createState`
* updates with function components.
*
* Web Components are easy to implement using the Shadow DOM to encapsulate
* the mount point using `innerHTML`.
*
* React-like components are supported by recreating the previous component
* tree and comparing this to the new tree using `outerHTML`.
*
* @return {Transaction | unknown | undefined}
*/
[$$render]() {
const vTree = /** @type {VTree=} */ this[$$vTree];
const oldProps = this.props;
const oldState = this.state;
// There are some slight differences between rendering a typical class or
// function based component and a web component. Web Components are
// rendered into the shadow DOM, while the class based components need
// extra logic to handle the invisible component boundaries.
const isWebComponent = this[$$type] === 'web';
/**
* Recreate the existing tree including this component. This is not
* directly stored anywhere, as the VDOM tree is flattened, and must be
* recreated per render.
*
* @type {VTree | HTMLElement}
*/
const mount = createMountPoint(this, isWebComponent);
if (isWebComponent) {
this.props = createProps(this, this.props);
this.state = createState(this, this.state);
}
// Make this component active and then synchronously render.
ActiveRenderState.push(this);
// TBD Is this needed now that components track their own descendents?
if ($$hooks in this) {
this[$$hooks].i = 0;
}
const rendered = this.render(this.props, this.state);
const transaction = Internals.Transaction.create(
mount,
rendered,
{ inner: true },
);
// Set the VTree attributes to match the current props and use this as the initial state for a re-render.
if (vTree) {
assign(vTree.attributes, this.props);
transaction.state.oldTree = vTree;
transaction.state.isDirty = false;
}
// Middleware and task changes can affect the return value, it's not always guarenteed to be the transaction
// this allows us to tap into that return value and remain consistent.
const retVal = transaction.start();
// Reset the active state after rendering so we don't accidentally bleed
// into other components render cycle.
ActiveRenderState.length = 0;
this.componentDidUpdate(oldProps, oldState);
return retVal;
}
connectedCallback() {
const { defaultProps = {} } = /** @type {any} */ (this).constructor;
// This callback gets called during replace operations, there is no point
// in re-rendering or creating a new shadow root due to this.
// This is the initial render for the Web Component
this[$$render]();
this.componentDidMount();
keys(defaultProps).forEach(propName => {
defineProperty(this, propName, {
get: () => this.props[propName],
set: (value) => {
this.props[propName] = value;
this.componentWillReceiveProps(this.props, this.state);
if (this.shouldComponentUpdate(this.props, this.state)) {
this[$$render]();
}
},
});
});
}
disconnectedCallback() {
this.componentWillUnmount();
}
attributeChangedCallback() {
const nextProps = createProps(this, this.props);
const nextState = createState(this, this.state);
this.componentWillReceiveProps(nextProps, nextState);
if (this.shouldComponentUpdate(nextProps, nextState)) {
this[$$render]();
}
}
/**
* @param {Props} props
* @param {State} state
*
* @returns {boolean}
*/
// @ts-ignore
shouldComponentUpdate(props, state) { return true; }
componentWillMount() {}
componentDidMount() {}
/**
* @param {Props} props
* @param {State} state
*/
// @ts-ignore
componentWillReceiveProps(props, state) {}
/**
* @param {Props} props
* @param {State} state
*
* @returns {void}
*/
// @ts-ignore
componentDidUpdate(props, state) {}
componentWillUnmount() {}
}
// TODO: Can this be done inside the constructor so that class components do not
// need to inherit from HTMLElement?
try {
setPrototypeOf(Component.prototype, HTMLElement.prototype);
} catch {}
// Automatically subscribe the Component middleware.
Component.subscribeMiddleware();