-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove default custom serialization and make it an opt-in option
Removing custom serializer as a default, since its not alligned with redux recomendation about store & serialization fix #261
- Loading branch information
1 parent
4911227
commit 20b6cab
Showing
14 changed files
with
121 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ es | |
types | ||
*.log | ||
e2e_dist | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
node_modules | ||
coverage | ||
*.log | ||
e2e_dist | ||
src | ||
tests | ||
.github | ||
.vscode | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { syncMain } from './syncMain' | ||
import { mainStateSyncEnhancer } from './mainStateSyncEnhancer' | ||
import { stopForwarding } from './utils' | ||
import { syncRenderer } from './syncRenderer' | ||
import { rendererStateSyncEnhancer } from './rendererStateSyncEnhancer' | ||
|
||
export { syncMain, syncRenderer, stopForwarding } | ||
export { mainStateSyncEnhancer, rendererStateSyncEnhancer, stopForwarding } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ipcMain, webContents } from 'electron' | ||
import { Action, applyMiddleware, Middleware, StoreCreator, StoreEnhancer } from 'redux' | ||
|
||
import { preventDoubleInitialization, stopForwarding, validateAction } from './utils' | ||
|
||
function createMiddleware(options: MainStateSyncEnhancerOptions) { | ||
const middleware: Middleware = (store) => { | ||
ipcMain.handle('electron-redux.INIT_STATE', async () => { | ||
// Serialize the initial state using custom replacer | ||
return JSON.stringify(store.getState(), options.replacer) | ||
}) | ||
|
||
// When receiving an action from a renderer | ||
ipcMain.on('electron-redux.ACTION', (event, action: Action) => { | ||
const localAction = stopForwarding(action) | ||
store.dispatch(localAction) | ||
|
||
// Forward it to all of the other renderers | ||
webContents.getAllWebContents().forEach((contents) => { | ||
// Ignore the renderer that sent the action | ||
if (contents.id !== event.sender.id) { | ||
contents.send('electron-redux.ACTION', localAction) | ||
} | ||
}) | ||
}) | ||
|
||
return (next) => (action) => { | ||
if (validateAction(action)) { | ||
webContents.getAllWebContents().forEach((contents) => { | ||
contents.send('electron-redux.ACTION', action) | ||
}) | ||
} | ||
|
||
return next(action) | ||
} | ||
} | ||
return middleware | ||
} | ||
|
||
export type MainStateSyncEnhancerOptions = { | ||
/** | ||
* Custom store serializaton function. This function is called for each member of the object. | ||
* If a member contains nested objects, | ||
* the nested objects are transformed before the parent object is. | ||
*/ | ||
replacer?: (this: unknown, key: string, value: unknown) => unknown | ||
} | ||
|
||
const defaultOptions: MainStateSyncEnhancerOptions = {} | ||
|
||
/** | ||
* Creates new instance of main process redux enhancer. | ||
* @param {MainStateSyncEnhancerOptions} options Additional enhancer options | ||
* @returns StoreEnhancer | ||
*/ | ||
export const mainStateSyncEnhancer = (options = defaultOptions): StoreEnhancer => ( | ||
createStore: StoreCreator | ||
) => { | ||
preventDoubleInitialization() | ||
const middleware = createMiddleware(options) | ||
return (reducer, state) => { | ||
return createStore(reducer, state, applyMiddleware(middleware)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './actions' | ||
export * from './json' | ||
export * from './misc' |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.