UNPKG

tauri-settings

Version:

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

105 lines (104 loc) 4.22 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 { has, get, set, hasCache } from './getter-setter'; import { add_config, cache_to_file } from '../utils/handlers'; export class SettingsManager { constructor(defaultSettings, config) { /** @internal */ this.configId = 0; this.default = Object.assign({}, defaultSettings); this.config = config !== null && config !== void 0 ? config : {}; } /** * 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* () { let configId = yield add_config(this.config, this.default); this.configId = configId; }); } /** * @internal */ saveSettings() { return __awaiter(this, void 0, void 0, function* () { yield cache_to_file(this.configId); }); } /** * 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 __awaiter(this, void 0, void 0, function* () { return hasCache(key, this.configId); }); } /** * Sets 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) { return __awaiter(this, void 0, void 0, function* () { return this.default[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 */ setCache(key, value) { return __awaiter(this, void 0, void 0, function* () { this.default[key] = value; }); } /** * Checks whether a key exists in the settings file. * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. */ has(key) { return __awaiter(this, void 0, void 0, function* () { return has(key, this.configId); }); } /** * 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* () { return get(key, this.configId); }); } /** * 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 */ set(key, value) { return __awaiter(this, void 0, void 0, function* () { yield set(key, value, this.configId); }); } /** * 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(); }); } }