Skip to content

Commit

Permalink
shave bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 16, 2020
1 parent 2a657c5 commit 3525543
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"no-underscore-dangle": ["error", { "allow": ["__REDUX_DEVTOOLS_EXTENSION__"] }],
"no-bitwise": "off",
"default-case": "off",
"no-param-reassign": "off"
"no-param-reassign": "off",
"symbol-description": "off"
},
"overrides": [{
"files": ["__tests__/**/*"],
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## [Unreleased]
### Changed
- Reduce bundle size a little

## [0.17.0] - 2020-01-09
### Changed
Expand Down
16 changes: 11 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ const useUnstableContextWithoutWarning = (Context, observedBits) => {

const EMPTY_OBJECT = {};

const UPDATE_STATE = Symbol('UPDATE_STATE');
const UPDATE_STATE = (
process.env.NODE_ENV !== 'production' ? Symbol('UPDATE_STATE')
/* for production */ : Symbol()
);

const PROP_UPDATER = 'r';
const PROP_STATE = 'e';

const PROP_GLOBAL_STATE_PROVIDER = 'p';
const PROP_SET_GLOBAL_STATE = 's';
Expand All @@ -49,7 +55,7 @@ const createGlobalStateCommon = (reducer, initialState) => {

const patchedReducer = (state, action) => {
if (action.type === UPDATE_STATE) {
return action.updater(state);
return action[PROP_UPDATER] ? action[PROP_UPDATER](state) : action[PROP_STATE];
}
return reducer(state, action);
};
Expand All @@ -74,7 +80,7 @@ const createGlobalStateCommon = (reducer, initialState) => {
wholeState = state;
} else if (state !== wholeState) {
// wholeState was updated during initialization
dispatch({ type: UPDATE_STATE, updater: () => wholeState });
dispatch({ type: UPDATE_STATE, [PROP_STATE]: wholeState });
}
const cleanup = () => {
listener = null;
Expand Down Expand Up @@ -104,7 +110,7 @@ const createGlobalStateCommon = (reducer, initialState) => {
[name]: updateValue(previousState[name], update),
});
if (listener) {
listener({ type: UPDATE_STATE, updater });
listener({ type: UPDATE_STATE, [PROP_UPDATER]: updater });
} else {
wholeState = updater(wholeState);
}
Expand Down Expand Up @@ -133,7 +139,7 @@ const createGlobalStateCommon = (reducer, initialState) => {

const setWholeState = (state) => {
if (listener) {
listener({ type: UPDATE_STATE, updater: () => state });
listener({ type: UPDATE_STATE, [PROP_STATE]: state });
} else {
wholeState = state;
}
Expand Down

0 comments on commit 3525543

Please sign in to comment.