Skip to content

Commit 88dcc80

Browse files
committed
feat(react): add Connector for service, view and controller
1 parent 7d4688d commit 88dcc80

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/react/connector.tsx

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as React from 'react';
2+
import Logger from 'mo/common/logger';
3+
import { IComponent } from './component';
4+
import { Controller } from './controller';
5+
6+
export function connect<S, P, C>(
7+
Service: IComponent,
8+
View: React.ComponentType<P>,
9+
Controller: Controller
10+
) {
11+
return class Connector extends React.Component {
12+
state: { lastUpdated: number };
13+
constructor(props) {
14+
super(props);
15+
this.onChange = this.onChange.bind(this);
16+
this.state = {
17+
lastUpdated: Date.now(),
18+
};
19+
}
20+
21+
componentDidMount() {
22+
Service.onUpdateState(this.onChange);
23+
}
24+
25+
onChange(nextState: S) {
26+
Logger.info(nextState, Service.getState());
27+
this.setState({
28+
lastUpdated: Date.now(),
29+
});
30+
}
31+
32+
render() {
33+
const state = Service.getState();
34+
return (
35+
<View
36+
{...this.state}
37+
{...state}
38+
{...this.props}
39+
{...Controller}
40+
/>
41+
);
42+
}
43+
};
44+
}

0 commit comments

Comments
 (0)