UNPKG

1.24 kBJavaScriptView Raw
1import { Component } from '../component';
2
3/** Retains a pool of Components for re-use, keyed on component name.
4 * Note: since component names are not unique or even necessarily available, these are primarily a form of sharding.
5 * @private
6 */
7const components = {};
8
9
10/** Reclaim a component for later re-use by the recycler. */
11export function collectComponent(component) {
12 let name = component.constructor.name;
13 (components[name] || (components[name] = [])).push(component);
14}
15
16
17/** Create a component. Normalizes differences between PFC's and classful Components. */
18export function createComponent(Ctor, props, context) {
19 let list = components[Ctor.name],
20 inst;
21
22 if (Ctor.prototype && Ctor.prototype.render) {
23 inst = new Ctor(props, context);
24 Component.call(inst, props, context);
25 }
26 else {
27 inst = new Component(props, context);
28 inst.constructor = Ctor;
29 inst.render = doRender;
30 }
31
32
33 if (list) {
34 for (let i=list.length; i--; ) {
35 if (list[i].constructor===Ctor) {
36 inst.nextBase = list[i].nextBase;
37 list.splice(i, 1);
38 break;
39 }
40 }
41 }
42 return inst;
43}
44
45
46/** The `.render()` method for a PFC backing instance. */
47function doRender(props, state, context) {
48 return this.constructor(props, context);
49}