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

feat(store): add clearResult to reset a mock selector #2270

Merged
merged 1 commit into from
Jan 8, 2020
Merged
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
14 changes: 11 additions & 3 deletions modules/store/src/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type MemoizedProjection = {
memoized: AnyFn;
reset: () => void;
setResult: (result?: any) => void;
clearResult: () => void;
};

export type MemoizeFn = (t: AnyFn) => MemoizedProjection;
Expand All @@ -23,6 +24,7 @@ export interface MemoizedSelector<
release(): void;
projector: ProjectorFn;
setResult: (result?: Result) => void;
clearResult: () => void;
}

export interface MemoizedSelectorWithProps<
Expand All @@ -34,6 +36,7 @@ export interface MemoizedSelectorWithProps<
release(): void;
projector: ProjectorFn;
setResult: (result?: Result) => void;
clearResult: () => void;
}

export function isEqualCheck(a: any, b: any): boolean {
Expand Down Expand Up @@ -76,13 +79,17 @@ export function defaultMemoize(
}

function setResult(result: any = undefined) {
overrideResult = result;
overrideResult = { result };
}

function clearResult() {
overrideResult = undefined;
}

// tslint:disable-next-line:no-any anything could be the result.
function memoized(): any {
if (overrideResult !== undefined) {
return overrideResult;
return overrideResult.result;
}

if (!lastArguments) {
Expand All @@ -107,7 +114,7 @@ export function defaultMemoize(
return newResult;
}

return { memoized, reset, setResult };
return { memoized, reset, setResult, clearResult };
}

export function createSelector<State, S1, Result>(
Expand Down Expand Up @@ -590,6 +597,7 @@ export function createSelectorFactory(
release,
projector: memoizedProjector.memoized,
setResult: memoizedState.setResult,
clearResult: memoizedState.clearResult,
});
};
}
Expand Down
2 changes: 1 addition & 1 deletion modules/store/testing/src/mock_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class MockStore<T> extends Store<T> {
MockStore.selectors.forEach((_, selector) => {
if (typeof selector !== 'string') {
selector.release();
selector.setResult();
selector.clearResult();
}
});

Expand Down