可序列化中介軟體
一個自訂中介軟體,用於偵測狀態或已發送動作中是否包含任何不可序列化的值,其範例仿照 redux-immutable-state-invariant
。任何偵測到的不可序列化值都會記錄到主控台。
此中介軟體預設會由 configureStore
和 getDefaultMiddleware
加入 store。
你可以將任何受支援的選項傳遞為 getDefaultMiddleware
的 serializableCheck
值,以自訂此中介軟體的行為。
選項
interface SerializableStateInvariantMiddlewareOptions {
/**
* The function to check if a value is considered serializable. This
* function is applied recursively to every value contained in the
* state. Defaults to `isPlain()`.
*/
isSerializable?: (value: any) => boolean
/**
* The function that will be used to retrieve entries from each
* value. If unspecified, `Object.entries` will be used. Defaults
* to `undefined`.
*/
getEntries?: (value: any) => [string, any][]
/**
* An array of action types to ignore when checking for serializability.
* Defaults to []
*/
ignoredActions?: string[]
/**
* An array of dot-separated path strings or regular expressions to ignore
* when checking for serializability, Defaults to
* ['meta.arg', 'meta.baseQueryMeta']
*/
ignoredActionPaths?: (string | RegExp)[]
/**
* An array of dot-separated path strings or regular expressions to ignore
* when checking for serializability, Defaults to []
*/
ignoredPaths?: (string | RegExp)[]
/**
* Execution time warning threshold. If the middleware takes longer
* than `warnAfter` ms, a warning will be displayed in the console.
* Defaults to 32ms.
*/
warnAfter?: number
/**
* Opt out of checking state. When set to `true`, other state-related params will be ignored.
*/
ignoreState?: boolean
/**
* Opt out of checking actions. When set to `true`, other action-related params will be ignored.
*/
ignoreActions?: boolean
}
匯出
createSerializableStateInvariantMiddleware
建立序列化檢查中間件的實例,並提供給定選項。
你很可能不需要自己呼叫這個,因為 getDefaultMiddleware
已經這樣做了。
範例
- TypeScript
- JavaScript
import { Iterable } from 'immutable'
import {
configureStore,
createSerializableStateInvariantMiddleware,
isPlain,
Tuple,
} from '@reduxjs/toolkit'
import reducer from './reducer'
// Augment middleware to consider Immutable.JS iterables serializable
const isSerializable = (value: any) =>
Iterable.isIterable(value) || isPlain(value)
const getEntries = (value: any) =>
Iterable.isIterable(value) ? value.entries() : Object.entries(value)
const serializableMiddleware = createSerializableStateInvariantMiddleware({
isSerializable,
getEntries,
})
const store = configureStore({
reducer,
middleware: () => new Tuple(serializableMiddleware),
})
import { Iterable } from 'immutable'
import {
configureStore,
createSerializableStateInvariantMiddleware,
isPlain,
Tuple,
} from '@reduxjs/toolkit'
import reducer from './reducer'
// Augment middleware to consider Immutable.JS iterables serializable
const isSerializable = (value) => Iterable.isIterable(value) || isPlain(value)
const getEntries = (value) =>
Iterable.isIterable(value) ? value.entries() : Object.entries(value)
const serializableMiddleware = createSerializableStateInvariantMiddleware({
isSerializable,
getEntries,
})
const store = configureStore({
reducer,
middleware: () => new Tuple(serializableMiddleware),
})
isPlain
檢查給定的值是否被視為「純值」。
目前實作為
- TypeScript
- JavaScript
import isPlainObject from './isPlainObject'
export function isPlain(val: any) {
return (
typeof val === 'undefined' ||
val === null ||
typeof val === 'string' ||
typeof val === 'boolean' ||
typeof val === 'number' ||
Array.isArray(val) ||
isPlainObject(val)
)
}
import isPlainObject from './isPlainObject'
export function isPlain(val) {
return (
typeof val === 'undefined' ||
val === null ||
typeof val === 'string' ||
typeof val === 'boolean' ||
typeof val === 'number' ||
Array.isArray(val) ||
isPlainObject(val)
)
}
這將接受所有標準 JS 物件、陣列和基本型別,但會為 Date
、Map
和其他類似類別實例傳回 false。