2018-12-02 19:27:22 +03:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import Store from './Store.js';
|
2016-10-01 17:21:32 +03:00
|
|
|
|
|
|
|
// "react-redux connect()"-like example
|
2018-12-02 19:27:22 +03:00
|
|
|
class StoreListener extends React.PureComponent
|
|
|
|
{
|
|
|
|
componentDidMount()
|
2016-10-01 17:21:32 +03:00
|
|
|
{
|
|
|
|
Store.on(this.update);
|
2018-12-02 19:27:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount()
|
2016-10-01 17:21:32 +03:00
|
|
|
{
|
|
|
|
Store.un(this.update);
|
2018-12-02 19:27:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
update = () =>
|
2016-10-01 17:21:32 +03:00
|
|
|
{
|
2018-12-02 19:27:22 +03:00
|
|
|
var newState = this.mapStateToProps(Store.data);
|
2016-10-01 17:21:32 +03:00
|
|
|
for (var i in newState)
|
|
|
|
{
|
|
|
|
if (this.state[i] != newState[i])
|
|
|
|
{
|
|
|
|
this.setState(newState);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 19:27:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render()
|
2016-10-01 17:21:32 +03:00
|
|
|
{
|
2018-12-02 19:27:22 +03:00
|
|
|
var props = { ...this.initial, ...this.props, ...this.state };
|
|
|
|
return React.createElement(this.wrappedComponent, props);
|
2016-10-01 17:21:32 +03:00
|
|
|
}
|
2018-12-02 19:27:22 +03:00
|
|
|
}
|
2016-10-01 17:21:32 +03:00
|
|
|
|
2018-12-02 19:27:22 +03:00
|
|
|
export default function(component, map, initial)
|
2016-10-01 17:21:32 +03:00
|
|
|
{
|
2018-12-02 19:27:22 +03:00
|
|
|
var cl = class extends StoreListener
|
|
|
|
{
|
|
|
|
constructor(props, context, updater)
|
|
|
|
{
|
|
|
|
super(props, context, updater);
|
|
|
|
this.wrappedComponent = component;
|
|
|
|
this.mapStateToProps = map;
|
|
|
|
this.initial = initial;
|
|
|
|
this.state = map(Store.data);
|
|
|
|
this.update = this.update.bind(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return cl;
|
2016-10-01 17:21:32 +03:00
|
|
|
};
|