2016-09-06 21:44:15 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-09-06 21:44:15 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-09-06 21:44:15 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2016-09-06 21:44:15 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2016-09-06 21:44:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const AsyncStorage = require('AsyncStorage');
|
|
|
|
const React = require('React');
|
|
|
|
|
|
|
|
export type PassProps<State> = {
|
|
|
|
state: State,
|
|
|
|
setState: (stateLamda: (state: State) => State) => void,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple container for persisting some state and passing it into the wrapped component as
|
|
|
|
* `props.persister.state`. Update it with `props.persister.setState`. The component is initially
|
|
|
|
* rendered using `getInitialState` in the spec and is then re-rendered with the persisted data
|
|
|
|
* once it's fetched.
|
|
|
|
*
|
2017-05-06 03:50:47 +00:00
|
|
|
* This is currently tied to RNTester because it's generally not good to use AsyncStorage like
|
2016-09-06 21:44:15 +00:00
|
|
|
* this in real apps with user data, but we could maybe pull it out for other internal settings-type
|
|
|
|
* usage.
|
|
|
|
*/
|
2016-11-15 04:39:21 +00:00
|
|
|
function createContainer<Props: Object, State>(
|
2017-08-18 01:36:54 +00:00
|
|
|
Component: React.ComponentType<Props & {persister: PassProps<State>}>,
|
2016-09-06 21:44:15 +00:00
|
|
|
spec: {
|
|
|
|
cacheKeySuffix: (props: Props) => string,
|
|
|
|
getInitialState: (props: Props) => State,
|
|
|
|
version?: string,
|
|
|
|
},
|
2017-08-18 01:36:54 +00:00
|
|
|
): React.ComponentType<Props> {
|
2018-05-11 20:32:37 +00:00
|
|
|
return class ComponentWithPersistedState extends React.Component<
|
|
|
|
Props,
|
|
|
|
$FlowFixMeState,
|
|
|
|
> {
|
2017-08-29 21:54:33 +00:00
|
|
|
/* $FlowFixMe(>=0.53.0 site=react_native_fb,react_native_oss) This comment
|
|
|
|
* suppresses an error when upgrading Flow's support for React. To see the
|
|
|
|
* error delete this comment and run Flow. */
|
2018-05-11 20:32:37 +00:00
|
|
|
static displayName = `RNTesterStatePersister(${Component.displayName ||
|
|
|
|
Component.name})`;
|
2016-09-06 21:44:15 +00:00
|
|
|
state = {value: spec.getInitialState(this.props)};
|
2018-05-11 20:32:37 +00:00
|
|
|
_cacheKey = `RNTester:${spec.version || 'v1'}:${spec.cacheKeySuffix(
|
|
|
|
this.props,
|
|
|
|
)}`;
|
2016-09-06 21:44:15 +00:00
|
|
|
componentDidMount() {
|
|
|
|
AsyncStorage.getItem(this._cacheKey, (err, value) => {
|
|
|
|
if (!err && value) {
|
|
|
|
this.setState({value: JSON.parse(value)});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_passSetState = (stateLamda: (state: State) => State): void => {
|
2018-05-11 20:32:37 +00:00
|
|
|
this.setState(state => {
|
2016-09-06 21:44:15 +00:00
|
|
|
const value = stateLamda(state.value);
|
|
|
|
AsyncStorage.setItem(this._cacheKey, JSON.stringify(value));
|
|
|
|
return {value};
|
|
|
|
});
|
|
|
|
};
|
2017-08-18 01:36:54 +00:00
|
|
|
render(): React.Node {
|
2016-09-06 21:44:15 +00:00
|
|
|
return (
|
|
|
|
<Component
|
|
|
|
{...this.props}
|
|
|
|
persister={{
|
|
|
|
state: this.state.value,
|
|
|
|
setState: this._passSetState,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
const RNTesterStatePersister = {
|
2016-09-06 21:44:15 +00:00
|
|
|
createContainer,
|
|
|
|
};
|
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
module.exports = RNTesterStatePersister;
|