From bf6f9a723f94b967f56196a9a711518ecd0b8e01 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Thu, 18 Jun 2015 17:20:43 -0700 Subject: [PATCH] [added] NativeHistory -wip --- modules/NativeHistory.js | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 modules/NativeHistory.js diff --git a/modules/NativeHistory.js b/modules/NativeHistory.js new file mode 100644 index 0000000000..70639b7e1b --- /dev/null +++ b/modules/NativeHistory.js @@ -0,0 +1,69 @@ +import { AsyncStorage, PropTypes } from 'react-native'; +import MemoryHistory from './MemoryHistory'; +import Location from './Location'; + +var { bool, string } = PropTypes; + +function encodeState(state) { + return JSON.stringify(state); +} + +function decodeState(string) { + var state; + try { + state = JSON.parse(string); + } catch (error) { + // Invalid JSON in AsyncStorage for some reason. Ignore it. + state = {}; + } + + // Make sure we have a real Location. + if (state && state.location) { + var { location } = state; + state.location = new Location(location.path, location.query, location.navigationType); + } + + return state; +} + +/** + * A history implementation for React Native environments that + * supports persistence across the application lifecycle using + * the AsyncStorage module. + */ +class NativeHistory extends MemoryHistory { + + static propTypes = Object.assign({}, MemoryHistory.propTypes, { + storageKey: string.isRequired, + autoSave: bool.isRequired + }); + + static defaultProps = Object.assign({}, MemoryHistory.defaultProps, { + storageKey: '@ReactRouterNativeHistory', + autoSave: true + }); + + static childContextTypes = Object.assign({}, MemoryHistory.childContextTypes); + + componentWillMount() { + AsyncStorage.getItem(this.props.storageKey, (error, value) => { + if (error) { + throw error; // TODO: Keep this around in state? + } else { + this.setState(decodeState(value)); + } + }); + } + + componentDidUpdate() { + if (this.props.autoSave) + this.saveToAsyncStorage(); + } + + saveToAsyncStorage(callback) { + AsyncStorage.setItem(this.props.storageKey, encodeState(this.state), callback); + } + +} + +export default NativeHistory;