mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-28 17:00:04 +02:00
global state store
This commit is contained in:
parent
8493b51ec2
commit
e03bdc619f
5 changed files with 95 additions and 12 deletions
13
src/js/__tests__/GlobalStateStore.spec.js
Normal file
13
src/js/__tests__/GlobalStateStore.spec.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
var GlobalStateActions = require('../actions/GlobalStateActions');
|
||||
var GlobalStateStore = require('../stores/GlobalStateStore');
|
||||
|
||||
describe('this store', function() {
|
||||
it('is can change animating', function() {
|
||||
expect(GlobalStateStore.getIsAnimating()).toEqual(false);
|
||||
GlobalStateActions.changeIsAnimating(true);
|
||||
expect(GlobalStateStore.getIsAnimating()).toEqual(true);
|
||||
GlobalStateActions.changeIsAnimating(false);
|
||||
expect(GlobalStateStore.getIsAnimating()).toEqual(false);
|
||||
});
|
||||
|
||||
});
|
19
src/js/actions/GlobalStateActions.js
Normal file
19
src/js/actions/GlobalStateActions.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
"use strict";
|
||||
|
||||
var AppConstants = require('../constants/AppConstants');
|
||||
var AppDispatcher = require('../dispatcher/AppDispatcher');
|
||||
|
||||
var ActionTypes = AppConstants.ActionTypes;
|
||||
|
||||
var GlobalStateActions = {
|
||||
|
||||
changeIsAnimating: function(isAnimating) {
|
||||
AppDispatcher.handleViewAction({
|
||||
type: ActionTypes.CHANGE_IS_ANIMATING,
|
||||
isAnimating: isAnimating
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports = GlobalStateActions;
|
|
@ -2,9 +2,24 @@
|
|||
|
||||
var keyMirror = require('react/lib/keyMirror');
|
||||
|
||||
var CHANGE_EVENT = 'change';
|
||||
|
||||
module.exports = {
|
||||
|
||||
CHANGE_EVENT: CHANGE_EVENT,
|
||||
|
||||
StoreSubscribePrototype: {
|
||||
subscribe: function(cb) {
|
||||
this.on(CHANGE_EVENT, cb);
|
||||
},
|
||||
|
||||
unsubscribe: function(cb) {
|
||||
this.removeListener(CHANGE_EVENT, cb);
|
||||
}
|
||||
},
|
||||
|
||||
ActionTypes: keyMirror({
|
||||
CHANGE_IS_ANIMATING: null,
|
||||
SUBMIT_COMMAND: null,
|
||||
CHANGE_LOCALE: null,
|
||||
CHANGE_LOCALE_FROM_HEADER: null
|
||||
|
|
42
src/js/stores/GlobalStateStore.js
Normal file
42
src/js/stores/GlobalStateStore.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
"use strict";
|
||||
|
||||
var AppConstants = require('../constants/AppConstants');
|
||||
var AppDispatcher = require('../dispatcher/AppDispatcher');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
var assign = require('object-assign');
|
||||
|
||||
var ActionTypes = AppConstants.ActionTypes;
|
||||
|
||||
var _isAnimating = false;
|
||||
|
||||
var GlobalStateStore = assign(
|
||||
{},
|
||||
EventEmitter.prototype,
|
||||
AppConstants.StoreSubscribePrototype,
|
||||
{
|
||||
getIsAnimating: function() {
|
||||
return _isAnimating;
|
||||
},
|
||||
|
||||
dispatchToken: AppDispatcher.register(function(payload) {
|
||||
var action = payload.action;
|
||||
var shouldInform = false;
|
||||
|
||||
switch (action.type) {
|
||||
case ActionTypes.CHANGE_IS_ANIMATING:
|
||||
_isAnimating = action.isAnimating;
|
||||
shouldInform = true;
|
||||
break;
|
||||
case ActionTypes.CHANGE_FLIP_TREE_Y:
|
||||
break;
|
||||
}
|
||||
|
||||
if (shouldInform) {
|
||||
GlobalStateStore.emit(AppConstants.CHANGE_EVENT);
|
||||
}
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
module.exports = GlobalStateStore;
|
|
@ -7,7 +7,6 @@ var EventEmitter = require('events').EventEmitter;
|
|||
var assign = require('object-assign');
|
||||
|
||||
var ActionTypes = AppConstants.ActionTypes;
|
||||
var CHANGE_EVENT = 'change';
|
||||
var DEFAULT_LOCALE = 'en_US';
|
||||
|
||||
// resolve the messy mapping between browser language
|
||||
|
@ -50,8 +49,11 @@ function _getLocaleFromHeader(langString) {
|
|||
}
|
||||
|
||||
var _locale = DEFAULT_LOCALE;
|
||||
var LocaleStore = assign({}, EventEmitter.prototype, {
|
||||
|
||||
var LocaleStore = assign(
|
||||
{},
|
||||
EventEmitter.prototype,
|
||||
AppConstants.StoreSubscribePrototype,
|
||||
{
|
||||
getDefaultLocale: function() {
|
||||
return DEFAULT_LOCALE;
|
||||
},
|
||||
|
@ -64,14 +66,6 @@ var LocaleStore = assign({}, EventEmitter.prototype, {
|
|||
return assign({}, headerLocaleMap);
|
||||
},
|
||||
|
||||
subscribe: function(cb) {
|
||||
this.on(CHANGE_EVENT, cb);
|
||||
},
|
||||
|
||||
unsubscribe: function(cb) {
|
||||
this.removeListener(CHANGE_EVENT, cb);
|
||||
},
|
||||
|
||||
getLocale: function() {
|
||||
return _locale;
|
||||
},
|
||||
|
@ -95,7 +89,7 @@ var LocaleStore = assign({}, EventEmitter.prototype, {
|
|||
}
|
||||
|
||||
if (shouldInform) {
|
||||
LocaleStore.emit(CHANGE_EVENT);
|
||||
LocaleStore.emit(AppConstants.CHANGE_EVENT);
|
||||
}
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue