Skip to content

Commit

Permalink
feat: add option for denyList (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapkra committed Dec 3, 2020
1 parent aefed4e commit aa07110
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ By default, all actions are broadcast to all registered stores. However, some st

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)

### Blacklisted actions
### Blocked actions

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).
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).

## Changelog

Expand Down
2 changes: 1 addition & 1 deletion src/mainStateSyncEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function createMiddleware(options: MainStateSyncEnhancerOptions) {
})

return (next) => (action) => {
if (validateAction(action)) {
if (validateAction(action, options.denyList)) {
webContents.getAllWebContents().forEach((contents) => {
// Ignore chromium devtools
if (contents.getURL().startsWith('devtools://')) return
Expand Down
9 changes: 7 additions & 2 deletions src/options/MainStateSyncEnhancerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export type MainStateSyncEnhancerOptions = {
/**
* Custom store serialization function. This function is called for each member of the object.
* If a member contains nested objects,
* 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

/**
* Custom list for actions that should never replay across stores
*/
denyList?: RegExp[]
}

export const defaultMainOptions: MainStateSyncEnhancerOptions = {}
5 changes: 5 additions & 0 deletions src/options/RendererStateSyncEnhancerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export type RendererStateSyncEnhancerOptions = {
*/
deserializer?: (this: unknown, key: string, value: unknown) => unknown

/**
* Custom list for actions that should never replay across stores
*/
denyList?: RegExp[]

/**
* 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
Expand Down
8 changes: 4 additions & 4 deletions src/rendererStateSyncEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { Action, applyMiddleware, Middleware, StoreCreator, StoreEnhancer } from
import { IPCEvents } from './constants'
import { fetchInitialState, fetchInitialStateAsync } from './fetchState'
import { replaceState, withStoreReplacer } from './fetchState/replaceState'
import { defaultRendererOptions } from './options/RendererStateSyncEnhancerOptions'
import { defaultRendererOptions, RendererStateSyncEnhancerOptions } from './options/RendererStateSyncEnhancerOptions'

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

const middleware: Middleware = (store) => {
const createMiddleware = (options: RendererStateSyncEnhancerOptions): Middleware => (store) => {
// When receiving an action from main
ipcRenderer.on(IPCEvents.ACTION, (_, action: Action) => {
store.dispatch(stopForwarding(action))
})

return (next) => (action) => {
if (validateAction(action)) {
if (validateAction(action, options.denyList)) {
ipcRenderer.send(IPCEvents.ACTION, action)
}

Expand All @@ -39,7 +39,7 @@ export const rendererStateSyncEnhancer = (options = defaultRendererOptions): Sto
const store = createStore(
options.lazyInit ? withStoreReplacer(reducer) : reducer,
initialState,
applyMiddleware(middleware)
applyMiddleware(createMiddleware(options))
)

if (options.lazyInit) {
Expand Down
11 changes: 6 additions & 5 deletions src/utils/actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { isFSA, FluxStandardAction } from './isFSA'

// Certain actions that we should never replay across stores
const blacklist = [/^@@/, /^redux-form/]

// Gives us just enough action type info to work for the functions below
export type ActionMeta = {
scope?: 'local' | string
Expand All @@ -23,10 +20,14 @@ export const stopForwarding = (action: FluxStandardAction<ActionMeta>) => ({
/**
* validateAction ensures that the action meets the right format and isn't scoped locally
*/
export const validateAction = (action: any): action is FluxStandardAction<ActionMeta> => {
export const validateAction = (
action: any,
// Actions that we should never replay across stores
denyList: RegExp[] = [/^@@/, /^redux-form/]
): action is FluxStandardAction<ActionMeta> => {
return (
isFSA(action) &&
action.meta?.scope !== 'local' &&
blacklist.every((rule) => !rule.test(action.type))
denyList.every((rule) => !rule.test(action.type))
)
}

0 comments on commit aa07110

Please sign in to comment.