Skip to content

Commit 8ad544e

Browse files
committed
Fix runtime implementation of the new version of createSelectorCreator
1 parent 93be6fe commit 8ad544e

File tree

6 files changed

+1122
-105
lines changed

6 files changed

+1122
-105
lines changed

src/types.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CreateSelectorOptions } from 'reselect'
21
import type { MergeParameters } from './versionedTypes'
32
export type { MergeParameters } from './versionedTypes'
43

@@ -120,11 +119,26 @@ export type UnknownMemoizer<Func extends UnknownFunction = UnknownFunction> = (
120119
...options: any[]
121120
) => Func
122121

122+
/**
123+
* Omit any index signatures from the given object type, leaving only explicitly defined properties.
124+
* Source: https://stackoverflow.com/questions/51465182/how-to-remove-index-signature-using-mapped-types/68261113#68261113
125+
* This is mainly used to remove explicit `any`s from the return type of some memoizers. e.g: `microMemoize`
126+
*/
127+
export type OmitIndexSignature<ObjectType> = {
128+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
129+
? never
130+
: KeyType]: ObjectType[KeyType]
131+
}
132+
123133
/** Extracts memoize options from the parameters of a memoizer function. */
124134
export type MemoizeOptsFromParams<MemoizeFunction extends UnknownMemoizer> =
125135
| DropFirst<Parameters<MemoizeFunction>>[0]
126136
| DropFirst<Parameters<MemoizeFunction>>
127137

138+
/** Extract the extra properties that are attached to the return value of a memoizer. e.g.: clearCache */
139+
export type ExtractMemoizerFields<T extends UnknownMemoizer> =
140+
OmitIndexSignature<ReturnType<T>>
141+
128142
/** Extract the return type from all functions as a tuple */
129143
export type ExtractReturnType<T extends readonly AnyFunction[]> = {
130144
[index in keyof T]: T[index] extends T[number] ? ReturnType<T[index]> : never

test/reselect.bench.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
import { createSelector } from '@reduxjs/toolkit'
2-
import { bench } from 'vitest'
3-
import { autotrackMemoize } from '../src/autotrackMemoize/autotrackMemoize'
4-
import { weakMapMemoize } from '../src/weakMapMemoize'
5-
6-
describe('bench', () => {
7-
interface State {
8-
todos: {
9-
id: number
10-
completed: boolean
11-
}[]
12-
}
13-
const state: State = {
14-
todos: [
15-
{ id: 0, completed: false },
16-
{ id: 1, completed: false }
17-
]
18-
}
19-
bench(
20-
'selectorDefault',
21-
() => {
22-
const selectorDefault = createSelector(
23-
(state: State) => state.todos,
24-
todos => todos.map(t => t.id)
25-
)
26-
selectorDefault(state)
27-
},
28-
{ iterations: 500 }
29-
)
30-
31-
bench(
32-
'selectorAutotrack',
33-
() => {
34-
const selectorAutotrack = createSelector(
35-
(state: State) => state.todos,
36-
todos => todos.map(t => t.id),
37-
{ memoize: autotrackMemoize }
38-
)
39-
selectorAutotrack(state)
40-
},
41-
{ iterations: 500 }
42-
)
43-
44-
bench(
45-
'selectorWeakMap',
46-
() => {
47-
const selectorWeakMap = createSelector(
48-
(state: State) => state.todos,
49-
todos => todos.map(t => t.id),
50-
{ memoize: weakMapMemoize }
51-
)
52-
selectorWeakMap(state)
53-
},
54-
{ iterations: 500 }
55-
)
56-
})
1+
import { createSelector } from '@reduxjs/toolkit'
2+
import { bench } from 'vitest'
3+
import { autotrackMemoize } from '../src/autotrackMemoize/autotrackMemoize'
4+
import { weakMapMemoize } from '../src/weakMapMemoize'
5+
6+
describe('bench', () => {
7+
interface State {
8+
todos: {
9+
id: number
10+
completed: boolean
11+
}[]
12+
}
13+
const state: State = {
14+
todos: [
15+
{ id: 0, completed: false },
16+
{ id: 1, completed: false }
17+
]
18+
}
19+
bench(
20+
'selectorDefault',
21+
() => {
22+
const selectorDefault = createSelector(
23+
(state: State) => state.todos,
24+
todos => todos.map(t => t.id)
25+
)
26+
selectorDefault(state)
27+
},
28+
{ iterations: 500 }
29+
)
30+
31+
bench(
32+
'selectorAutotrack',
33+
() => {
34+
const selectorAutotrack = createSelector(
35+
(state: State) => state.todos,
36+
todos => todos.map(t => t.id),
37+
{ memoize: autotrackMemoize }
38+
)
39+
selectorAutotrack(state)
40+
},
41+
{ iterations: 500 }
42+
)
43+
44+
bench(
45+
'selectorWeakMap',
46+
() => {
47+
const selectorWeakMap = createSelector(
48+
(state: State) => state.todos,
49+
todos => todos.map(t => t.id),
50+
{ memoize: weakMapMemoize }
51+
)
52+
selectorWeakMap(state)
53+
},
54+
{ iterations: 500 }
55+
)
56+
})

0 commit comments

Comments
 (0)