Skip to content

Commit

Permalink
Add select function api (#10)
Browse files Browse the repository at this point in the history
* Add select function api.

* Use select in useStaat hook
  • Loading branch information
ericmackrodt committed Jul 2, 2019
1 parent f792d06 commit 50f2cae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
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

0 comments on commit 50f2cae

Please sign in to comment.