-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.ts
30 lines (21 loc) · 1.01 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { DestroyCallback } from "./Broadcaster";
export type DataStoreActionReturn<TDataType> = Partial<TDataType> | Promise<Partial<TDataType>>;
export type DataStoreActions<TDataType> = Record<any, (state: TDataType, ...args: any[]) => DataStoreActionReturn<TDataType>>;
// Ditch the first parameter argument of a function
export type OmitFirstArg<F extends (state: any, ...args: any[]) => any> =
F extends (state: any, ...args: infer P) => ReturnType<F>
? (...args: P) => ReturnType<F>
: F;
export type AsyncReturn = { async: Promise<boolean> };
export type AsyncDataStoreActions<TDataType, TActions extends DataStoreActions<TDataType>> = {
[K in keyof TActions]: OmitFirstArg<(...args: Parameters<TActions[K]>) => AsyncReturn>
};
export interface IDataStore<TDataType, TObserverProps, T extends DataStoreActions<TDataType>>
{
state: TDataType
actions: AsyncDataStoreActions<TDataType, T>;
observe(
callback: (newData: TDataType) => void,
props?: TObserverProps,
updateImmediately?: boolean): DestroyCallback
}