Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
ui/react: extract types from hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sokolov committed Mar 2, 2021
1 parent 07513f8 commit 24c0953
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
30 changes: 9 additions & 21 deletions src/ui/react/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import type { DebugConfig, FullDB } from "../../../core";
import type { ReactType } from "../types";
import type {
Changer,
MassUpdater,
MassUpdaterInternal,
Updater,
UpdaterInternal,
} from "./types";

export type { MassUpdater, Updater } from "./types";

function flat<T>(list: T[][]): T[] {
return list.reduce((a, b) => a.concat(b));
}

type MassUpdaterInternal<Domain, K extends keyof Domain> = (
options: {},
kind: K,
id: string,
delta: (prev: Domain[K]) => Domain[K]
) => void;
export type MassUpdater<Domain, K extends keyof Domain> = MassUpdaterInternal<
Domain,
K
> & {
(kind: K, id: string, delta: (prev: Domain[K]) => Domain[K]): void;
};

type UpdaterInternal<T> = (options: {}, delta: (prev: T) => T) => void;
export type Updater<T> = UpdaterInternal<T> & {
(delta: (prev: T) => T): void;
} & (T extends {}
? { <K extends keyof T & string>(field: K, value: T[K]): void }
: {});
type Changer<T> = [T | "loading", Updater<T>];

function makeMassUpdater<Domain, K extends keyof Domain>(
db: FullDB<Domain>
): MassUpdater<Domain, K> {
Expand Down
20 changes: 20 additions & 0 deletions src/ui/react/hooks/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export type MassUpdaterInternal<Domain, K extends keyof Domain> = (
options: {},
kind: K,
id: string,
delta: (prev: Domain[K]) => Domain[K]
) => void;
export type MassUpdater<Domain, K extends keyof Domain> = MassUpdaterInternal<
Domain,
K
> & {
(kind: K, id: string, delta: (prev: Domain[K]) => Domain[K]): void;
};

export type UpdaterInternal<T> = (options: {}, delta: (prev: T) => T) => void;
export type Updater<T> = UpdaterInternal<T> & {
(delta: (prev: T) => T): void;
} & (T extends {}
? { <K extends keyof T & string>(field: K, value: T[K]): void }
: {});
export type Changer<T> = [T | "loading", Updater<T>];

0 comments on commit 24c0953

Please sign in to comment.