Skip to content

Commit

Permalink
Add debug as an option to piling.importState(state, options)
Browse files Browse the repository at this point in the history
  • Loading branch information
flekschas committed Feb 16, 2021
1 parent 67e394f commit 2c45cee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## Next

_[Changes since v0.7.12](https://github.com/flekschas/piling.js/compare/v0.7.12...master)_
_[Changes since v0.7.13](https://github.com/flekschas/piling.js/compare/v0.7.13...master)_

## v0.7.13

- Add `debug` as an option to `piling.importState(state, options)` and `createLibraryFromState` to help discover issues with broken state.

_[Changes since v0.7.12](https://github.com/flekschas/piling.js/compare/v0.7.12...v0.7.13)_

## v0.7.12

Expand Down
10 changes: 5 additions & 5 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -4057,7 +4057,7 @@ const createPilingJs = (rootElement, initProps = {}) => {

const importState = (
newState,
{ deserialize = false, overwriteState = false } = {}
{ deserialize = false, overwriteState = false, debug = false } = {}
) => {
// We assume that the user imports an already initialized state
isInitialPositioning = false;
Expand All @@ -4067,10 +4067,10 @@ const createPilingJs = (rootElement, initProps = {}) => {
if (action.type.indexOf('OVERWRITE') >= 0) resolve();
});
});
store.import(
deserialize ? deserializeState(newState) : newState,
overwriteState
);
store.import(deserialize ? deserializeState(newState) : newState, {
overwriteState,
debug,
});
resetPileBorder();
whenUpdated.then(() => {
pubSub.unsubscribe(updateUnsubscriber);
Expand Down
43 changes: 34 additions & 9 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ export const reset = () => ({
payload: {},
});

export const overwrite = (newState) => ({
export const overwrite = (newState, debug) => ({
type: 'OVERWRITE',
payload: { newState },
payload: { newState, debug },
});

export const softOverwrite = (newState) => ({
export const softOverwrite = (newState, debug) => ({
type: 'SOFT_OVERWRITE',
payload: { newState },
payload: { newState, debug },
});

const [arrangementType, setArrangementType] = setter('arrangementType');
Expand Down Expand Up @@ -665,7 +665,7 @@ const [showSpatialIndex, setShowSpatialIndex] = setter(
const createStore = () => {
let lastAction = null;

const appReducer = combineReducers({
const reducers = {
arrangementObjective,
arrangementOptions,
arrangementType,
Expand Down Expand Up @@ -774,16 +774,38 @@ const createStore = () => {
temporaryDepiledPiles,
zoomBounds,
zoomScale,
});
};

const appReducer = combineReducers(reducers);

const warnForUnknownImportProps = (newState) => {
const unknownProps = Object.keys(newState)
.reduce((unknown, prop) => {
if (reducers[prop] === undefined) {
unknown.push(`"${prop}"`);
}
return unknown;
}, [])
.join(', ');
if (unknownProps) {
console.warn(
`The following state properties are not understood and will not imported: ${unknownProps}`
);
}
};

const rootReducer = (state, action) => {
lastAction = action;

if (action.type === 'RESET') {
state = undefined; // eslint-disable-line no-param-reassign
} else if (action.type === 'OVERWRITE') {
if (action.payload.debug)
warnForUnknownImportProps(action.payload.newState);
state = action.payload.newState; // eslint-disable-line no-param-reassign
} else if (action.type === 'SOFT_OVERWRITE') {
if (action.payload.debug)
warnForUnknownImportProps(action.payload.newState);
// eslint-disable-next-line no-param-reassign
state = update(state, action.payload.newState, true);
}
Expand All @@ -805,7 +827,10 @@ const createStore = () => {
return clonedState;
};

const importState = (newState, overwriteState = false) => {
const importState = (
newState,
{ overwriteState = false, debug = false } = {}
) => {
if (newState.version !== version) {
console.warn(
`The version of the imported state "${newState.version}" doesn't match the library version "${version}". Use at your own risk!`
Expand All @@ -814,8 +839,8 @@ const createStore = () => {

if (newState.version) delete newState.version;

if (overwriteState) reduxStore.dispatch(overwrite(newState));
else reduxStore.dispatch(softOverwrite(newState));
if (overwriteState) reduxStore.dispatch(overwrite(newState, debug));
else reduxStore.dispatch(softOverwrite(newState, debug));
};

const resetState = () => {
Expand Down

0 comments on commit 2c45cee

Please sign in to comment.