Skip to content

Add DebugStore #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions App/Constants/AppConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ module.exports = keyMirror({
POST_ADDED: null,
FOLLOW_LIST_UPDATED: null,
TEST_COMPONENT_ROUTE: null,
DEBUG_CURRENT_ROUTE_PATH_KEY: null,
});
39 changes: 32 additions & 7 deletions App/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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() {
Expand All @@ -29,47 +32,69 @@ 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);
},

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(<LoggedIn ref="current" routeStack={routeStack} />);
return (
<LoggedIn
ref="current"
routeStack={routeStack}
/>
);
}
else {
return(<LoggedOut ref="current" routeStack={routeStack} />);
Expand Down
66 changes: 66 additions & 0 deletions App/Stores/DebugStore.js
Original file line number Diff line number Diff line change
@@ -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;