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

experiment: additional arguments into selectState #990

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions etc/redux-toolkit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,17 @@ export interface EntityAdapter<T> extends EntityStateAdapter<T> {
export type EntityId = number | string;

// @public (undocumented)
export interface EntitySelectors<T, V> {
export interface EntitySelectors<T, V, ExtraArgs extends any[] = []> {
// (undocumented)
selectAll: (state: V) => T[];
selectAll: (state: V, ...args: ExtraArgs) => T[];
// (undocumented)
selectById: (state: V, id: EntityId) => T | undefined;
selectById: (state: V, id: EntityId, ...args: ExtraArgs) => T | undefined;
// (undocumented)
selectEntities: (state: V) => Dictionary<T>;
selectEntities: (state: V, ...args: ExtraArgs) => Dictionary<T>;
// (undocumented)
selectIds: (state: V) => EntityId[];
selectIds: (state: V, ...args: ExtraArgs) => EntityId[];
// (undocumented)
selectTotal: (state: V) => number;
selectTotal: (state: V, ...args: ExtraArgs) => number;
}

// @public (undocumented)
Expand Down
12 changes: 6 additions & 6 deletions src/entities/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ export interface EntityStateAdapter<T> {
/**
* @public
*/
export interface EntitySelectors<T, V> {
selectIds: (state: V) => EntityId[]
selectEntities: (state: V) => Dictionary<T>
selectAll: (state: V) => T[]
selectTotal: (state: V) => number
selectById: (state: V, id: EntityId) => T | undefined
export interface EntitySelectors<T, V, ExtraArgs extends any[] = []> {
selectIds: (state: V, ...args: ExtraArgs) => EntityId[]
selectEntities: (state: V, ...args: ExtraArgs) => Dictionary<T>
selectAll: (state: V, ...args: ExtraArgs) => T[]
selectTotal: (state: V, ...args: ExtraArgs) => number
selectById: (state: V, id: EntityId, ...args: ExtraArgs) => T | undefined
}

/**
Expand Down
17 changes: 8 additions & 9 deletions src/entities/state_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { EntityState, EntitySelectors, Dictionary, EntityId } from './models'

export function createSelectorsFactory<T>() {
function getSelectors(): EntitySelectors<T, EntityState<T>>
function getSelectors<V>(
selectState: (state: V) => EntityState<T>
): EntitySelectors<T, V>
function getSelectors<V, ExtraArgs extends any[]>(
selectState: (state: V, ...args: ExtraArgs) => EntityState<T>
): EntitySelectors<T, V, ExtraArgs>
function getSelectors(
selectState?: (state: any) => EntityState<T>
selectState?: (state: any, ...args: any[]) => EntityState<T>
): EntitySelectors<T, any> {
const selectIds = (state: any) => state.ids

Expand Down Expand Up @@ -40,18 +40,17 @@ export function createSelectorsFactory<T>() {
}
}

const selectGlobalizedEntities = createDraftSafeSelector(
selectState,
selectEntities
)
const selectGlobalizedEntities: (
...args: any[]
) => Dictionary<T> = createDraftSafeSelector(selectState, selectEntities)

return {
selectIds: createDraftSafeSelector(selectState, selectIds),
selectEntities: selectGlobalizedEntities,
selectAll: createDraftSafeSelector(selectState, selectAll),
selectTotal: createDraftSafeSelector(selectState, selectTotal),
selectById: createDraftSafeSelector(
selectGlobalizedEntities,
(state, _, ...args) => selectGlobalizedEntities(state, ...args),
selectId,
selectById
),
Expand Down