UNPKG

tauri-settings

Version:

A user settings manager for Tauri inspired by electron-settings.

111 lines (110 loc) 4.67 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { STATUS } from '../fs/ensure-settings-file'; import { getSettings, saveSettings } from '../fs/load-save'; import { get, set } from '../settings/getter-setter'; import { getDotNotation, setDotNotation } from '../utils/dot-notation'; export class SettingsManager { constructor(defaultSettings, options = {}) { this.default = Object.assign({}, defaultSettings); this.options = Object.assign({}, options); } /** * Initializes a settings file with the defaults. If settings exist, load them. * @returns The entire settings object */ initialize() { return __awaiter(this, void 0, void 0, function* () { const currentSettings = yield getSettings(this.options); this.path = currentSettings.path; if (currentSettings.status === STATUS.FILE_CREATED) { this.settings = Object.assign({}, this.default); yield this.saveSettings(); } else if (currentSettings.status === STATUS.FILE_EXISTS) { this.settings = Object.assign(Object.assign({}, this.default), currentSettings.settings); } return this.settings; }); } /** * @internal */ saveSettings() { return __awaiter(this, void 0, void 0, function* () { yield saveSettings(this.settings, this.path, this.options); }); } /** * Checks whether a key exists in the settings cache. * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. */ hasCache(key) { return getDotNotation(this.settings, key) !== null; } /** * Gets the value of a setting from the cache. * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. * @returns The value of the setting */ getCache(key) { if (!this.hasCache(key)) throw 'Error: key does not exist'; return getDotNotation(this.settings, key); } /** * Sets the value for a setting. Only updates cache. * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. * @param value The new value for the setting * @returns The entire settings object */ setCache(key, value) { if (!this.hasCache(key)) throw 'Error: key does not exist'; setDotNotation(this.settings, key, value); return value; } /** * Gets the value of a setting directly from the storage. Also updates cache. * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. * @returns The value of the setting */ get(key) { return __awaiter(this, void 0, void 0, function* () { const value = yield get(key, this.options); // to also update cache this.setCache(key, value); return value; }); } /** * Sets the value for a setting directly to the storage. Also updates cache. * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. * @param value The new value for the setting * @returns The entire settings object */ set(key, value) { return __awaiter(this, void 0, void 0, function* () { // to also update cache this.setCache(key, value); return yield set(key, value, this.options); }); } /** * Saves the current settings cache to the storage. * @returns The entire settings object */ syncCache() { return __awaiter(this, void 0, void 0, function* () { yield this.saveSettings(); return this.settings; }); } }