Skip to content

Commit

Permalink
refactor(retux): fallback when proxy is not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Jan 10, 2020
1 parent 370f414 commit 4237be3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/retux/src/basic/proxy-action-creators.ts
Expand Up @@ -10,7 +10,8 @@ import { MixedActionCreators, DefaultActionCreator } from '../utils'
* Action Creators are created on first visit
* and be reused on subsequence visits.
*
* Requires modern JS engine which supports `Proxy`.
* Fallback to [[createDefaultActionCreators]]
* if `Proxy` is not supported.
*
* Example
*
Expand Down
18 changes: 16 additions & 2 deletions packages/retux/src/default-action-creators/proxy.ts
Expand Up @@ -4,10 +4,14 @@ import {
DefaultActionCreator,
CreateDefaultActionCreator
} from '../utils'
import { createDefaultActionCreators } from './create'

/**
* Lazy generate Action Creators with Proxy.
*
* Fallback to [[createDefaultActionCreators]]
* if `Proxy` is not supported.
*
* @param createActionCreator Retux basic or fsa `createActionCreator` function.
* @param actionHandlers Retux Action Handlers.
* @param extraActionCreators Overwrite some of the generated
Expand All @@ -24,7 +28,7 @@ export function proxyDefaultActionCreators<
) {
let cachedOwnKeys: Set<string> | undefined

return new Proxy({} as { [key: string]: DefaultActionCreator }, {
const handler: ProxyHandler<{ [key: string]: DefaultActionCreator }> = {
get(memo, type: string) {
if (hasOwnProperty.call(memo, type)) {
return memo[type]
Expand Down Expand Up @@ -68,5 +72,15 @@ export function proxyDefaultActionCreators<
return { value: memo[type], configurable: true, enumerable: true }
}
}
})
}

try {
return new Proxy({}, handler)
} catch (e) {
return createDefaultActionCreators(
createActionCreator,
actionHandlers,
extraActionCreators
)
}
}
9 changes: 5 additions & 4 deletions packages/retux/src/fsa/proxy-action-creators.ts
Expand Up @@ -4,26 +4,27 @@ import { proxyDefaultActionCreators } from '../default-action-creators/proxy'
import { MixedActionCreators, DefaultActionCreator } from '../utils'

/**
* Lazy generate Basic Action Creators with signature:
* Lazy generate Flux Standard Action Creators with signature:
* `(payload?, meta?) => Action`
*
* Action Creators are created on first visit
* and be reused on subsequence visits.
*
* Requires modern JS engine which supports `Proxy`.
* Fallback to [[createDefaultActionCreators]]
* if `Proxy` is not supported.
*
* Example
*
* ```typescript
* const action = proxyActionCreators<ActionCalatog>(actionHandlers)
* const action = proxyActionCreators(actionHandlers)
* dispatch(action.ACTION_NAME)
* dispatch(action.ACTION_NAME) // same action creator
* ```
*
* Rewire `ACTION1` to an alternative Action Creator.
*
* ```typescript
* const action = proxyActionCreators<ActionCatalog>(
* const action = proxyActionCreators(
* actionHandlers,
* {
* ACTION1: () => {}
Expand Down

0 comments on commit 4237be3

Please sign in to comment.