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

Commit

Permalink
refactor: Рефакторинг типов для IPC
Browse files Browse the repository at this point in the history
  • Loading branch information
cawa-93 committed May 20, 2021
1 parent bf5d117 commit 71dc1c0
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"test": "node tests/app.spec.js",
"watch": "node scripts/watch.js",
"lint": "eslint . --ext js,ts,vue",
"typecheck": "node scripts/buildEnvTypes.js && concurrently \"npm:typecheck:*\"",
"typecheck": "node scripts/buildEnvTypes.js && concurrently --raw \"npm:typecheck:*\"",
"typecheck:main": "tsc --noEmit -p packages/main/tsconfig.json",
"typecheck:preload": "tsc --noEmit -p packages/preload/tsconfig.json",
"typecheck:renderer": "vue-tsc --noEmit -p packages/renderer/tsconfig.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/ipc/DialogsHost.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {DialogsControllers} from '../../../shared/types/ipc/DialogsControllers';
import type {DialogsControllers} from '/@shared/types/ipc/DialogsControllers';
import {dialog} from 'electron';

class DialogsHost implements DialogsControllers {
Expand Down
8 changes: 7 additions & 1 deletion packages/shared/types/ipc/DialogsControllers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export interface DialogsControllers {
import type {HostBase, MethodOnlyHost} from './Host';


interface DialogsControllersBase extends HostBase {
showError: (title: string, content: string) => void
}


export type DialogsControllers = MethodOnlyHost<DialogsControllersBase>
5 changes: 5 additions & 0 deletions packages/shared/types/ipc/Host.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type {ClearIndex, Fn, NonFunctionKeys} from '../utils';


export type HostBase = Record<string, Fn>
export type MethodOnlyHost<T extends HostBase> = Omit<ClearIndex<T>, NonFunctionKeys<ClearIndex<T>>>
8 changes: 7 additions & 1 deletion packages/shared/types/ipc/WindowControllers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export interface WindowControllers {
import type {HostBase, MethodOnlyHost} from './Host';


interface WindowControllersBase extends HostBase {
minimize: () => void
maximize: () => void
unmaximize: () => void
close: () => void
isMaximized: () => boolean
}


export type WindowControllers = MethodOnlyHost<WindowControllersBase>
8 changes: 7 additions & 1 deletion packages/shared/types/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type {WindowControllers} from './WindowControllers';
import type {DialogsControllers} from './DialogsControllers';
import type {HostBase} from './Host';
import type {ClearIndex} from '../utils';

export interface IpcNameHostsMap {


interface IpcNameHostsMapBase extends Record<string, HostBase> {
'WindowControllers': WindowControllers
'DialogsControllers': DialogsControllers
}


export type IpcNameHostsMap = ClearIndex<IpcNameHostsMapBase>
55 changes: 53 additions & 2 deletions packages/shared/types/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
export type Promisified<T> = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly [P in keyof T]: T[P] extends (...a: any[]) => any ? (...a: Parameters<T[P]>) => Promise<ReturnType<T[P]>> : Promise<T[P]>;
readonly [P in keyof T]: T[P] extends Fn ? (...a: Parameters<T[P]>) => Promise<ReturnType<T[P]>> : Promise<T[P]>;
}

/**
* NonUndefined
* @see https://github.com/piotrwitek/utility-types#nonundefineda
* @desc Exclude undefined from set `A`
* @example
* // Expect: "string | null"
* SymmetricDifference<string | null | undefined>;
*/
export type NonUndefined<A> = A extends undefined ? never : A;


/**
* FunctionKeys
* @see https://github.com/piotrwitek/utility-types#functionkeyst
* @desc Get union type of keys that are functions in object type `T`
* @example
* type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;};
*
* // Expect: "setName | someFn"
* type Keys = FunctionKeys<MixedProps>;
*/
export declare type FunctionKeys<T extends Record<string, unknown>> = {
[K in keyof T]-?: NonUndefined<T[K]> extends Fn ? K : never;
}[keyof T];



/**
* NonFunctionKeys
* @see https://github.com/piotrwitek/utility-types#nonfunctionkeyst
* @desc Get union type of keys that are non-functions in object type `T`
* @example
* type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;};
*
* // Expect: "name | someKey"
* type Keys = NonFunctionKeys<MixedProps>;
*/
export declare type NonFunctionKeys<T extends Record<string, unknown>> = {
[K in keyof T]-?: NonUndefined<T[K]> extends Fn ? never : K;
}[keyof T];



// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Fn = (...args: any[]) => any



export type ClearIndex<T> = {
[P in keyof T as string extends P ? never : number extends P ? never : P]: T[P]
};

0 comments on commit 71dc1c0

Please sign in to comment.