New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: remove default custom serialization and make it an opt-in option #272
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import { ipcRenderer } from 'electron' | ||
import { Action, applyMiddleware, Middleware, Reducer, StoreCreator, StoreEnhancer } from 'redux' | ||
|
||
import { hydrate, preventDoubleInitialization, stopForwarding, validateAction } from './utils' | ||
import { preventDoubleInitialization, stopForwarding, validateAction } from './utils' | ||
|
||
async function getRendererState(callback: (state: unknown) => void) { | ||
async function fetchInitialState( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice naming |
||
options: RendererStateSyncEnhancerOptions, | ||
callback: (state: unknown) => void | ||
) { | ||
// Electron will throw an error if there isn't a handler for the channel. | ||
// We catch it so that we can throw a more useful error | ||
const state = await ipcRenderer.invoke('electron-redux.FETCH_STATE').catch((error) => { | ||
console.error(error) | ||
throw new Error('No Redux store found in main process. Did you use the syncMain enhancer?') | ||
const state = await ipcRenderer.invoke('electron-redux.INIT_STATE').catch((error) => { | ||
console.warn(error) | ||
throw new Error( | ||
'No Redux store found in main process. Did you use the mainStateSyncEnhancer in the MAIN process?' | ||
) | ||
}) | ||
|
||
// We do some fancy hydration on certain types like Map and Set. | ||
// See also `freeze` | ||
callback(JSON.parse(state, hydrate)) | ||
callback(JSON.parse(state, options.reviver)) | ||
} | ||
|
||
/** | ||
|
@@ -61,7 +66,27 @@ const middleware: Middleware = (store) => { | |
} | ||
} | ||
|
||
export const syncRenderer: StoreEnhancer = (createStore: StoreCreator) => { | ||
export type RendererStateSyncEnhancerOptions = { | ||
/** | ||
* Custom function used during de-serialization of the redux store to transform the object. | ||
* 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. | ||
*/ | ||
reviver?: (this: unknown, key: string, value: unknown) => unknown | ||
} | ||
|
||
const defaultOptions: RendererStateSyncEnhancerOptions = {} | ||
|
||
/** | ||
* Creates new instance of renderer process redux enhancer. | ||
* Upon initialization, it will fetch the state from the main process & subscribe for event | ||
* communication required to keep the actions in sync. | ||
* @param {RendererStateSyncEnhancerOptions} options Additional settings for enhancer | ||
* @returns StoreEnhancer | ||
*/ | ||
export const rendererStateSyncEnhancer = (options = defaultOptions): StoreEnhancer => ( | ||
createStore: StoreCreator | ||
) => { | ||
preventDoubleInitialization() | ||
|
||
return (reducer, state) => { | ||
|
@@ -74,7 +99,7 @@ export const syncRenderer: StoreEnhancer = (createStore: StoreCreator) => { | |
// This is the reason we need to be an enhancer, rather than a middleware. | ||
// We use this (along with the wrapReducer function above) to dispatch an | ||
// action that initializes the store without needing to fetch it synchronously. | ||
getRendererState((state) => { | ||
fetchInitialState(options, (state) => { | ||
store.dispatch(replaceState(state)) | ||
}) | ||
|
||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍