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 5238bce commit d5fe41c
Show file tree
Hide file tree
Showing 3 changed files with 20 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 @@ -25,7 +25,7 @@ function createMiddleware(options: MainStateSyncEnhancerOptions) {
})

return (next) => (action) => {
if (validateAction(action)) {
if (validateAction(action, options.denyList)) {
webContents.getAllWebContents().forEach((contents) => {
contents.send('electron-redux.ACTION', action)
})
Expand All @@ -39,11 +39,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
7 changes: 4 additions & 3 deletions src/utils/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FluxStandardAction, isFSA } from 'flux-standard-action'

// 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 @@ -24,11 +24,12 @@ export const stopForwarding = (action: FluxStandardAction<string, unknown, Actio
* validateAction ensures that the action meets the right format and isn't scoped locally
*/
export const validateAction = (
action: unknown
action: unknown,
denyList: RegExp[] = []
): action is FluxStandardAction<string, unknown, ActionMeta> => {
return (
isFSA<string, unknown, ActionMeta>(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 d5fe41c

Please sign in to comment.