Skip to content

Commit

Permalink
Small fix on mergeDeep strategy for persistence rehydration
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlplusb committed Nov 8, 2019
1 parent 16c7d13 commit 38de869
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
43 changes: 43 additions & 0 deletions src/computed-properties.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
import memoizerific from 'memoizerific';
import { get } from './lib';
import { computedSymbol } from './constants';

export function createComputedPropertyBinder(
parentPath,
key,
definition,
computedState,
references,
) {
const computedMeta = definition[computedSymbol];
const memoisedResultFn = memoizerific(1)(definition);
return function createComputedProperty(o) {
Object.defineProperty(o, key, {
configurable: true,
enumerable: true,
get: () => {
let storeState;
if (computedState.isInReducer) {
storeState = computedState.currentState;
} else if (references.getState == null) {
return undefined;
} else {
try {
storeState = references.getState();
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
console.warn('Invalid access attempt to a computed property');
}
return undefined;
}
}
const state = get(parentPath, storeState);
const inputs = computedMeta.stateResolvers.map(resolver =>
resolver(state, storeState),
);
return memoisedResultFn(...inputs);
},
});
};
}

export function createComputedPropertiesMiddleware(references) {
return store => next => action => {
references.internals.computedState.currentState = store.getState();
Expand Down
42 changes: 0 additions & 42 deletions src/computed.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/extract-data-from-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { extractPersistConfig } from './persistence';
import { createActionCreator } from './actions';
import { createThunkHandler, createThunkActionsCreator } from './thunks';
import { bindListenerDefinitions } from './listeners';
import { createComputedPropertyBinder } from './computed';
import { createComputedPropertyBinder } from './computed-properties';

export default function extractDataFromModel(
model,
Expand Down
6 changes: 6 additions & 0 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ export function rehydrateStateFromPersistIfNeeded(
Object.keys(currentNext).forEach(key => {
const data = currentNext[key];
if (typeof data === 'object') {
if (
currentTarget[key] == null ||
typeof currentTarget[key] !== 'object'
) {
currentTarget[key] = {};
}
setAt(currentTarget[key], data);
} else {
currentTarget[key] = data;
Expand Down

0 comments on commit 38de869

Please sign in to comment.