Skip to content
Myr

feat(devtool): inferred action type (#2987)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(devtools middleware) add automatic action name finding * docs(readme) add inferActionName example * feat: update readmes * feat: update devtools middleware and tests * feat: remove inferActionName ---------

authored by Ali Mert Çakar · committed by GitHub · parent 6953c29 · Browse files · 3 files+48 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
  • Co-authored-by: Danilo Britto <dbritto.dev@gmail.com>

Diff stats

3 files+48 added−3 removed

  • tests:+31 added−0 removed
  • other:+17 added−3 removed

3 files+48 added−3 removed

modified:docs/middlewares/devtools.md+2 added-2 removed
56 unmodified lines
57 - **optional** `name`: A custom identifier for the connection in the Redux DevTools.58 - **optional** `enabled`: Defaults to `true` when is on development mode, and defaults to `false`59 when is on production mode. Enables or disables the Redux DevTools integration60 for this store.61 - **optional** `anonymousActionType`: Defaults to `anonymous`. A string to use as the action type62 for anonymous mutations in the Redux DevTools.61 - **optional** `anonymousActionType`: Defaults to the inferred action type or `anonymous` if62 unavailable. A string to use as the action type for anonymous mutations in the Redux DevTools.63 - **optional** `store`: A custom identifier for the store in the Redux DevTools.6465#### Returns66
227 unmodified lines
modified:src/middleware/devtools.ts+15 added-1 removed
1import type {} from '@redux-devtools/extension'23import type {4 StateCreator,5 StoreApi,6 StoreMutatorIdentifier,
105 unmodified lines
112type ConnectionInformation = {113 connection: Connection114 stores: Record<StoreName, StoreInformation>115}116117const trackedConnections: Map<ConnectionName, ConnectionInformation> = new Map()118119const getTrackedConnectionState = (120 name: string | undefined,
42 unmodified lines
163 trackedConnections.delete(name)164 }165}166167const findCallerName = (stack: string | undefined) => {168 if (!stack) return undefined169 const traceLines = stack.split('\n')170 const apiSetStateLineIndex = traceLines.findIndex((traceLine) =>171 traceLine.includes('api.setState'),172 )173 if (apiSetStateLineIndex < 0) return undefined174 const callerLine = traceLines[apiSetStateLineIndex + 1]?.trim() || ''175 return /.+ (.+) .+/.exec(callerLine)?.[1]176}177178const devtoolsImpl: DevtoolsImpl =179 (fn, devtoolsOptions = {}) =>180 (set, get, api) => {181 const { enabled, anonymousActionType, store, ...options } = devtoolsOptions
24 unmodified lines
206 let isRecording = true207 ;(api.setState as any) = ((state, replace, nameOrAction: Action) => {208 const r = set(state, replace as any)209 if (!isRecording) return r210 const inferredActionType = findCallerName(new Error().stack)211 const action: { type: string } =212 nameOrAction === undefined199 ? { type: anonymousActionType || 'anonymous' }213 ? { type: anonymousActionType || inferredActionType || 'anonymous' }214 : typeof nameOrAction === 'string'215 ? { type: nameOrAction }216 : nameOrAction217 if (store === undefined) {
210 unmodified lines
modified:tests/devtools.test.tsx+31 added
191 unmodified lines
192 )193 })194})195196describe('When state changes with automatic setter inferring...', () => {197 it("sends { type: setStateName || 'setCount`, ...rest } as the action with current state", async () => {198 const options = {199 name: 'testOptionsName',200 enabled: true,201 }202203 const api = createStore<{204 count: number205 setCount: (count: number) => void206 }>()(207 devtools(208 (set) => ({209 count: 0,210 setCount: (newCount: number) => {211 set({ count: newCount })212 },213 }),214 options,215 ),216 )217218 api.getState().setCount(10)219 const [connection] = getNamedConnectionApis(options.name)220 expect(connection.send).toHaveBeenLastCalledWith(221 { type: 'Object.setCount' },222 { count: 10, setCount: expect.any(Function) },223 )224 })225})226227describe('when it receives a message of type...', () => {228 describe('ACTION...', () => {229 it('does nothing', async () => {230 const initialState = { count: 0 }
2292 unmodified lines