Skip to content

Commit

Permalink
feat: add option for denyList
Browse files Browse the repository at this point in the history
  • Loading branch information
sapkra committed Oct 27, 2020
1 parent c1a93c0 commit 62aae9f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/mainStateSyncEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 All @@ -44,11 +44,16 @@ function createMiddleware(options: MainStateSyncEnhancerOptions) {

export type MainStateSyncEnhancerOptions = {
/**
* Custom store serializaton function. This function is called for each member of the object.
* If a member contains nested objects,
* 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

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

const defaultOptions: MainStateSyncEnhancerOptions = {}
Expand Down
11 changes: 8 additions & 3 deletions src/rendererStateSyncEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ const wrapReducer = (reducer: Reducer) => <S, A extends Action>(
}
}

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

return (next) => (action) => {
if (validateAction(action)) {
if (validateAction(action, options.denyList)) {
ipcRenderer.send('electron-redux.ACTION', action)
}

Expand All @@ -73,6 +73,11 @@ export type RendererStateSyncEnhancerOptions = {
* the nested objects are transformed before the parent object is.
*/
reviver?: (this: unknown, key: string, value: unknown) => unknown

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

const defaultOptions: RendererStateSyncEnhancerOptions = {}
Expand All @@ -93,7 +98,7 @@ export const rendererStateSyncEnhancer = (options = defaultOptions): StoreEnhanc
const store = createStore(
wrapReducer(reducer as any), // TODO: this needs some ❤️
state,
applyMiddleware(middleware)
applyMiddleware(createMiddleware(options))
)

// This is the reason we need to be an enhancer, rather than a middleware.
Expand Down
9 changes: 6 additions & 3 deletions src/utils/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isFSA, FluxStandardAction } from './isFSA'

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

// Gives us just enough action type info to work for the functions below
type ActionMeta = {
Expand All @@ -23,10 +23,13 @@ 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,
denyList: RegExp[] = [],
): action is FluxStandardAction<ActionMeta> => {
return (
isFSA(action) &&
action.meta?.scope !== 'local' &&
blacklist.every((rule) => !rule.test(action.type))
defaultDenyList.concat(denyList).every((rule) => !rule.test(action.type))
)
}

0 comments on commit 62aae9f

Please sign in to comment.