Skip to content

Commit

Permalink
fix: ReadiumCSS / reader state was corrupted (PR #1064 follows PR #1058
Browse files Browse the repository at this point in the history
Fixes #1062)
  • Loading branch information
panaC authored May 26, 2020
1 parent 7ebc3a8 commit dfbd5ef
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/common/redux/actions/reader/setReduxState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ export const ID = "READER_SET_REDUXSTATE";

export interface Payload {
reduxState: Partial<IReaderStateReader>;
publicationIdentifier: string;
winId: string;
}

export function build(publicationIdentifier: string, reduxState: Payload["reduxState"]):
export function build(winId: string, reduxState: Payload["reduxState"]):
Action<typeof ID, Payload> {

return {
type: ID,
payload: {
reduxState,
publicationIdentifier,
winId,
},
};
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/redux/actions/win/session/setReduxState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ export const ID = "WIN_SESSION_SET_REDUXSTATE";
export interface Payload {
reduxState: Partial<IReaderStateReader>;
identifier: string;
publicationIdentifier: string;
}

export function build(id: string, reduxState: Payload["reduxState"]):
export function build(winId: string, pubId: string, reduxState: Payload["reduxState"]):
Action<typeof ID, Payload> {

return {
type: ID,
payload: {
reduxState,
identifier: id,
identifier: winId,
publicationIdentifier: pubId,
},
};
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/redux/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import { readerActions } from "readium-desktop/common/redux/actions";
import { i18nReducer } from "readium-desktop/common/redux/reducers/i18n";
import { keyboardReducer } from "readium-desktop/common/redux/reducers/keyboard";
// import { netReducer } from "readium-desktop/common/redux/reducers/net";
Expand All @@ -16,7 +15,7 @@ import { RootState } from "readium-desktop/main/redux/states";
import { priorityQueueReducer } from "readium-desktop/utils/redux-reducers/pqueue.reducer";
import { combineReducers } from "redux";

import { publicationActions } from "../actions";
import { publicationActions, winActions } from "../actions";
import { lcpReducer } from "./lcp";
import { readerDefaultConfigReducer } from "./reader/defaultConfig";
import { sessionReducer } from "./session";
Expand Down Expand Up @@ -49,12 +48,12 @@ export const rootReducer = combineReducers<RootState>({
publication: combineReducers({
lastReadingQueue: priorityQueueReducer
<
readerActions.setReduxState.TAction,
winActions.session.setReduxState.TAction,
publicationActions.deletePublication.TAction
>(
{
push: {
type: readerActions.setReduxState.ID,
type: winActions.session.setReduxState.ID,
selector: (action) =>
[(new Date()).getTime(), action.payload.publicationIdentifier],
},
Expand Down
15 changes: 11 additions & 4 deletions src/main/redux/reducers/win/session/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import { IReaderStateReader } from "readium-desktop/common/redux/states/renderer/readerRootState";
import { winActions } from "readium-desktop/main/redux/actions";
import {
IDictWinSessionReaderState,
Expand Down Expand Up @@ -84,16 +85,22 @@ export function winSessionReaderReducer(
const id = action.payload.identifier;

if (state[id]) {

const reduxState: IReaderStateReader = { ...state[id].reduxState };
Object.entries(action.payload.reduxState).forEach(([key, value]) => {
if (value) {
// @ts-ignore
reduxState[key] = value;
}
});

return {
...state,
...{
[id]: {
...state[id],
...{
reduxState: {
...state[id].reduxState,
...action.payload.reduxState,
},
reduxState,
},
},
},
Expand Down
7 changes: 5 additions & 2 deletions src/main/redux/sagas/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,11 @@ function* readerCloseRequest(identifier?: string) {

function* readerSetReduxState(action: readerActions.setReduxState.TAction) {

const { publicationIdentifier, reduxState } = action.payload;
yield put(winActions.session.setReduxState.build(publicationIdentifier, reduxState));
const { winId, reduxState } = action.payload;

const pubId = yield* selectTyped(
(state: RootState) => state?.win?.session?.reader[winId]?.reduxState?.info?.publicationIdentifier);
yield put(winActions.session.setReduxState.build(winId, pubId, reduxState));
}

export function saga() {
Expand Down
18 changes: 13 additions & 5 deletions src/renderer/reader/redux/middleware/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const dispatchSetReduxState = (

const state = store.getState();
store.dispatch(
readerActions.setReduxState.build(state.reader.info.publicationIdentifier, readerState),
readerActions.setReduxState.build(state.win.identifier, readerState),
);
};

Expand All @@ -33,16 +33,24 @@ export const reduxPersistMiddleware: Middleware

const nextState = store.getState();

const readerState: Partial<IReaderStateReader> = {};
let dispatchFlag = false;
if (!ramda.equals(prevState.reader.config, nextState.reader.config)) {

dispatchSetReduxState(store, { config: nextState.reader.config });
readerState.config = nextState.reader.config;
dispatchFlag = true;
}
if (!ramda.equals(prevState.reader.locator, nextState.reader.locator)) {

} else if (!ramda.equals(prevState.reader.locator, nextState.reader.locator)) {
readerState.locator = nextState.reader.locator;
dispatchFlag = true;
}
if (dispatchFlag) {

dispatchSetReduxState(store, { locator: nextState.reader.locator });
dispatchSetReduxState(store, readerState);
}

// readerInfo is readOnly no need to persist
// readerInfo is readOnly no need to persist it

return returnValue;
};

0 comments on commit dfbd5ef

Please sign in to comment.