Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.9 Integration branch #2401

Merged
merged 174 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
174 commits
Select commit Hold shift + click to select a range
c5d5335
drop pre-TS-4.1 types and workarounds (#2249)
phryneas Apr 17, 2022
cdc55e7
Add `isJsonContentType` predicate to `fetchBaseQuery` (#2331)
msutkowski Jun 10, 2022
b0f59fc
Add `jsonContentType` to `fetchBaseQuery` options (#2403)
msutkowski Jun 11, 2022
2397379
Merge remote-tracking branch 'origin/master' into v1.9-integration
phryneas Jun 24, 2022
7004ba4
Add ThunkMiddleware to the re-exported types from redux-thunk (#2451)
orta Jun 24, 2022
c630e83
Merge branch 'master' into v1.9-integration
markerikson Jul 2, 2022
1e5f34c
Merge remote-tracking branch 'origin/master' into v1.9-integration
phryneas Jul 8, 2022
fb2372b
add timeout option to fetchBaseQuery (#2143)
phryneas Jul 8, 2022
8ec8c39
Merge remote-tracking branch 'origin/next' into v1.9-integration
phryneas Jul 8, 2022
7dcab4e
createSlice: use template literal types for action type (#2250)
phryneas Jul 8, 2022
59dfcd2
add types for manually typing hook results in userland code (#2276)
phryneas Jul 8, 2022
a60e79d
Add 'content-type' ResponseHandler (#2363)
taylorkline Jul 8, 2022
8c0f239
Merge branch 'master' into v1.9-integration
markerikson Aug 6, 2022
49748b9
feature : endpoint-specific query arg serialization
michal-kurz Jul 6, 2022
e4c6765
Consolidate serializeQueryArgs override behavior into createApi
markerikson Aug 11, 2022
08bd66d
Merge branch 'master' into v1.9-integration
markerikson Aug 11, 2022
2840542
Merge pull request #2493 from michal-kurz/feature/endpoint-specific-a…
markerikson Aug 11, 2022
4860a89
RFC: add "merge" functionality
phryneas May 16, 2021
fdb4ab6
Update `merge` logic to make it optional and handle different use cases
markerikson Aug 14, 2022
aa7dafc
Merge pull request #1059 from reduxjs/feature/queryDefinition-merge
markerikson Aug 14, 2022
a254fb5
Add one-time runtime deprecation warnings for reducer object notation
markerikson Aug 14, 2022
24dbdf1
Merge pull request #2591 from reduxjs/feature/reducer-object-deprecation
markerikson Aug 14, 2022
4b09d16
feat: Add the ability to type `StoreEnhancers`
fostyfost Jul 27, 2022
f1afad8
Merge pull request #2550 from fostyfost/feature/store-enhancer-generics
markerikson Aug 14, 2022
a4a0c2d
Merge branch 'master' into v1.9-integration
markerikson Aug 15, 2022
5e17bd8
Rewrite subscription handling to batch actions
markerikson Aug 15, 2022
4cea2bf
Fix some React warnings in tests
markerikson Aug 15, 2022
e964498
Only reset cache timers when subscription counts hit 0
markerikson Aug 15, 2022
ba9ed75
Allow `keepUnusedDataFor: Infinity`
markerikson Aug 15, 2022
3ca3c88
Merge pull request #2599 from reduxjs/feature/batch-rtkq-rejections
markerikson Aug 15, 2022
c887f6c
add transformErrorReponse to rtkq endpoints
dreyks Dec 16, 2021
48f52e0
Remove time-sensitive CI test
markerikson Aug 16, 2022
a5e6587
Merge pull request #1841 from dreyks/feature/rtkq-transform-error-res…
markerikson Aug 16, 2022
245f6a0
:art: Keep track of all runningQueries
wesen Mar 31, 2022
478c935
alternate solution: identify synchronous condition skip & handle diff…
Apr 7, 2022
490c2d1
Merge pull request #2187 from wesen/bug/2186/fix-manually-initiated-r…
markerikson Aug 16, 2022
4862161
expose endpoint types
phryneas Oct 24, 2021
e5aaa08
Implement codemods for createReducer and createSlice builder
martinerko May 3, 2022
c0f515d
Move codemods into a new `rtk-codemods` package folder
markerikson Aug 16, 2022
3157ede
Add empty package.json for rtk-codemods and update Yarn
markerikson Aug 16, 2022
b4e8d94
Merge pull request #2602 from reduxjs/feature/rtk-object-codemods
markerikson Aug 16, 2022
22d0fbb
Merge pull request #1646 from reduxjs/expose-endpoint-types
markerikson Aug 16, 2022
8e82f4e
add createAsyncThunk.forTypes
phryneas Aug 16, 2022
17befa6
add another type test documenting still incomplete behaviour
phryneas Aug 16, 2022
b27f404
rename to `withTypes`, allow merge-overriding
phryneas Aug 19, 2022
74e7cb4
Add checks for `extra` in `withTypes
markerikson Aug 19, 2022
daf9920
Add example of `createAsyncThunk.withTypes`
markerikson Aug 19, 2022
0e46d65
Merge pull request #2604 from reduxjs/pr/cat-forTypes
markerikson Aug 19, 2022
ab92af2
Merge branch 'master' into v1.9-integration
markerikson Aug 19, 2022
ed3d162
Release 1.9.0-alpha.0
markerikson Aug 19, 2022
18113c8
return promise from query result & hook `refetch`
Apr 7, 2022
4b368fe
fix existing tests
Apr 7, 2022
737e039
Add test for refetch returning a promise
markerikson Aug 21, 2022
eb34fb3
Merge pull request #2212 from reduxjs/pr/refetch-promise
markerikson Aug 21, 2022
fb8b717
retry condition by http error response
kahirokunn Apr 14, 2022
01318ec
made retry condition more generic
kahirokunn Jun 11, 2022
2896b99
Clarified the description of shouldRetry.
kahirokunn Jul 1, 2022
89cab7a
style: Unified the style of retryCondition comments
kahirokunn Jul 9, 2022
0f62792
chore: retry > options.maxRetries has been eliminated and only should…
kahirokunn Jul 1, 2022
8959619
chore: rename shouldRetry to retryCondition
kahirokunn Jul 9, 2022
79351f4
test: accepts a custom retryCondition fn
kahirokunn Jul 9, 2022
0213506
test: retryCondition with endpoint config that overrides baseQuery co…
kahirokunn Jul 9, 2022
30d402e
test: retryCondition also works with mutations
kahirokunn Jul 10, 2022
e2ff14e
Make `maxRetries` and `retryCondition` mutually exclusive
markerikson Aug 22, 2022
9ed2ad1
Consolidate RTKQ middleware to simplify stack size
markerikson Aug 27, 2022
5e4c51f
Merge pull request #2239 from kahirokunn/retry-error
markerikson Aug 27, 2022
e502bc8
add upsertQueryDataThunk
barnabasJ Apr 20, 2022
8c3f5dd
add tests
barnabasJ Apr 20, 2022
ef461bc
use symbol as key for forceQueryFn
barnabasJ Apr 21, 2022
525b52a
dumb down upsertQueryData and add emptyCache boolean to patch result
barnabasJ Jun 26, 2022
36feac5
use a better key name
barnabasJ Jun 27, 2022
bac1e03
fix logic
barnabasJ Jun 27, 2022
936493e
Update `upsertQueryData` tests to match current behavior
markerikson Aug 27, 2022
9f12efd
Enable `upsertQueryData` calls while a request is in flight
markerikson Aug 27, 2022
06ee327
Fix `upsertQueryData` thunk types
markerikson Aug 27, 2022
72e5673
Merge pull request #2266 from barnabasJ/feature/upsertQueryData
markerikson Aug 28, 2022
6d10fab
Merge pull request #2641 from reduxjs/feature/consolidate-rtkq-middle…
markerikson Aug 28, 2022
67b6ed2
Merge branch 'master' into v1.9-integration
markerikson Aug 28, 2022
bb640f8
Try fixing createAsyncThunk issues with TS 4.8
markerikson Aug 28, 2022
38f0f7d
Lock MSW to 0.40.2, and patch it to ignore TS errors
markerikson Aug 28, 2022
ce98d69
Merge pull request #2643 from reduxjs/bugfix/1.9-ts4.8-fixes
markerikson Aug 28, 2022
1a28687
Use better check for "forced query" in `queryThunk` condition check
markerikson Aug 28, 2022
d2e200a
Also run handlers if action has hydration info
markerikson Aug 28, 2022
e7e9591
Merge pull request #2644 from reduxjs/bugfix/rtkq-middleware-fixes
markerikson Aug 28, 2022
50e1b8e
Release 1.9.0-alpha.1
markerikson Aug 28, 2022
d9e481b
fix `upsertQueryData` race situations (#2646)
phryneas Aug 29, 2022
e71d9ba
fix upserting
phryneas Sep 2, 2022
1b631d3
Add tests for basic upsert behavior
markerikson Sep 4, 2022
29f4e4f
Merge pull request #2669 from reduxjs/fix-upsert
markerikson Sep 4, 2022
4f17c71
Merge remote-tracking branch 'origin/master' into v1.9-integration
phryneas Oct 2, 2022
7611078
Export the BaseQueryApi symbol. (#2740)
nsthorat Oct 2, 2022
fcabc7d
Fix retryCondition `error`. (#2743)
phryneas Oct 4, 2022
6fa17c6
fix: export ToolkitStore interface from configureStore
adamhari Oct 5, 2022
659561a
Merge pull request #2750 from adamhari/export-toolkitstore-interface-…
markerikson Oct 6, 2022
94d93d9
reset `dispatchQueued` variable after flushing
phryneas Oct 8, 2022
7c4348c
Merge pull request #2757 from reduxjs/pr/fixup-batchActions-microtask
markerikson Oct 8, 2022
bf7c37b
query/endpointDefinitions: add sideEffectForced to QueryExtraOptions
Sep 1, 2022
0dedba0
rename sideEffectForced to forceRefetch
Sep 13, 2022
9723738
Add args to forceRefetch, add doc block, and test for merging
markerikson Oct 8, 2022
2d000a5
Merge pull request #2663 from schadenn/feature/add-side-effect-forced
markerikson Oct 8, 2022
73abb6a
Merge branch 'master' into v1.9-integration
markerikson Oct 8, 2022
6975282
Speed up subscription behavior by tracking state in middleware
markerikson Oct 8, 2022
9dbdde5
Merge pull request #2759 from reduxjs/feature/middleware-internal-sub…
markerikson Oct 9, 2022
dbcc556
Release 1.9.0-alpha.2
markerikson Oct 9, 2022
7917fd2
Add codemods-cli setup
markerikson Oct 10, 2022
e00441e
Dedupe packages
markerikson Oct 10, 2022
b877486
Add stub createReducerBuilder codemod
markerikson Oct 10, 2022
ef872b7
Add stub test fixture
markerikson Oct 10, 2022
56bb58b
Sorta get TS-based transforms running
markerikson Oct 12, 2022
52d1519
Got TS-based `createReducer` transform mostly working
markerikson Oct 12, 2022
2c86fa9
Add empty createSliceBuilder codemod
markerikson Oct 12, 2022
cdea9e5
Get createSlice transform working and fix whitespace test failures
markerikson Oct 12, 2022
59411a9
Replace original `rtk-codemods` folder with new implementation
markerikson Oct 13, 2022
6fd7cb1
Prepare for initial rtk-codemods package release
markerikson Oct 13, 2022
857f147
Release @reduxjs/rtk-codemods 0.0.1
markerikson Oct 13, 2022
d7a8a5d
Get codemod running with ts-node on deploy
markerikson Oct 13, 2022
81f30f4
Release @reduxjs/rtk-codemods 0.0.2
markerikson Oct 13, 2022
d9fe1fd
`prepareHeaders` does not need to return `headers` any more (#2775)
phryneas Oct 14, 2022
b9aefe0
Merge pull request #2768 from reduxjs/feature/rtk-reducer-codemods
markerikson Oct 15, 2022
290a78b
remove alpha compatibility fallbacks
phryneas Oct 15, 2022
85abc34
`fetchBaseQuery`: allow `headers` option
phryneas Oct 15, 2022
daf8d9a
Merge pull request #2777 from reduxjs/pr/remove-alpha-compat
markerikson Oct 15, 2022
6f1f0a4
fix `skipToken` behaviour in `useQueryState`
phryneas Oct 15, 2022
b92fb8d
Merge pull request #2779 from reduxjs/pr/batched-skiptoken-fix
markerikson Oct 18, 2022
76cedeb
Merge pull request #2778 from reduxjs/pr/fetchBaseQuery-header-option
markerikson Oct 18, 2022
a7fd57c
scope `getRunningOperationPromise` to store instance
phryneas Jul 2, 2022
565f597
replace `getRunningOperationPromise(s)` with `getRunning(Query|Querie…
phryneas Oct 15, 2022
a73aee9
re-add faulty `getRunningOperationPromises`
phryneas Oct 15, 2022
e126f17
Merge pull request #2481 from reduxjs/fix-2477
markerikson Oct 18, 2022
2b864ed
Pass internal middleware state in on setup for simplicity
markerikson Oct 18, 2022
8d506ba
Remove leftover middleware perf debugging code
markerikson Oct 18, 2022
1696f7a
Move subscription tracking logic over to batchActions file
markerikson Oct 18, 2022
feccd87
Assorted other cleanup
markerikson Oct 18, 2022
8f48003
Merge pull request #2789 from reduxjs/feature/v1.9-pre-beta-cleanup
markerikson Oct 18, 2022
1e983cc
Convert "middleware not registered" warning into a full error
markerikson Oct 19, 2022
498ff77
Also throw error from query hooks if middleware not registered
markerikson Oct 19, 2022
0d02024
Merge pull request #2792 from reduxjs/feature/v1.9-more-beta-prep
markerikson Oct 19, 2022
82cf232
Release 1.9.0-beta.0
markerikson Oct 19, 2022
9fc1ecc
Update Yarn to 3.2.4 to fix Docusaurus install issues
markerikson Oct 16, 2022
8bd8a40
Update Docusaurus to 2.1.0 and fix React types packages
markerikson Oct 16, 2022
af63a4f
Export `defaultSerializeQueryArgs`
markerikson Oct 20, 2022
9faae2a
Document `serializeQueryArgs` option
markerikson Oct 20, 2022
5ff666f
Document `merge` option
markerikson Oct 20, 2022
d567ef3
Document `upsertQueryData`
markerikson Oct 21, 2022
71003b8
Document `forceRefetch` option
markerikson Oct 21, 2022
85a3d0d
Rework fetchBaseQuery API ref and document new options
markerikson Oct 26, 2022
bcddc68
Add codemods page
markerikson Oct 26, 2022
6763f7b
Turn off `skipLibCheck` for typetests
markerikson Oct 26, 2022
e0a8bd5
Merge pull request #2804 from reduxjs/docs/v1.9-docs
markerikson Oct 28, 2022
d8d1af6
Rework endpoint serializeQueryArgs to allow object/number returns
markerikson Oct 28, 2022
17d1789
Merge pull request #2835 from reduxjs/feature/rework-sqa
markerikson Oct 28, 2022
d4717b1
allow for global `responseHandler` and `validateStatus` configuration
phryneas Oct 26, 2022
a2fec02
Add tests for global fetchBaseQuery args
markerikson Oct 30, 2022
270a14f
Merge pull request #2823 from reduxjs/draft/inspiring-currying
markerikson Oct 30, 2022
db3ed1e
Fix refetches when sQA returns same value and queryArgs are object
markerikson Oct 30, 2022
d934fad
Merge pull request #2844 from reduxjs/bugfix/forceRefetch-obj-args
markerikson Oct 30, 2022
0de2441
Add an auto-batching enhancer that delays low-pri notifications
markerikson Oct 30, 2022
ac01d3d
Apply SHOULD_AUTOBATCH to RTKQ thunks for opt-in perf boost
markerikson Oct 30, 2022
cca9779
Add autoBatchEnhancer docs
markerikson Oct 30, 2022
b367946
Merge pull request #2846 from reduxjs/feature/auto-batch-enhancer-again
markerikson Oct 30, 2022
150a93e
Check middleware registration directly to avoid persistence issues
markerikson Oct 30, 2022
ec05907
Merge pull request #2850 from reduxjs/feature/middleware-registration…
markerikson Oct 30, 2022
4c32e29
Release 1.9.0-rc.0
markerikson Oct 30, 2022
23292c5
Fix "running thunks" types from returning undefined
markerikson Nov 1, 2022
e672cd8
Remove unnecessary memoized selectors
markerikson Nov 1, 2022
38d65c0
Merge pull request #2856 from reduxjs/feature/1.9-rc-tweaks
markerikson Nov 1, 2022
b12b22c
Merge branch 'master' into v1.9-integration
markerikson Nov 1, 2022
f178b94
Make autobatching notification queueing configurable
markerikson Nov 2, 2022
fdc1c50
Merge pull request #2857 from reduxjs/feature/autobatch-timing
markerikson Nov 2, 2022
48fe9e1
Release 1.9.0-rc.1
markerikson Nov 2, 2022
35f7e6d
Switch autobatch default method to 'raf'
markerikson Nov 3, 2022
8f30d43
Add TS 4.9 RC to the test matrix
markerikson Nov 3, 2022
5e4ad31
Bump dependencies
markerikson Nov 4, 2022
ed75be0
Merge pull request #2859 from reduxjs/feature/final-v1.9-tweaks
markerikson Nov 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
fail-fast: false
matrix:
node: ['16.x']
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', '4.8']
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', '4.8', '4.9.2-rc']
steps:
- name: Checkout repo
uses: actions/checkout@v2
Expand All @@ -123,10 +123,6 @@ jobs:

- run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.base.json ./jest.config.js ./src/tests/*.* ./src/query/tests/*.*

- name: "@ts-ignore stuff that didn't exist pre-4.1 in the tests"
if: ${{ matrix.ts < 4.1 }}
run: sed -i -e 's/@pre41-ts-ignore/@ts-ignore/' -e '/pre41-remove-start/,/pre41-remove-end/d' ./src/tests/*.* ./src/query/tests/*.ts*

- name: Test types
run: |
yarn tsc --version
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/dist/**
**/etc/**
**/temp/**
**/temp/**
**/__testfixtures__/**
20 changes: 20 additions & 0 deletions .yarn/patches/msw-npm-0.40.2-2107d48752
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
diff --git a/lib/types/context/set.d.ts b/lib/types/context/set.d.ts
index 266229bad706ec49392b8b87e18560c1566b490d..4fad485f8ffec2db92e808a05ccd9274414a9bd9 100644
--- a/lib/types/context/set.d.ts
+++ b/lib/types/context/set.d.ts
@@ -15,4 +15,5 @@ export declare type ForbiddenHeaderError<HeaderName extends string> = `SafeRespo
* })
* @see {@link https://mswjs.io/docs/api/context/set `ctx.set()`}
*/
+// @ts-ignore
export declare function set<N extends string | HeadersObject>(...args: N extends string ? Lowercase<N> extends ForbiddenHeaderNames ? ForbiddenHeaderError<N> : [N, string] : N extends HeadersObject<infer CookieName> ? Lowercase<CookieName> extends ForbiddenHeaderNames ? ForbiddenHeaderError<CookieName> : [N] : [N]): ResponseTransformer;
diff --git a/lib/types/sharedOptions.d.ts b/lib/types/sharedOptions.d.ts
index d1d6e05df2dc2c29f06d8d0b91c500a10e651a29..3d8c29fd2089b2abf21d78cd277aac9271e781c2 100644
--- a/lib/types/sharedOptions.d.ts
+++ b/lib/types/sharedOptions.d.ts
@@ -21,4 +21,5 @@ export interface LifeCycleEventsMap<ResponseType> {
'response:bypass': (response: ResponseType, requestId: string) => void;
unhandledException: (error: Error, request: MockedRequest) => void;
}
+// @ts-ignore
export declare type LifeCycleEventEmitter<ResponseType> = Pick<StrictEventEmitter<ResponseType>, 'on' | 'removeListener' | 'removeAllListeners'>;
768 changes: 0 additions & 768 deletions .yarn/releases/yarn-3.1.0.cjs

This file was deleted.

801 changes: 801 additions & 0 deletions .yarn/releases/yarn-3.2.4.cjs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"
spec: '@yarnpkg/plugin-workspace-tools'
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs
spec: "@yarnpkg/plugin-version"
spec: '@yarnpkg/plugin-version'

yarnPath: .yarn/releases/yarn-3.1.0.cjs
yarnPath: .yarn/releases/yarn-3.2.4.cjs
143 changes: 143 additions & 0 deletions docs/api/autoBatchEnhancer.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
---
id: autoBatchEnhancer
title: autoBatchEnhancer
sidebar_label: autoBatchEnhancer
hide_title: true
---

&nbsp;

# `autoBatchEnhancer`

A Redux store enhancer that looks for one or more "low-priority" dispatched actions in a row, and queues a callback to run subscriber notifications on a delay. It then notifies subscribers either when the queued callback runs, or when the next "normal-priority" action is dispatched, whichever is first.

## Basic Usage

```ts
import {
createSlice,
configureStore,
autoBatchEnhancer,
prepareAutoBatched,
} from '@reduxjs/toolkit'

interface CounterState {
value: number
}

const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 } as CounterState,
reducers: {
incrementBatched: {
// Batched, low-priority
reducer(state) {
state.value += 1
},
// highlight-start
// Use the `prepareAutoBatched` utility to automatically
// add the `action.meta[SHOULD_AUTOBATCH]` field the enhancer needs
prepare: prepareAutoBatched<void>(),
// highlight-end
},
// Not batched, normal priority
decrementUnbatched(state) {
state.value -= 1
},
},
})
const { incrementBatched, decrementUnbatched } = counterSlice.actions

const store = configureStore({
reducer: counterSlice.reducer,
// highlight-start
enhancers: (existingEnhancers) => {
// Add the autobatch enhancer to the store setup
return existingEnhancers.concat(autoBatchEnhancer())
},
// highlight-end
})
```

## API

### `autoBatchEnhancer`

```ts title="autoBatchEnhancer signature" no-transpile
export type SHOULD_AUTOBATCH = string
type AutoBatchOptions =
| { type: 'tick' }
| { type: 'timer'; timeout: number }
| { type: 'raf' }
| { type: 'callback'; queueNotification: (notify: () => void) => void }

export type autoBatchEnhancer = (options?: AutoBatchOptions) => StoreEnhancer
```

Creates a new instance of the autobatch store enhancer.

Any action that is tagged with `action.meta[SHOULD_AUTOBATCH] = true` will be treated as "low-priority", and a notification callback will be queued. The enhancer will delay notifying subscribers until either:

- The queued callback runs and triggers the notifications
- A "normal-priority" action (any action _without_ `action.meta[SHOULD_AUTOBATCH] = true`) is dispatched in the same tick

`autoBatchEnhancer` accepts options to configure how the notification callback is queued:

- `{type: 'raf'}`: queues using `requestAnimationFrame` (default)
- `{type: 'tick'}: queues using `queueMicrotask`
- `{type: 'timer, timeout: number}`: queues using `setTimeout`
- `{type: 'callback', queueNotification: (notify: () => void) => void}: lets you provide your own callback, such as a debounced or throttled function

The default behavior is to queue the notifications using `requestAnimationFrame`.

The `SHOULD_AUTOBATCH` value is meant to be opaque - it's currently a string for simplicity, but could be a `Symbol` in the future.

### `prepareAutoBatched`

```ts title="prepareAutoBatched signature" no-transpile
type prepareAutoBatched = <T>() => (payload: T) => { payload: T; meta: unknown }
```

Creates a function that accepts a `payload` value, and returns an object with `{payload, meta: {[SHOULD_AUTOBATCH]: true}}`. This is meant to be used with RTK's `createSlice` and its "`prepare` callback" syntax:

```ts no-transpile
createSlice({
name: 'todos',
initialState,
reducers: {
todoAdded: {
reducer(state, action: PayloadAction<Todo>) {
state.push(action.payload)
},
// highlight-start
prepare: prepareAutoBatched<Todo>(),
// highlight-end
},
},
})
```

## Batching Approach and Background

The post [A Comparison of Redux Batching Techniques](https://blog.isquaredsoftware.com/2020/01/blogged-answers-redux-batching-techniques/) describes four different approaches for "batching Redux actions/dispatches"

- a higher-order reducer that accepts multiple actions nested inside one real action, and iterates over them together
- an enhancer that wraps `dispatch` and debounces the notification callback
- an enhancer that wraps `dispatch` to accept an array of actions
- React's `unstable_batchedUpdates()`, which just combines multiple queued renders into one but doesn't affect subscriber notifications

This enhancer is a variation of the "debounce" approach, but with a twist.

Instead of _just_ debouncing _all_ subscriber notifications, it watches for any actions with a specific `action.meta[SHOULD_AUTOBATCH]: true` field attached.

When it sees an action with that field, it queues a callback. The reducer is updated immediately, but the enhancer does _not_ notify subscribers right way. If other actions with the same field are dispatched in succession, the enhancer will continue to _not_ notify subscribers. Then, when the queued callback runs, it finally notifies all subscribers, similar to how React batches re-renders.

The additional twist is also inspired by React's separation of updates into "low-priority" and "immediate" behavior (such as a render queued by an AJAX request vs a render queued by a user input that should be handled synchronously).

If some low-pri actions have been dispatched and a notification microtask is queued, then a _normal_ priority action (without the field) is dispatched, the enhancer will go ahead and notify all subscribers synchronously as usual, and _not_ notify them at the end of the tick.

This allows Redux users to selectively tag certain actions for effective batching behavior, making this purely opt-in on a per-action basis, while retaining normal notification behavior for all other actions.

### RTK Query and Batching

RTK Query already marks several of its key internal action types as batchable. If you add the `autoBatchEnhancer` to the store setup, it will improve the overall UI performance, especially when rendering large lists of components that use the RTKQ query hooks.
77 changes: 77 additions & 0 deletions docs/api/codemods.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
id: codemods
title: Codemods
sidebar_label: Codemods
hide_title: true
---

&nbsp;

# Codemods

Per [the description in `1.9.0-alpha.0`](https://github.com/reduxjs/redux-toolkit/releases/tag/v1.9.0-alpha.0), we plan to remove the "object" argument from `createReducer` and `createSlice.extraReducers` in the future RTK 2.0 major version. In `1.9.0-alpha.0`, we added a one-shot runtime warning to each of those APIs.

To simplify upgrading codebases, we've published a set of codemods that will automatically transform the deprecated "object" syntax into the equivalent "builder" syntax.

The codemods package is available on NPM as [**`@reduxjs/rtk-codemods`**](https://www.npmjs.com/package/@reduxjs/rtk-codemods). It currently contains two codemods: `createReducerBuilder` and `createSliceBuilder`.

To run the codemods against your codebase, run `npx @reduxjs/rtk-codemods <TRANSFORM NAME> path/of/files/ or/some**/*glob.js`.

Examples:

```bash
npx @reduxjs/rtk-codemods createReducerBuilder ./src

npx @reduxjs/rtk-codemods createSliceBuilder ./packages/my-app/**/*.ts
```

We also recommend re-running Prettier on the codebase before committing the changes.

**These codemods _should_ work, but we would greatly appreciate testing and feedback on more real-world codebases!**

Before:

```js
createReducer(initialState, {
[todoAdded1a]: (state, action) => {
// stuff
},
[todoAdded1b]: (state, action) => action.payload,
})

const slice1 = createSlice({
name: 'a',
initialState: {},
extraReducers: {
[todoAdded1a]: (state, action) => {
// stuff
},
[todoAdded1b]: (state, action) => action.payload,
},
})
```

After:

```js
createReducer(initialState, (builder) => {
builder.addCase(todoAdded1a, (state, action) => {
// stuff
})

builder.addCase(todoAdded1b, (state, action) => action.payload)
})

const slice1 = createSlice({
name: 'a',
initialState: {},

extraReducers: (builder) => {
builder.addCase(todoAdded1a, (state, action) => {
// stuff
})

builder.addCase(todoAdded1b, (state, action) => action.payload)
},
})
```
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"devDependencies": {
"@manaflair/redux-batch": "^1.0.0",
"@types/nanoid": "^2.1.0",
"@types/react": "^16.9.46",
"@types/react": "^18.0",
"@types/redux-logger": "^3.0.8",
"async-mutex": "^0.3.2",
"axios": "^0.20.0",
Expand Down
Loading