-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make renderer state initialization synchronous by default (#280)
* feat: make renderer state initialization synchronous by default Implements #278 * code review comments * chore: spelling Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com> * chore: code review comments Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com>
- Loading branch information
1 parent
2e0dbbb
commit 09017fe
Showing
16 changed files
with
185 additions
and
136 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
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,5 @@ | ||
export enum IPCEvents { | ||
INIT_STATE = 'electron-redux.INIT_STATE', | ||
INIT_STATE_ASYNC = 'electron-redux.INIT_STATE_ASYNC', | ||
ACTION = 'electron-redux.ACTION', | ||
} |
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 @@ | ||
import { ipcRenderer } from 'electron' | ||
import { IPCEvents } from '../constants' | ||
import { RendererStateSyncEnhancerOptions } from '../options/RendererStateSyncEnhancerOptions' | ||
|
||
function fetchInitialState<T>(options: RendererStateSyncEnhancerOptions): T { | ||
const state = ipcRenderer.sendSync(IPCEvents.INIT_STATE) | ||
return JSON.parse(state, options.deserializer) | ||
} | ||
|
||
export default fetchInitialState |
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,22 @@ | ||
import { ipcRenderer } from 'electron' | ||
import { IPCEvents } from '../constants' | ||
import { RendererStateSyncEnhancerOptions } from '../options/RendererStateSyncEnhancerOptions' | ||
|
||
async function fetchInitialStateAsync( | ||
options: RendererStateSyncEnhancerOptions, | ||
callback: (state: unknown) => void | ||
): Promise<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 | ||
try { | ||
const state = await ipcRenderer.invoke(IPCEvents.INIT_STATE_ASYNC) | ||
callback(JSON.parse(state, options.deserializer)) | ||
} catch (error) { | ||
console.warn(error) | ||
throw new Error( | ||
'No Redux store found in main process. Did you use the mainStateSyncEnhancer in the MAIN process?' | ||
) | ||
} | ||
} | ||
|
||
export default fetchInitialStateAsync |
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,4 @@ | ||
import fetchInitialState from './fetchInitialState' | ||
import fetchInitialStateAsync from './fetchInitialStateAsync' | ||
|
||
export { fetchInitialState, fetchInitialStateAsync } |
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,32 @@ | ||
import { AnyAction, Reducer } from 'redux' | ||
import { FluxStandardAction } from '../utils/isFSA' | ||
import { ActionMeta } from '../utils' | ||
|
||
interface ReplaceStateAction<S> extends FluxStandardAction<ActionMeta> { | ||
payload: S | ||
} | ||
|
||
const REPLACE_STATE = 'electron-redux.REPLACE_STATE' | ||
|
||
/** | ||
* Creates an action that will replace the current state with the provided | ||
* state. The scope is set to local in this creator function to make sure it is | ||
* never forwarded. | ||
*/ | ||
export const replaceState = <S>(state: S): ReplaceStateAction<S> => ({ | ||
type: REPLACE_STATE, | ||
payload: state, | ||
meta: { scope: 'local' }, | ||
}) | ||
|
||
export const withStoreReplacer = <S, A extends AnyAction>(reducer: Reducer<S, A>) => ( | ||
state: S | undefined, | ||
action: A | ||
): S => { | ||
switch (action.type) { | ||
case REPLACE_STATE: | ||
return (action as any).payload | ||
default: | ||
return reducer(state, action) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type MainStateSyncEnhancerOptions = { | ||
/** | ||
* Custom store serialization 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. | ||
*/ | ||
serializer?: (this: unknown, key: string, value: unknown) => unknown | ||
} | ||
|
||
export const defaultMainOptions: MainStateSyncEnhancerOptions = {} |
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,18 @@ | ||
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. | ||
*/ | ||
deserializer?: (this: unknown, key: string, value: unknown) => unknown | ||
|
||
/** | ||
* By default, the renderer store is initialized from the main store synchronously. | ||
* Since the synchronous fetching of the state is blocking the renderer process until it gets the state | ||
* from the main process, it might be better with huge stores to initialize them in an asynchronous manner, | ||
* by setting this flag to true | ||
*/ | ||
lazyInit?: boolean | ||
} | ||
|
||
export const defaultRendererOptions: RendererStateSyncEnhancerOptions = {} |
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
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
Oops, something went wrong.