From 0beaf5331d204d4386e719b5340b2302b1b56960 Mon Sep 17 00:00:00 2001 From: Jean-Richard Lai Date: Tue, 5 Jan 2016 20:56:22 -0800 Subject: [PATCH] Add DebugStore --- App/Constants/AppConstants.js | 1 + App/Root.js | 39 +++++++++++++++++---- App/Stores/DebugStore.js | 66 +++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 App/Stores/DebugStore.js diff --git a/App/Constants/AppConstants.js b/App/Constants/AppConstants.js index 2482a78f..fab9771e 100644 --- a/App/Constants/AppConstants.js +++ b/App/Constants/AppConstants.js @@ -13,4 +13,5 @@ module.exports = keyMirror({ POST_ADDED: null, FOLLOW_LIST_UPDATED: null, TEST_COMPONENT_ROUTE: null, + DEBUG_CURRENT_ROUTE_PATH_KEY: null, }); diff --git a/App/Root.js b/App/Root.js index 15650e4c..35a5c37e 100644 --- a/App/Root.js +++ b/App/Root.js @@ -6,6 +6,7 @@ var { var assign = require('object-assign'); var Routes = require('./Navigation/Routes'); + var Launch = require('./Root/Launch'); var LoggedOut = require('./Root/LoggedOut'); var LoggedIn = require('./Root/LoggedIn'); @@ -15,6 +16,8 @@ var TestRunner = require('./Root/TestRunner'); var AppActions = require('./Actions/AppActions'); var CurrentUserStore = require('./Stores/CurrentUserStore'); var EnvironmentStore = require('./Stores/EnvironmentStore'); +var DebugStore = require('./Stores/DebugStore'); + var DispatcherListener = require('./Mixins/DispatcherListener'); function getUserState() { @@ -29,26 +32,33 @@ function getEnvironmentState() { }; }; +function getDebugState() { + return { savedPath: DebugStore.get().currentRoutePath }; +} + var Root = React.createClass({ mixins: [DispatcherListener], getInitialState: function() { return assign({}, getUserState(), - getEnvironmentState() + getEnvironmentState(), + getDebugState() ); }, onUserChange: function() { - var state = getUserState(); - state.routeStack = Routes.parse(null, state.user.isLoggedIn(), true); - this.setState(state); + this.setState(getUserState()); }, onEnvChange: function() { this.setState(getEnvironmentState()); }, + onDebugChange: function() { + this.setState(getDebugState()); + }, + dispatchAction: function(action) { Launcher.launch(this, action); }, @@ -56,20 +66,35 @@ var Root = React.createClass({ componentDidMount: function() { CurrentUserStore.addChangeListener(this.onUserChange); EnvironmentStore.addChangeListener(this.onEnvChange); + DebugStore.addChangeListener(this.onDebugChange); AppActions.appLaunched(); }, componentWillUnmount: function() { + DebugStore.removeChangeListener(this.onDebugChange); EnvironmentStore.removeChangeListener(this.onEnvChange); CurrentUserStore.removeChangeListener(this.onUserChange); }, + getSavedPath: function() { + if (!this.state.environment) { return null; } + if (!this.state.environment.data.simulator) { return null; } + + return this.state.savedPath; + }, + renderContent: function() { if (this.state.routeUnderTest) return null; - - var routeStack = this.state.routeStack; + + var routeStack = Routes.parse(this.getSavedPath(), this.state.user.isLoggedIn(), true); + if(this.state.user.isLoggedIn()) { - return(); + return ( + + ); } else { return(); diff --git a/App/Stores/DebugStore.js b/App/Stores/DebugStore.js new file mode 100644 index 00000000..4863d992 --- /dev/null +++ b/App/Stores/DebugStore.js @@ -0,0 +1,66 @@ +import React from 'react-native'; +import { EventEmitter } from 'events'; + +import Dispatcher from '../Dispatcher'; +import AppConstants from '../Constants/AppConstants'; +import LocalKeyStore from '../Stores/LocalKeyStore'; + +const EnvironmentManager = React.NativeModules.EnvironmentManager; + +const CHANGE_EVENT = 'change'; +const DEBUG_CURRENT_ROUTE_PATH_KEY = AppConstants.DEBUG_CURRENT_ROUTE_PATH_KEY; + +var _values = {}; + +function setCurrentRoutePath(routePath) { + _values.currentRoutePath = routePath; +} + +function resetData() { + _values = {}; +} + +var SingletonStore = Object.assign({}, EventEmitter.prototype, { + get() { + return _values; + }, + + emitChange() { + this.emit(CHANGE_EVENT); + }, + + addChangeListener(callback) { + this.on(CHANGE_EVENT, callback); + }, + + removeChangeListener(callback) { + this.removeListener(CHANGE_EVENT, callback); + } +}); + +Dispatcher.register(function(action) { + switch (action.actionType) { + case AppConstants.APP_LAUNCHED: + LocalKeyStore.getKey(DEBUG_CURRENT_ROUTE_PATH_KEY, (error, routePath) => { + setCurrentRoutePath(routePath); + SingletonStore.emitChange(); + }); + break; + case AppConstants.LAUNCH_ROUTE_PATH: + if (action.routePath) { + LocalKeyStore.setKey(DEBUG_CURRENT_ROUTE_PATH_KEY, action.routePath); + setCurrentRoutePath(action.routePath); + SingletonStore.emitChange(); + } + break; + case AppConstants.LOGOUT_REQUESTED: + resetData(); + LocalKeyStore.setKey(DEBUG_CURRENT_ROUTE_PATH_KEY, ''); + SingletonStore.emitChange(); + break; + default: + // no op + } +}); + +export default SingletonStore;