import { equals } from 'ramda'; import { Observable, Subject } from 'rxjs'; import { share, takeUntil, distinctUntilChanged } from 'rxjs/operators'; import * as t from './types'; export type ISettingsClientArgs = { change$: Subject; getKeys: t.GetSettingsKeys; getValues: t.GetSettingsValues; setValues: t.SetSettingsValues; openInEditor: t.OpenSettings; openFolder: t.OpenSettings; namespace: string; }; /** * An abstract representation of the configuration settings store * that works on either the [main] or [renderer] processes. */ export class SettingsClient implements t.ISettingsClient { /** * [Fields] */ private readonly _args: ISettingsClientArgs; private readonly _dispose$ = new Subject(); public readonly dispose$ = this._dispose$.pipe(share()); public readonly change$: Observable>; /** * [Lifecycle] */ constructor(args: ISettingsClientArgs) { this._args = args; this.change$ = args.change$.pipe( takeUntil(this.dispose$), distinctUntilChanged((prev, next) => equals(prev, next)), share(), ); } /** * Disposes of the client. */ public dispose() { this._dispose$.next(); this._dispose$.complete(); } public get isDisposed() { return this._dispose$.isStopped; } /** * [Methods] */ /** * Retrieves an object with values for the given keys. * Pass no keys to retrieve all values. */ public async read(...keys: (keyof T)[]) { keys = formatKeys({ keys, namespace: this._args.namespace }); return (await this._args.getValues(keys)) as Partial; } /** * Writes values to disk. */ public async write(...values: t.ISettingsKeyValue[]) { values = formatValues({ values, namespace: this._args.namespace }); const res = await this._args.setValues(values, 'UPDATE'); return res as t.ISettingsSetValuesResponse; } /** * Retrieves all available keys. */ public async keys(): Promise<(keyof T)[]> { return this._args.getKeys(); } /** * Retrieves the value at the given key from storage. */ public async get(key: K, defaultValue?: T[K]) { const k = formatKey({ key, namespace: this._args.namespace }); const res = await this.read(key); const value = res[k] === undefined ? defaultValue : res[k]; return value as T[K]; } /** * Saves the given value. */ public async put(key: K, value: T[K]) { await this.write({ key, value }); return value; } /** * Deletes the given key(s). */ public async delete(...keys: (keyof T)[]) { keys = formatKeys({ keys, namespace: this._args.namespace }); const values = keys.map((key) => ({ key, value: undefined })); await this._args.setValues(values, 'DELETE'); } /** * Clears all keys. */ public async clear() { const keys = await this.keys(); if (keys.length > 0) { await this.delete(...keys); } } /** * Opens the settings JSON in an external editor. */ public openInEditor() { this._args.openInEditor(); return this; } /** * Opens the folder containing the settings JSON. */ public openFolder() { this._args.openFolder(); return this; } /** * Retrives a clone of the that prefixes keys with the given namespace. */ public namespace(namespace: string) { const args = { ...this._args, namespace }; return new SettingsClient(args); } } /** * [Helpers] */ function formatKey(args: { key: keyof T; namespace: string }) { const namespace = (args.namespace || '').trim(); let key = args.key.toString(); if (namespace) { const prefix = `${namespace}:`; key = key.replace(new RegExp(`^${prefix}`), ''); key = namespace ? `${prefix}${key}` : key; } return key as keyof T; } function formatKeys(args: { keys: (keyof T)[]; namespace: string }) { const namespace = args.namespace; return args.keys.map((key) => formatKey({ key, namespace })); } function formatValues(args: { values: t.ISettingsKeyValue[]; namespace: string; }) { const namespace = args.namespace; return args.values.map((item) => ({ key: formatKey({ key: item.key, namespace }), value: item.value, })); }