Skip to content

Commit aa07110

Browse files
authored
feat: add option for denyList (#274)
1 parent aefed4e commit aa07110

6 files changed

Lines changed: 25 additions & 14 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ By default, all actions are broadcast to all registered stores. However, some st
7373

7474
To stop an action from propagating from renderer to main store, simply set the scope to local by decorating your action with `stopForwarding` function. Read more about it in the [docs](#todo)
7575

76-
### Blacklisted actions
76+
### Blocked actions
7777

78-
By default, some of the actions are blacklisted from broadcasting / propagating, those include actions starting with `@@` and `redux-form`. The list of ignored actions can be modified with [options](#todo).
78+
By default, some of the actions are blocked from broadcasting / propagating, those include actions starting with `@@` and `redux-form`. The list of ignored actions can be modified with [options](#todo).
7979

8080
## Changelog
8181

src/mainStateSyncEnhancer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function createMiddleware(options: MainStateSyncEnhancerOptions) {
3636
})
3737

3838
return (next) => (action) => {
39-
if (validateAction(action)) {
39+
if (validateAction(action, options.denyList)) {
4040
webContents.getAllWebContents().forEach((contents) => {
4141
// Ignore chromium devtools
4242
if (contents.getURL().startsWith('devtools://')) return
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
export type MainStateSyncEnhancerOptions = {
22
/**
3-
* Custom store serialization function. This function is called for each member of the object.
4-
* If a member contains nested objects,
3+
* Custom store serialization function.
4+
* This function is called for each member of the object. If a member contains nested objects,
55
* the nested objects are transformed before the parent object is.
66
*/
77
serializer?: (this: unknown, key: string, value: unknown) => unknown
8+
9+
/**
10+
* Custom list for actions that should never replay across stores
11+
*/
12+
denyList?: RegExp[]
813
}
914

1015
export const defaultMainOptions: MainStateSyncEnhancerOptions = {}

src/options/RendererStateSyncEnhancerOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export type RendererStateSyncEnhancerOptions = {
66
*/
77
deserializer?: (this: unknown, key: string, value: unknown) => unknown
88

9+
/**
10+
* Custom list for actions that should never replay across stores
11+
*/
12+
denyList?: RegExp[]
13+
914
/**
1015
* By default, the renderer store is initialized from the main store synchronously.
1116
* Since the synchronous fetching of the state is blocking the renderer process until it gets the state

src/rendererStateSyncEnhancer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { Action, applyMiddleware, Middleware, StoreCreator, StoreEnhancer } from
33
import { IPCEvents } from './constants'
44
import { fetchInitialState, fetchInitialStateAsync } from './fetchState'
55
import { replaceState, withStoreReplacer } from './fetchState/replaceState'
6-
import { defaultRendererOptions } from './options/RendererStateSyncEnhancerOptions'
6+
import { defaultRendererOptions, RendererStateSyncEnhancerOptions } from './options/RendererStateSyncEnhancerOptions'
77

88
import { preventDoubleInitialization, stopForwarding, validateAction } from './utils'
99

10-
const middleware: Middleware = (store) => {
10+
const createMiddleware = (options: RendererStateSyncEnhancerOptions): Middleware => (store) => {
1111
// When receiving an action from main
1212
ipcRenderer.on(IPCEvents.ACTION, (_, action: Action) => {
1313
store.dispatch(stopForwarding(action))
1414
})
1515

1616
return (next) => (action) => {
17-
if (validateAction(action)) {
17+
if (validateAction(action, options.denyList)) {
1818
ipcRenderer.send(IPCEvents.ACTION, action)
1919
}
2020

@@ -39,7 +39,7 @@ export const rendererStateSyncEnhancer = (options = defaultRendererOptions): Sto
3939
const store = createStore(
4040
options.lazyInit ? withStoreReplacer(reducer) : reducer,
4141
initialState,
42-
applyMiddleware(middleware)
42+
applyMiddleware(createMiddleware(options))
4343
)
4444

4545
if (options.lazyInit) {

src/utils/actions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { isFSA, FluxStandardAction } from './isFSA'
22

3-
// Certain actions that we should never replay across stores
4-
const blacklist = [/^@@/, /^redux-form/]
5-
63
// Gives us just enough action type info to work for the functions below
74
export type ActionMeta = {
85
scope?: 'local' | string
@@ -23,10 +20,14 @@ export const stopForwarding = (action: FluxStandardAction<ActionMeta>) => ({
2320
/**
2421
* validateAction ensures that the action meets the right format and isn't scoped locally
2522
*/
26-
export const validateAction = (action: any): action is FluxStandardAction<ActionMeta> => {
23+
export const validateAction = (
24+
action: any,
25+
// Actions that we should never replay across stores
26+
denyList: RegExp[] = [/^@@/, /^redux-form/]
27+
): action is FluxStandardAction<ActionMeta> => {
2728
return (
2829
isFSA(action) &&
2930
action.meta?.scope !== 'local' &&
30-
blacklist.every((rule) => !rule.test(action.type))
31+
denyList.every((rule) => !rule.test(action.type))
3132
)
3233
}

0 commit comments

Comments
 (0)