1 | import { Component } from '../component';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const components = {};
|
8 |
|
9 |
|
10 |
|
11 | export function collectComponent(component) {
|
12 | let name = component.constructor.name;
|
13 | (components[name] || (components[name] = [])).push(component);
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 | export 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 |
|
47 | function doRender(props, state, context) {
|
48 | return this.constructor(props, context);
|
49 | }
|