Skip to content

Commit

Permalink
feat: UseCustomHook type added to simplify things
Browse files Browse the repository at this point in the history
  • Loading branch information
nmccready committed Aug 22, 2019
1 parent 8183981 commit bad2763
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ function setRef<T, A>(this: Store<T, A>, newState: T): void {

export type HookWork<T = undefined> = () => T;

export type UseCustomHook<T, A, WorkR = undefined> = [T, A] | [T, A, ...WorkR[]];

function useCustom<T, A, WorkR extends WorkR[] = undefined>(
this: Store<T, A>,
React: ReactLib,
hookWork?: HookWork<WorkR>
): [T, A, ...WorkR[]] {
): UseCustomHook<T, A, WorkR> {
const newListener = React.useState()[1];
React.useEffect(() => {
this.listeners.push(newListener);
Expand Down Expand Up @@ -130,7 +132,7 @@ const useStore = <
actions = baseActions,
initializer,
}: UseStoreProps<T, InnerA, OuterA, WorkR> = {} as UseStoreProps<T, InnerA, OuterA, WorkR>
): ((_?: HookWork<WorkR>) => [T, OuterA, ...WorkR[]]) => {
): ((_?: HookWork<WorkR>) => UseCustomHook<T, InnerA, WorkR>) => {
const store = { state: initialState, listeners: [] } as Store<T, OuterA>;
store.setState = setState.bind(store);
store.setRef = setRef.bind(store);
Expand Down

0 comments on commit bad2763

Please sign in to comment.