|
|
@ -6,26 +6,28 @@ let stateCache = {};
|
|
|
|
|
|
|
|
|
|
|
|
type Constructor<T> = new(...args: any[]) => T;
|
|
|
|
type Constructor<T> = new(...args: any[]) => T;
|
|
|
|
|
|
|
|
|
|
|
|
export function historyPersistor<T extends Constructor<React.Component>>(Base: T, tag: string) {
|
|
|
|
export function historyPersistor(tag: string) {
|
|
|
|
return withRouter(class extends Base {
|
|
|
|
return function<T extends Constructor<React.Component>>(Base: T) {
|
|
|
|
constructor(...rest: any[]) {
|
|
|
|
return withRouter(class extends Base {
|
|
|
|
const props = rest[0];
|
|
|
|
constructor(...rest: any[]) {
|
|
|
|
super(...rest);
|
|
|
|
const props = rest[0];
|
|
|
|
const tagName = this.getKeyForTag(props, tag);
|
|
|
|
super(...rest);
|
|
|
|
if (tagName in stateCache) {
|
|
|
|
const tagName = this.getKeyForTag(props, tag);
|
|
|
|
this.state = stateCache[tagName];
|
|
|
|
if (tagName in stateCache) {
|
|
|
|
|
|
|
|
this.state = stateCache[tagName];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (super.componentWillUnmount) {
|
|
|
|
if (super.componentWillUnmount) {
|
|
|
|
super.componentWillUnmount();
|
|
|
|
super.componentWillUnmount();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
stateCache[this.getKeyForTag(this.props, tag)] = this.state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stateCache[this.getKeyForTag(this.props, tag)] = this.state;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getKeyForTag(props: any, tagName: string) {
|
|
|
|
getKeyForTag(props: any, tagName: string) {
|
|
|
|
return props.location.pathname + ':' + tagName;
|
|
|
|
return props.location.pathname + ':' + tagName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|