Skip to content

Commit

Permalink
Allow passing selector creators with different memoize options
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Oct 29, 2023
1 parent 2b6d65e commit 30c5ae7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
12 changes: 10 additions & 2 deletions packages/toolkit/src/entities/state_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import type { CreateSelectorFunction, Selector, createSelector } from 'reselect'
import { createDraftSafeSelector } from '../createDraftSafeSelector'
import type { EntityState, EntitySelectors, EntityId } from './models'

type AnyFunction = (...args: any) => any
type AnyCreateSelectorFunction = CreateSelectorFunction<
<F extends AnyFunction>(f: F) => F,
<F extends AnyFunction>(f: F) => F
>

export interface GetSelectorsOptions {
// TODO Review if this causes issues or if we can go back to using `CreateSelectorFunction`
createSelector?: typeof createSelector
createSelector?: AnyCreateSelectorFunction
}

export function createSelectorsFactory<T, Id extends EntityId>() {
Expand All @@ -20,7 +26,9 @@ export function createSelectorsFactory<T, Id extends EntityId>() {
selectState?: (state: V) => EntityState<T, Id>,
options: GetSelectorsOptions = {}
): EntitySelectors<T, any, Id> {
const { createSelector = createDraftSafeSelector } = options
let { createSelector } = options
createSelector ??= createDraftSafeSelector

const selectIds = (state: EntityState<T, Id>) => state.ids

const selectEntities = (state: EntityState<T, Id>) => state.entities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
import { createNextState } from '../..'

describe('Sorted State Adapter', () => {
let adapter: EntityAdapter<string, BookModel>
let state: EntityState<string, BookModel>
let adapter: EntityAdapter<BookModel, string>
let state: EntityState<BookModel, string>

beforeAll(() => {
//eslint-disable-next-line
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('Sorted State Adapter', () => {
order: number
ts: number
}
const sortedItemsAdapter = createEntityAdapter<string, OrderedEntity>({
const sortedItemsAdapter = createEntityAdapter<OrderedEntity>({
sortComparer: (a, b) => a.order - b.order,
})
const withInitialItems = sortedItemsAdapter.setAll(
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/entities/tests/state_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createSlice } from '../../createSlice'
import type { BookModel } from './fixtures/book'

describe('createStateOperator', () => {
let adapter: EntityAdapter<string, BookModel>
let adapter: EntityAdapter<BookModel, string>

beforeEach(() => {
adapter = createEntityAdapter({
Expand Down
5 changes: 4 additions & 1 deletion packages/toolkit/src/entities/tests/state_selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ describe('Entity State Selectors', () => {
})
describe('custom createSelector instance', () => {
it('should use the custom createSelector function if provided', () => {
const memoizeSpy = vi.fn(weakMapMemoize)
const memoizeSpy = vi.fn(
// test that we're allowed to pass memoizers with different options, as long as they're optional
<F extends (...args: any[]) => any>(fn: F, param?: boolean) => fn
)
const createCustomSelector = createDraftSafeSelectorCreator(memoizeSpy)

const adapter = createEntityAdapter({
Expand Down

0 comments on commit 30c5ae7

Please sign in to comment.