@acoustic-content-sdk/redux-store 中文文档教程
的实现Redux 存储支持动态添加功能模块。
Providing a feature module
为您的模块创建并导出 ReduxFeatureModule
实例。
Example
import {
ReduxFeatureModule,
createReduxFeatureModule
} from '@acoustic-content-sdk/redux-store';
import { sampleEpic } from './feature.epics';
import { sampleReducer } from './feature.reducer';
import { SampleFeatureState } from './feature.state';
export const sampleFeature = createReduxFeatureModule(
'SAMPLE_FEATURE',
sampleReducer,
sampleEpic
);
在某些情况下,一个功能模块依赖于其他功能模块的存在,例如因为 epic 可能需要它。 在这种情况下,在 dependencies
参数中列出功能模块所依赖的模块:
import {
ReduxFeatureModule,
createReduxFeatureModule
} from '@acoustic-content-sdk/redux-store';
import { sampleEpic } from './feature.epics';
import { sampleReducer } from './feature.reducer';
import { SampleFeatureState } from './feature.state';
export const sampleFeature = createReduxFeatureModule(
'SAMPLE_FEATURE',
sampleReducer,
sampleEpic,
[depModule1, depModule2, depModule3]
);
Registering a feature module
使用 addFeatureModule
方法向根存储注册功能模块。 这也将按拓扑顺序注册所有依赖模块。
import {
ReduxRootStore
} from '@acoustic-content-sdk/redux-store';
const store: ReduxRootStore = ...;
store.addFeatureModule(sampleFeature);
Consuming a feature module
使用 selectFeature
方法为所需的功能创建一个选择器。
Example
import { selectFeature } from '@acoustic-content-sdk/redux-store';
const selectSample = selectFeature(sampleFeature);
Side effects in Feature Modules
功能模块可能会为异步处理提供副作用 (Epics)。 有时,此类史诗需要初始化事件来执行引导逻辑。 在功能模块初始化之后,商店为此目的发送一个初始化事件。 使用 ofInitFeature
方法订阅此事件。
Example
import { ofInitFeature } from '@acoustic-content-sdk/redux-store';
const initEpic: Epic = (actions$) =>
actions$.pipe(ofInitFeature(sampleFeature), map(...));
API Documentation
Home > @acoustic-content-sdk/redux-store
redux-store package
Redux 的实现> 支持动态添加功能模块的存储。
Functions
Function | Description |
---|---|
createIdentifier(idOrModuleId, aValue) | Scopes the given ID with a feature prefix to make it unique |
createReduxFeatureModule(idOrModuleId, reducer, epic, dependencies) | Convenience method to create a feature module |
createReduxMetaModule(dependencies) | Convenience method to create a meta module, i.e. a module that bundles other modules |
createReduxRootStore(aDependencies, aPreLoadedState) | Constructs a new store that can handle feature modules. |
featureModuleId(id) | Constructs a feature module ID that carries type information |
ofInitFeature(idOrModuleId) | Returns an operator function that filters the initialization actions for a particular feature. This is typically used in feature epics. Since we know that an initialization can occur at most once, the operator also terminates automatically after the first emission. |
rxSelectFeature(idOrModuleId) | Returns a selector that selects the given feature from the store and returns an observable to that feature state. |
rxStore(aStore) | Exposes the store as an Observable. |
Interfaces
Interface | Description |
---|---|
PayloadAction | Base class for actions that carry a payload. Use the selectPayload method to extract the payload. |
ReduxFeatureModule | Defines the feature module. The ID identifies the section in the state and is also used to globally discriminate features.After instantiating a feature store the store will fire an initialization action for that feature. Use ofInitFeature() to register for these initialization actions. |
ReduxFeatureModuleId | Feature module identifier. This wrapper around an ID is useful, because it carries type information. |
ReduxRootStore | Implementation of a store that manages sub-state as features. Features are added to the store automatically, when required by the select method. |
ReduxRootStoreDependencies | Dependencies object available for epics |
Variables
Variable | Description |
---|---|
rxDispatch | Binds the dispatch method |
rxSelect | Exposes a memoized selector function |
selectFeature | Returns a selector that selects the given feature |
selectPayload | Selects the payload from a PayloadAction instance |
STORE_ID | Exposes the namespace of this store. This constant can e.g. be used to prefix actions. |
VERSION | Version and build number of the package |
Type Aliases
Type Alias | Description |
---|---|
ReduxRootState | Root state for the reducer, this is a mapping from feature ID to feature. |
Home > @acoustic-content-sdk/redux-store > createIdentifier
createIdentifier() function
使用特征前缀限定给定 ID 以使其唯一
签名:
export declare function createIdentifier(idOrModuleId: string | ReduxFeatureModuleId<any, any>, aValue?: string): string;
Parameters
Parameter | Type | Description |
---|---|---|
idOrModuleId | string | ReduxFeatureModuleId<any, any> |
the module |
aValue | string |
optionally a identifier, will create a random identifier otherwise |
返回:
string
the new identifier
Home > @acoustic-content-sdk/redux-store > createReduxFeatureModule
createReduxFeatureModule() function
创建功能模块的便捷方法
签名:
export declare function createReduxFeatureModule<S, FS = any, Input extends Action = AnyAction, Output extends Input = Input, Dependencies = any>(idOrModuleId?: string | ReduxFeatureModuleId<S, FS>, reducer?: Reducer<S>, epic?: Epic<Input, Output, ReduxRootState, Dependencies>, dependencies?: ArrayLike<ReduxFeatureModule<any>>): ReduxFeatureModule<S, FS>;
Parameters
Parameter | Type | Description |
---|---|---|
idOrModuleId | string | ReduxFeatureModuleId<S, FS> |
ID of the store, if not set, generate a random identifier |
reducer | Reducer<S> |
optionally the reducer |
epic | Epic<Input, Output, ReduxRootState, Dependencies> |
optionally the epic |
dependencies | ArrayLike<ReduxFeatureModule<any>> |
optionally the dependencies on other modules |
返回:
ReduxFeatureModule,FS
a ReduxFeatureModule instance
Home > @acoustic-content-sdk/redux-store > createReduxMetaModule
createReduxMetaModule() function
创建元模块的便捷方法,即捆绑其他模块的模块
签名:
export declare function createReduxMetaModule<FS = any>(dependencies: ArrayLike<ReduxFeatureModule<any>>): ReduxFeatureModule<any, FS>;
Parameters
Parameter | Type | Description |
---|---|---|
dependencies | ArrayLike<ReduxFeatureModule<any>> |
dependencies on other modules |
返回:
ReduxFeatureModule
a ReduxFeatureModule instance
Home > @acoustic-content-sdk/redux-store > createReduxRootStore
createReduxRootStore() function
构造一个可以处理功能模块的新商店。
签名:
export declare function createReduxRootStore(aDependencies: any, aPreLoadedState?: ReduxRootState): ReduxRootStore;
Parameters
Parameter | Type | Description |
---|---|---|
aDependencies | any |
the dependencies that will be injected into the epics |
aPreLoadedState | ReduxRootState |
optionally an initial state object |
返回:
ReduxRootStore
商店。 使用 ReduxRootStore.addFeatureModule() 方法注册一个功能模块。
Home > @acoustic-content-sdk/redux-store > featureModuleId
featureModuleId() function
构造一个携带类型信息的特征模块ID
Signature:
export declare function featureModuleId<S, FS = any>(id?: string): ReduxFeatureModuleId<S, FS>;
Parameters
Parameter | Type | Description |
---|---|---|
id | string |
the module identifier or empty to create a random identifier |
返回:
>
the ID
Home > @acoustic-content-sdk/redux-store > ofInitFeature
ofInitFeature() function
返回一个运算符函数,用于过滤特定功能的初始化操作。 这通常用于长篇史诗。 由于我们知道初始化最多可以发生一次,因此运算符也会在第一次发射后自动终止。
签名:
export declare function ofInitFeature<A = AnyAction>(idOrModuleId: string | ReduxFeatureModuleId<any, any>): OperatorFunction<A, string>;
Parameters
Parameter | Type | Description |
---|---|---|
idOrModuleId | string | ReduxFeatureModuleId<any, any> |
the feature ID to filter for |
返回:
用于过滤初始化操作并解析为特征 ID 的运算符函数
46e>46e @acoustic-content-sdk/redux-store > rxSelectFeature
rxSelectFeature() function
返回一个选择器,该选择器从存储中选择给定的特征并将可观察到的特征状态返回。
签名:
export declare function rxSelectFeature<S, FS = any>(idOrModuleId: string | ReduxFeatureModuleId<S, FS>): UnaryFunction<ReduxRootStore, Observable<S>>;
Parameters
Parameter | Type | Description |
---|---|---|
idOrModuleId | string | ReduxFeatureModuleId<S, FS> |
ID of the module |
返回:
UnaryFunction
具有所选特征的可观察
对象 < /a>
首页 > @acoustic-content-sdk/redux-store > rxStore
rxStore() function
将商店公开为Observable.
签名:
export declare function rxStore<S>(aStore: Store<S>): Observable<S>;
Parameters
Parameter | Type | Description |
---|---|---|
aStore | Store<S> |
the store |
返回:
Observable
商店作为 Observable
Home > @acoustic-content-sdk/redux-store > PayloadAction
PayloadAction interface
承载负载的操作的基类。 使用selectPayload方法提取有效负载。
Signature:
export interface PayloadAction<T> extends Action
Properties
Property | Type | Description |
---|---|---|
payload | T |
Home > @acoustic-content-sdk/redux-store > selectPayload
selectPayload variable
Selects the payload from a PayloadAction instance
Signature:
selectPayload: <T>(aAction: PayloadAction<T>) => T
主页 > @acoustic-content-sdk/redux-store > ReduxFeatureModule
ReduxFeatureModule interface
定义功能模块。 ID 标识状态中的部分,也用于全局区分功能。
实例化功能存储后,存储将触发该功能的初始化操作。 使用 ofInitFeature() 注册这些初始化操作。
Signature:
export interface ReduxFeatureModule<S, FS = any, Input extends Action = AnyAction, Output extends Input = Input, Dependencies = any> extends ReduxFeatureModuleId<S, FS>
Properties
Property | Type | Description |
---|---|---|
dependencies | ArrayLike<ReduxFeatureModule<any>> |
Dependencies on other modules |
epic | Epic<Input, Output, ReduxRootState, Dependencies> |
The epic for side effects |
id | string |
ID of the feature module, will also be used as the key to the state |
reducer | Reducer<S> |
The reducer for the module. |
Home > @acoustic-content-sdk/redux-store > ReduxFeatureModuleId
ReduxFeatureModuleId interface
特征模块标识符。 这个 ID 的包装很有用,因为它携带了类型信息。
Signature:
export interface ReduxFeatureModuleId<S, FS = any>
Properties
Property | Type | Description |
---|---|---|
id | string |
ID of the feature module, will also be used as the key to the state |
Home > @acoustic-content-sdk/redux-store > ReduxRootStore
ReduxRootStore interface
作为功能管理子状态的存储的实现。 当 select 方法需要时,功能会自动添加到商店中。
Signature:
export interface ReduxRootStore<S = ReduxRootState, A extends Action = AnyAction> extends Store<S, A>
Methods
Method | Description |
---|---|
addFeatureModule(aFeature) | Registers a feature module with the root store |
Home > @acoustic-content-sdk/redux-store > ReduxRootStoreDependencies
ReduxRootStoreDependencies interface
Dependencies object available for
epics Signature:
export interface ReduxRootStoreDependencies
Properties
Property | Type | Description |
---|---|---|
logSvc | LoggerService |
Service that allows to create logger instances |
rootStore | ReduxRootStore |
The root store dependencies |
Home > @acoustic-content-sdk/redux-store > rxDispatch Binds
rxDispatch variable
the dispatch method
Signature:
rxDispatch: <S>(aStore: Store<S>) => Consumer<AnyAction>
Home< /a> > @acoustic-content-sdk/redux-store > rxSelect
rxSelect variable
Exposes a memoized selector function
Signature:
rxSelect: <T, R>(aSelector: UnaryFunction<T, R>, aCmp?: EqualsPredicate<R>) => OperatorFunction<T, R>
Home > @acoustic-content-sdk/redux-store > selectFeature
selectFeature variable
Returns a selector that selects the given feature
Signature:
selectFeature: <S, FS = any>(idOrModuleId: string | ReduxFeatureModuleId<S, FS>, aDefaultState?: S) => UnaryFunction<ReduxRootState, S>
首页 > @acoustic-content-sdk/redux-store > STORE_ID
STORE_ID variable
公开此商店的命名空间。 例如,这个常量可以用来给动作加上前缀。
Signature:
STORE_ID = "@acoustic-content-sdk/redux-store"
Home > @acoustic-content-sdk/redux-store > VERSION
VERSION variable
Version and build number of the package
Signature:
VERSION: {
version: {
major: string;
minor: string;
patch: string;
branch: string;
};
build: Date;
}
首页 > @acoustic-content-sdk/redux-store > ReduxRootState
ReduxRootState type
reducer 的根状态,这是从特征 ID 到特征的映射。
Signature:
export declare type ReduxRootState = Record<string, any>;
Home > @acoustic-content-sdk/redux-store > ReduxRootStore > addFeatureModule
ReduxRootStore.addFeatureModule() method
向根存储注册一个功能模块
签名:
addFeatureModule<RFS = any, FS = any>(aFeature: ReduxFeatureModule<RFS, FS>): ReduxRootStore<FS, A>;
Parameters
Parameter | Type | Description |
---|---|---|
aFeature | ReduxFeatureModule<RFS, FS> |
the feature model |
返回:
ReduxRootStore
Home > @acoustic-content-sdk/redux-store > 有效载荷动作 > payload
PayloadAction.payload property
Signature:
[ACTION_PAYLOAD]: T;
Home > ; @acoustic-content-sdk/redux-store > ReduxFeatureModuleId > id
ReduxFeatureModuleId.id property
ID of the feature module, will also be used as the key to the state
Signature:
id: string;
主页 > @acoustic-content-sdk/redux-store > ReduxFeatureModule > dependencies
ReduxFeatureModule.dependencies property
Dependencies on other modules
Signature:
dependencies?: ArrayLike<ReduxFeatureModule<any>>;
Home< /a> > @acoustic-content-sdk/redux-store > ReduxFeatureModule > epic
ReduxFeatureModule.epic property
The epic for side effects
Signature:
epic?: Epic<Input, Output, ReduxRootState, Dependencies>;
Home > @acoustic-content-sdk/redux-store > ReduxFeatureModule > id
ReduxFeatureModule.id property
ID of the feature module, will also be used as the key to the state
Signature:
id: string;
主页 > @acoustic-content-sdk/redux-store > ReduxFeatureModule > reducer
ReduxFeatureModule.reducer property
模块的减速器。
Signature:
reducer?: Reducer<S>;
Home > @acoustic-content-sdk/redux-store > ReduxRootStoreDependencies > logSvc
ReduxRootStoreDependencies.logSvc property
Service that allows to create logger instances
Signature:
logSvc: LoggerService;
首页 > @acoustic-content-sdk/redux-store > ReduxRootStoreDependencies > rootStore
ReduxRootStoreDependencies.rootStore property
根存储依赖
项签名
rootStore: ReduxRootStore;