Spaces:
Sleeping
Sleeping
File size: 2,187 Bytes
b593f0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import type { CreateSelectorFunction, Selector } from 'reselect'
import { createDraftSafeSelector } from '../createDraftSafeSelector'
import type { EntityId, EntitySelectors, EntityState } from './models'
type AnyCreateSelectorFunction = CreateSelectorFunction<any, any, any>
export type GetSelectorsOptions = {
createSelector?: AnyCreateSelectorFunction
}
export function createSelectorsFactory<T, Id extends EntityId>() {
function getSelectors(
selectState?: undefined,
options?: GetSelectorsOptions,
): EntitySelectors<T, EntityState<T, Id>, Id>
function getSelectors<V>(
selectState: (state: V) => EntityState<T, Id>,
options?: GetSelectorsOptions,
): EntitySelectors<T, V, Id>
function getSelectors<V>(
selectState?: (state: V) => EntityState<T, Id>,
options: GetSelectorsOptions = {},
): EntitySelectors<T, any, Id> {
const {
createSelector = createDraftSafeSelector as AnyCreateSelectorFunction,
} = options
const selectIds = (state: EntityState<T, Id>) => state.ids
const selectEntities = (state: EntityState<T, Id>) => state.entities
const selectAll = createSelector(
selectIds,
selectEntities,
(ids, entities): T[] => ids.map((id) => entities[id]!),
)
const selectId = (_: unknown, id: Id) => id
const selectById = (entities: Record<Id, T>, id: Id) => entities[id]
const selectTotal = createSelector(selectIds, (ids) => ids.length)
if (!selectState) {
return {
selectIds,
selectEntities,
selectAll,
selectTotal,
selectById: createSelector(selectEntities, selectId, selectById),
}
}
const selectGlobalizedEntities = createSelector(
selectState as Selector<V, EntityState<T, Id>>,
selectEntities,
)
return {
selectIds: createSelector(selectState, selectIds),
selectEntities: selectGlobalizedEntities,
selectAll: createSelector(selectState, selectAll),
selectTotal: createSelector(selectState, selectTotal),
selectById: createSelector(
selectGlobalizedEntities,
selectId,
selectById,
),
}
}
return { getSelectors }
}
|