forked from DTStack/molecule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.tsx
163 lines (147 loc) · 5.04 KB
/
connector.tsx
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
import 'reflect-metadata';
import { container } from 'tsyringe';
import React, { Component } from 'react';
import Logger from 'mo/common/logger';
import { IComponent } from './component';
import { Controller } from './controller';
export type ServiceObject = {
[index: string]: IComponent;
};
export type ControllerObject = {
[index: string]: Controller;
};
export function connect<T = any>(
Service: IComponent | ServiceObject,
View: React.ComponentType<any>,
Controller?: Controller | ControllerObject
): React.ComponentType<T> {
return class Connector extends Component<T, any> {
state: { lastUpdated: number };
private _isMounted = false;
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
this.state = {
lastUpdated: Date.now(),
};
}
componentDidMount() {
this._isMounted = true;
this.handleService((service) => {
service.onUpdateState(this.onChange);
});
}
componentWillUnmount() {
this._isMounted = false;
this.handleService((service) => {
service.removeOnUpdateState(this.onChange);
});
}
onChange(prevState, nextState) {
Logger.info(prevState, nextState, (container as any)._registry);
this.update();
}
update = () => {
if (this._isMounted) {
this.setState({
lastUpdated: Date.now(),
});
}
};
getServiceState() {
const target = {};
this.handleService((service, prop) => {
if (prop) {
Object.assign(target, {
[prop]: { ...service.getState() },
});
} else {
Object.assign(target, { ...service.getState() });
}
});
return target;
}
handleService(callback: (service: IComponent, prop?: string) => void) {
if (this.isValidService(Service as IComponent)) {
callback(Service as IComponent);
} else {
for (const name in Service) {
if (name) {
const service: IComponent = Service[name];
if (this.isValidService(service)) {
callback(service, name);
}
}
}
}
}
getControllerProperties() {
const target = {};
if (!Controller) {
return target;
}
this.handleController((controller: Controller, prop) => {
if (prop) {
Object.assign(target, {
[prop]: this.getObjectPublicProperties(controller),
});
} else {
Object.assign(
target,
this.getObjectPublicProperties(controller)
);
}
});
return target;
}
handleController(
callback: (controller: Controller, prop?: string) => void
) {
if (this.isValidController(Controller as Controller)) {
callback(Controller as Controller);
} else {
for (const name in Controller) {
if (name) {
const controller: Controller = Controller[name];
if (this.isValidController(controller)) {
callback(controller, name);
}
}
}
}
}
render() {
return (
<View
{...this.state}
{...this.props}
{...this.getServiceState()}
{...this.getControllerProperties()}
/>
);
}
private isValidService(service: IComponent) {
return typeof service.onUpdateState === 'function';
}
private isValidController(controller: Controller) {
return typeof controller.initView === 'function';
}
private getObjectPublicProperties(obj: Controller) {
const keys = Object.keys(obj);
const result = {};
keys.forEach((key) => {
// Filter the Service, Controller instances and private properties which start with '_'
// TODO There need a better way to filter the private properties of Object,
// maybe we can make use of the # identifier in future.
if (
!key.endsWith('Service') &&
!key.endsWith('Controller') &&
!key.startsWith('_')
) {
result[key] = obj[key];
}
});
return result;
}
};
}