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

suggestion: add overload for "solo selector" without input selectors #644

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions src/createSelectorCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ export interface CreateSelectorFunction<
OverrideArgsMemoizeFunction
> &
InterruptRecursion

<
SoloSelector extends (...args: any[]) => any,
OverrideMemoizeFunction extends UnknownMemoizer = MemoizeFunction,
OverrideArgsMemoizeFunction extends UnknownMemoizer = ArgsMemoizeFunction
>(
selector: SoloSelector,
createSelectorOptions?: Simplify<
CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
>
>
): OutputSelector<
ParametersToVirtualInputSelectors<Parameters<SoloSelector>>,
ReturnType<SoloSelector>,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
> &
InterruptRecursion
}

type ParametersToVirtualInputSelectors<T extends [...any]> = {
[K in keyof T]: K extends keyof Array<any> ? T[K] : (...params: T) => T[K]
}

let globalStabilityCheck: StabilityCheckFrequency = 'once'
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export function collectInputSelectorResults(
) {
const inputSelectorResults = []
const { length } = dependencies
if (!length) {
// no input selectors -> only one selector function was passed to `createSelector`
// pass all arguments to that selector function
return [...inputSelectorArgs]
}
for (let i = 0; i < length; i++) {
// @ts-ignore
// apply arguments instead of spreading and mutate a local list of params for performance.
Expand Down
5 changes: 5 additions & 0 deletions test/reselect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ describe('Basic selector behavior', () => {
expect(selector.recomputations()).toBe(2)
})

test('only a result function is passed in, no input selectors', () => {
const selector = createSelector((a: string, b: number, c: string) => `${a}|${b}|${c}`)
expect(selector('foo', 3, 'baz')).toBe('foo|3|baz')
})

test('can accept props', () => {
let called = 0
const selector = createSelector(
Expand Down
Loading