Skip to content

Commit

Permalink
Add requester apis (#9)
Browse files Browse the repository at this point in the history
* Fix react tests

* Add requester functions

* Add requester.
  • Loading branch information
ericmackrodt committed Jul 2, 2019
1 parent 07e889e commit f792d06
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
42 changes: 42 additions & 0 deletions packages/core/src/__tests__/requester.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import staat from '../staat';
import { Staat } from '../types';

type TestState = {
count: number;
};

const state: TestState = {
count: 37,
};

describe('requester', () => {
let sut: Staat<TestState>;
beforeEach(() => {
sut = staat(state);
});

it('selects from state', async () => {
let value: number = 10;
await sut.request(async ({ select }) => {
value = select(s => s.count);
});

expect(value).toBe(37);
});

it('reduces state', async () => {
await sut.request(async ({ reduce }) => {
reduce(s => ({ ...s, count: 99 }));
});

expect(sut.currentState.count).toBe(99);
});

it('accepts arguments', async () => {
await sut.request(async ({ reduce }, value: number) => {
reduce(s => ({ ...s, count: value }));
}, 489);

expect(sut.currentState.count).toBe(489);
});
});
1 change: 1 addition & 0 deletions packages/core/src/__tests__/staat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('staat', () => {
expect(typeof sut.subscribe).toBe('function');
expect(typeof sut.unsubscribe).toBe('function');
expect(typeof sut.reduce).toBe('function');
expect(typeof sut.request).toBe('function');
});

it('reduces the value', () => {
Expand Down
31 changes: 27 additions & 4 deletions packages/core/src/staat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Staat,
TransformerOrObject,
LegacyStaat,
RequesterState,
} from './types';
import { isPromise, isTransformer } from './utils';

Expand Down Expand Up @@ -45,17 +46,39 @@ function makeReduce<TState>(container: StateContainer<TState>) {
};
}

function makeSelect<TState>(
container: StateContainer<TState>,
): <TSubset>(selector: (state: TState) => TSubset) => TSubset {
return selector => {
const state = container.getState();
return selector(state);
};
}

function makeRequester<TState>(container: StateContainer<TState>) {
return <TArgs extends any[]>(
requester: (state: RequesterState<TState>, ...args: TArgs) => Promise<void>,
...args: TArgs
): Promise<void> => {
const reduce = makeReduce<TState>(container);
const select = makeSelect<TState>(container);
return requester({ reduce, select }, ...args);
};
}

function initializeObject<TState>(
container: StateContainer<TState>,
): StateContainerType<TState> {
const obj: Partial<StateContainerType<TState>> = {};
const obj: Partial<StateContainerType<TState>> = {
subscribe: container.subscribe.bind(container),
unsubscribe: container.unsubscribe.bind(container),
reduce: makeReduce(container),
request: makeRequester(container),
};

Object.defineProperty(obj, 'currentState', {
get: () => container.getState(),
});
obj.subscribe = container.subscribe.bind(container);
obj.unsubscribe = container.unsubscribe.bind(container);
obj.reduce = makeReduce(container);
return obj as StateContainerType<TState>;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ export type TransformersTree<TTransformers extends {}> = {
: TTransformers[TKey]
};

export type RequesterState<TState> = {
select<TSubset>(selector: (state: TState) => TSubset): TSubset;
reduce<TArgs extends any[]>(
reducer: (state: TState, ...args: TArgs) => TState,
...args: TArgs
): TState;
};

export type StateContainerType<TState> = {
currentState: TState;
reduce<TArgs extends any[]>(
reducer: (state: TState, ...args: TArgs) => TState,
...args: TArgs
): TState;
request<TArgs extends any[]>(
requester: (state: RequesterState<TState>, ...args: TArgs) => Promise<void>,
...args: TArgs
): Promise<void>;
subscribe(fn: Subscription): void;
unsubscribe(fn: Subscription): void;
};
Expand Down

0 comments on commit f792d06

Please sign in to comment.