Skip to content

Commit

Permalink
Merge pull request #3904 from reduxjs/redux5-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Nov 23, 2023
2 parents 6778062 + eeca80a commit 3463225
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 119 deletions.
11 changes: 0 additions & 11 deletions docs/api/matching-utilities.mdx
Expand Up @@ -13,7 +13,6 @@ Redux Toolkit exports several type-safe action matching utilities that you can l

### General Purpose

- [`isAction`](#isaction) - returns true if a passed value is a standard action object, with a type string
- [`isAllOf`](#isallof) - returns true when **all** conditions are met
- [`isAnyOf`](#isanyof) - returns true when **at least one of** the conditions are met

Expand All @@ -27,16 +26,6 @@ All these matchers can either be called with one or more thunks as arguments, in
- [`isRejected`](#isrejected) - accepts one or more action creators and returns true when all match
- [`isRejectedWithValue`](#isrejectedwithvalue) - accepts one or more action creators and returns true when all match

## `isAction`

A type guard that takes an unknown variable, and returns `true` if it's a standard Redux action with a string `type` property.

:::caution

As of Redux 5.0, action types are _required_ to be a string.

:::

## `isAllOf`

A higher-order function that accepts one or more of:
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Expand Up @@ -112,7 +112,7 @@
],
"dependencies": {
"immer": "^10.0.3",
"redux": "^5.0.0-rc.0",
"redux": "^5.0.0-rc.1",
"redux-thunk": "^3.0.0-rc.0",
"reselect": "^5.0.0-beta.1"
},
Expand Down
9 changes: 7 additions & 2 deletions packages/toolkit/src/configureStore.ts
Expand Up @@ -7,11 +7,16 @@ import type {
Store,
UnknownAction,
} from 'redux'
import { applyMiddleware, createStore, compose, combineReducers } from 'redux'
import {
applyMiddleware,
createStore,
compose,
combineReducers,
isPlainObject,
} from 'redux'
import type { DevToolsEnhancerOptions as DevToolsOptions } from './devtoolsExtension'
import { composeWithDevTools } from './devtoolsExtension'

import isPlainObject from './isPlainObject'
import type {
ThunkMiddlewareFor,
GetDefaultMiddleware,
Expand Down
20 changes: 4 additions & 16 deletions packages/toolkit/src/createAction.ts
@@ -1,12 +1,11 @@
import type { Action, UnknownAction } from 'redux'
import { isAction } from 'redux'
import type {
IsUnknownOrNonInferrable,
IfMaybeUndefined,
IfVoid,
IsAny,
} from './tsHelpers'
import { hasMatchFunction } from './tsHelpers'
import isPlainObject from './isPlainObject'

/**
* An action with a string type and an associated payload. This is the
Expand Down Expand Up @@ -84,7 +83,7 @@ export type _ActionCreatorWithPreparedPayload<
*/
export interface BaseActionCreator<P, T extends string, M = never, E = never> {
type: T
match: (action: Action<string>) => action is PayloadAction<P, T, M, E>
match: (action: unknown) => action is PayloadAction<P, T, M, E>
}

/**
Expand Down Expand Up @@ -279,23 +278,12 @@ export function createAction(type: string, prepareAction?: Function): any {

actionCreator.type = type

actionCreator.match = (action: Action<string>): action is PayloadAction =>
action.type === type
actionCreator.match = (action: unknown): action is PayloadAction =>
isAction(action) && action.type === type

return actionCreator
}

/**
* Returns true if value is a plain object with a `type` property.
*/
export function isAction(action: unknown): action is Action<string> {
return (
isPlainObject(action) &&
'type' in action &&
typeof (action as Record<'type', unknown>).type === 'string'
)
}

/**
* Returns true if value is an RTK-like action creator, with a static type property and match method.
*/
Expand Down
8 changes: 2 additions & 6 deletions packages/toolkit/src/dynamicMiddleware/index.ts
Expand Up @@ -4,7 +4,7 @@ import type {
UnknownAction,
} from 'redux'
import { compose } from 'redux'
import { createAction, isAction } from '../createAction'
import { createAction } from '../createAction'
import { isAllOf } from '../matchers'
import { nanoid } from '../nanoid'
import { emplace, find } from '../utils'
Expand Down Expand Up @@ -75,11 +75,7 @@ export const createDynamicMiddleware = <
return compose(...appliedMiddleware)
}

const isWithMiddleware = isAllOf(
isAction,
withMiddleware,
matchInstance(instanceId)
)
const isWithMiddleware = isAllOf(withMiddleware, matchInstance(instanceId))

const middleware: DynamicMiddleware<State, Dispatch> =
(api) => (next) => (action) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/toolkit/src/dynamicMiddleware/tests/index.test.ts
Expand Up @@ -2,7 +2,6 @@ import type { Middleware } from 'redux'
import { createDynamicMiddleware } from '../index'
import { configureStore } from '../../configureStore'
import type { BaseActionCreator, PayloadAction } from '../../createAction'
import { isAction } from '../../createAction'
import { createAction } from '../../createAction'
import { isAllOf } from '../../matchers'

Expand All @@ -25,7 +24,7 @@ export const makeProbeableMiddleware = <Id extends number>(
): Middleware<{
(action: PayloadAction<Id, typeof probeType>): Id
}> => {
const isMiddlewareAction = isAllOf(isAction, probeMiddleware, matchId(id))
const isMiddlewareAction = isAllOf(probeMiddleware, matchId(id))
return (api) => (next) => (action) => {
if (isMiddlewareAction(action)) {
return id
Expand Down
3 changes: 0 additions & 3 deletions packages/toolkit/src/index.ts
Expand Up @@ -42,7 +42,6 @@ export type { DevToolsEnhancerOptions } from './devtoolsExtension'
export {
// js
createAction,
isAction,
isActionCreator,
isFSA as isFluxStandardAction,
} from './createAction'
Expand Down Expand Up @@ -157,8 +156,6 @@ export type {

export { nanoid } from './nanoid'

export { default as isPlainObject } from './isPlainObject'

export type {
ListenerEffect,
ListenerMiddleware,
Expand Down
23 changes: 0 additions & 23 deletions packages/toolkit/src/isPlainObject.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/toolkit/src/listenerMiddleware/index.ts
@@ -1,6 +1,7 @@
import type { Action, Dispatch, MiddlewareAPI, UnknownAction } from 'redux'
import { isAction } from 'redux'
import type { ThunkDispatch } from 'redux-thunk'
import { createAction, isAction } from '../createAction'
import { createAction } from '../createAction'
import { nanoid } from '../nanoid'

import type {
Expand Down
3 changes: 1 addition & 2 deletions packages/toolkit/src/serializableStateInvariantMiddleware.ts
@@ -1,7 +1,6 @@
import isPlainObject from './isPlainObject'
import type { Middleware } from 'redux'
import { isAction, isPlainObject } from 'redux'
import { getTimeMeasureUtils } from './utils'
import { isAction } from './createAction'

/**
* Returns true if the passed value is "plain", i.e. a value that is either
Expand Down
23 changes: 1 addition & 22 deletions packages/toolkit/src/tests/createAction.test.ts
@@ -1,4 +1,4 @@
import { createAction, isAction, isActionCreator } from '@reduxjs/toolkit'
import { createAction, isActionCreator } from '@reduxjs/toolkit'

describe('createAction', () => {
it('should create an action', () => {
Expand Down Expand Up @@ -127,27 +127,6 @@ const actionCreator = createAction('anAction')
class Action {
type = 'totally an action'
}
describe('isAction', () => {
it('should only return true for plain objects with a string type property', () => {
const actionCreator = createAction('anAction')
class Action {
type = 'totally an action'
}
const testCases: [action: unknown, expected: boolean][] = [
[{ type: 'an action' }, true],
[{ type: 'more props', extra: true }, true],
[{ type: 0 }, false],
[actionCreator(), true],
[actionCreator, false],
[Promise.resolve({ type: 'an action' }), false],
[new Action(), false],
['a string', false],
]
for (const [action, expected] of testCases) {
expect(isAction(action)).toBe(expected)
}
})
})

describe('isActionCreator', () => {
it('should only return true for action creators', () => {
Expand Down
25 changes: 0 additions & 25 deletions packages/toolkit/src/tests/isPlainObject.test.ts

This file was deleted.

10 changes: 5 additions & 5 deletions yarn.lock
Expand Up @@ -7075,7 +7075,7 @@ __metadata:
node-fetch: ^2.6.1
prettier: ^2.2.1
query-string: ^7.0.1
redux: ^5.0.0-rc.0
redux: ^5.0.0-rc.1
redux-thunk: ^3.0.0-rc.0
reselect: ^5.0.0-beta.1
rimraf: ^3.0.2
Expand Down Expand Up @@ -25152,10 +25152,10 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"redux@npm:^5.0.0-rc.0":
version: 5.0.0-rc.0
resolution: "redux@npm:5.0.0-rc.0"
checksum: b8146301a27b54c2cf49bb5ef2650ddff5218533758c9a8e408141465922aab927be84421b64f8ea7e0ce69faa29b536cd6a1262c399fb86bbe991eed6a4ca88
"redux@npm:^5.0.0-rc.1":
version: 5.0.0-rc.1
resolution: "redux@npm:5.0.0-rc.1"
checksum: e3a26c5df84fb5705c82e17ca59c19374e3da3e87e5582201ca9b727e2569671bc1f01d738044f34bf38baaa296646a42714207a3dbc83d92ddf186ee0fc21c8
languageName: node
linkType: hard

Expand Down

0 comments on commit 3463225

Please sign in to comment.