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

Add select function api #10

Merged
merged 2 commits into from
Jul 2, 2019
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
19 changes: 19 additions & 0 deletions packages/core/src/__tests__/staat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Staat, LegacyStaat } from '../types';

type TestState = {
count: number;
name?: string;
};

const state: TestState = {
Expand Down Expand Up @@ -140,5 +141,23 @@ describe('staat', () => {
await new Promise(resolve => setTimeout(resolve, 1));
expect(subscription).toHaveBeenCalledTimes(1);
});

describe('select', () => {
it('gets the whole state if no parameter is passed', () => {
expect(sut.select()).toBe(state);
});

it('gets single value', () => {
sut.reduce(s => ({ ...s, count: 77 }));
expect(sut.select(s => s.count)).toBe(77);
});

it('gets different object with value', () => {
sut.reduce(s => ({ ...s, count: 77, name: 'test' }));
expect(sut.select(s => ({ result: s.name }))).toEqual({
result: 'test',
});
});
});
});
});
18 changes: 13 additions & 5 deletions packages/core/src/staat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TransformerOrObject,
LegacyStaat,
RequesterState,
Select,
} from './types';
import { isPromise, isTransformer } from './utils';

Expand Down Expand Up @@ -46,11 +47,12 @@ function makeReduce<TState>(container: StateContainer<TState>) {
};
}

function makeSelect<TState>(
container: StateContainer<TState>,
): <TSubset>(selector: (state: TState) => TSubset) => TSubset {
return selector => {
function makeSelect<TState>(container: StateContainer<TState>): Select<TState> {
return <TSubset>(selector?: (state: TState) => TSubset | TState) => {
const state = container.getState();
if (!selector) {
return state;
}
return selector(state);
};
}
Expand All @@ -73,11 +75,17 @@ function initializeObject<TState>(
subscribe: container.subscribe.bind(container),
unsubscribe: container.unsubscribe.bind(container),
reduce: makeReduce(container),
select: makeSelect(container),
request: makeRequester(container),
};

Object.defineProperty(obj, 'currentState', {
get: () => container.getState(),
get: () => {
console.warn(
'[Staat - Warning] currentState will no longer be supported, use the select function instead',
);
return container.getState();
},
});
return obj as StateContainerType<TState>;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ export type RequesterState<TState> = {
): TState;
};

export interface Select<TState> {
(): TState;
<TSubset>(selector: (state: TState) => TSubset): TSubset;
}

export type StateContainerType<TState> = {
currentState: TState;
select: Select<TState>;
reduce<TArgs extends any[]>(
reducer: (state: TState, ...args: TArgs) => TState,
...args: TArgs
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function makeUseStaat<TState>(staat: Staat<TState>) {
return;
}

const newState = selector(staat.currentState);
const newState = staat.select(selector);

setState((oldState: TSubset) => {
if (!shallowEqual(oldState, newState)) {
Expand Down