import { Observable } from 'rxjs'; import { JsonMap } from '@platform/types'; /** * [Client] * An abstract representation of the configuration settings * that works on either the [main] or [renderer] processes. */ export type ISettingsClient = { change$: Observable>; read: (...keys: (keyof T)[]) => Promise>; write: (...values: ISettingsKeyValue[]) => Promise; keys: () => Promise<(keyof T)[]>; get: (key: K, defaultValue?: T[K]) => Promise; put: (key: K, value: T[K]) => Promise; delete: (...keys: (keyof T)[]) => Promise; // eslint-disable-line clear: () => Promise; openInEditor: () => ISettingsClient; openFolder: () => ISettingsClient; namespace(namespace: string): ISettingsClient; }; export type ISettingsKeyValue = { key: keyof T; value: T[keyof T] | undefined; }; export type SettingsJson = JsonMap; export type ISettingsFile = { version: number; body: SettingsJson; }; /** * The settings client with extended [main] properties. */ export type IMainSettingsClient = ISettingsClient & { path: string; }; /** * [Deletages] */ export type SettingsSetAction = 'UPDATE' | 'DELETE'; export type GetSettingsValues = ( keys: (keyof T)[], ) => Promise; export type SetSettingsValues = ( keys: ISettingsKeyValue[], action: SettingsSetAction, ) => Promise; export type GetSettingsKeys = () => Promise<(keyof T)[]>; export type OpenSettings = () => void; /** * [Events]. */ export type SettingsEvent = | ISettingsChangeEvent | ISettingsGetKeysEvent | ISettingsGetValuesEvent | ISettingsSetValuesEvent | ISettingsOpenEvent; export type ISettingsChangeEvent = { type: '@platform/SETTINGS/change'; payload: ISettingsChange; }; export type ISettingsChange = { keys: (keyof T)[]; values: T; action: SettingsSetAction; }; export type ISettingsGetKeysEvent = { type: '@platform/SETTINGS/keys'; payload: Record; // NB: Placeholder {object}. }; export type ISettingsGetValuesEvent = { type: '@platform/SETTINGS/get'; payload: { keys: string[] }; }; export type ISettingsGetValuesResponse = { ok: boolean; exists: boolean; version: number; body: SettingsJson; error?: string; }; export type ISettingsSetValuesEvent = { type: '@platform/SETTINGS/set'; payload: { values: ISettingsKeyValue[]; action: SettingsSetAction }; }; export type ISettingsSetValuesResponse< T extends SettingsJson = any // eslint-disable-line > = { ok: boolean; error?: string; }; export type ISettingsOpenEvent = { type: '@platform/SETTINGS/open'; payload: { target: 'EDITOR' | 'FOLDER' }; };