mirror of
https://github.com/koloml/furbooru-tagging-assistant.git
synced 2025-12-24 15:12:58 +00:00
Implementing the classes to manipulate settings
This commit is contained in:
82
src/lib/extension/ConfigurationController.js
Normal file
82
src/lib/extension/ConfigurationController.js
Normal file
@@ -0,0 +1,82 @@
|
||||
import StorageHelper from "$lib/chrome/StorageHelper.js";
|
||||
|
||||
export default class ConfigurationController {
|
||||
/** @type {string} */
|
||||
#configurationName;
|
||||
|
||||
/**
|
||||
* @param {string} configurationName Name of the configuration to work with.
|
||||
*/
|
||||
constructor(configurationName) {
|
||||
this.#configurationName = configurationName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the setting with the given name.
|
||||
*
|
||||
* @param {string} settingName Setting name.
|
||||
* @param {any} [defaultValue] Default value to return if the setting does not exist. Defaults to `null`.
|
||||
*
|
||||
* @return {Promise<any|null>} The setting value or the default value if the setting does not exist.
|
||||
*/
|
||||
async readSetting(settingName, defaultValue = null) {
|
||||
const settings = await ConfigurationController.#storageHelper.read(this.#configurationName, {});
|
||||
return settings[settingName] ?? defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the given value to the setting.
|
||||
*
|
||||
* @param {string} settingName Setting name.
|
||||
* @param {any} value Value to write.
|
||||
*
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async writeSetting(settingName, value) {
|
||||
const settings = await ConfigurationController.#storageHelper.read(this.#configurationName, {});
|
||||
|
||||
settings[settingName] = value;
|
||||
|
||||
ConfigurationController.#storageHelper.write(this.#configurationName, settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the specific setting.
|
||||
*
|
||||
* @param {string} settingName Setting name to delete.
|
||||
*
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async deleteSetting(settingName) {
|
||||
const settings = await ConfigurationController.#storageHelper.read(this.#configurationName, {});
|
||||
|
||||
delete settings[settingName];
|
||||
|
||||
ConfigurationController.#storageHelper.write(this.#configurationName, settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to changes in the configuration.
|
||||
*
|
||||
* @param {function(Record<string, any>)} callback Callback to call when the configuration changes. The new
|
||||
* configuration is passed as an argument.
|
||||
*
|
||||
* @return {function(): void} Unsubscribe function.
|
||||
*/
|
||||
subscribeToChanges(callback) {
|
||||
/** @param {Record<string, StorageChange>} changes */
|
||||
const changesSubscriber = changes => {
|
||||
if (!changes[this.#configurationName]) {
|
||||
return;
|
||||
}
|
||||
|
||||
callback(changes[this.#configurationName].newValue);
|
||||
}
|
||||
|
||||
ConfigurationController.#storageHelper.subscribe(changesSubscriber);
|
||||
|
||||
return () => ConfigurationController.#storageHelper.unsubscribe(changesSubscriber);
|
||||
}
|
||||
|
||||
static #storageHelper = new StorageHelper(chrome.storage.local);
|
||||
}
|
||||
87
src/lib/extension/settings/MaintenanceSettings.js
Normal file
87
src/lib/extension/settings/MaintenanceSettings.js
Normal file
@@ -0,0 +1,87 @@
|
||||
import ConfigurationController from "$lib/extension/ConfigurationController.js";
|
||||
import MaintenanceProfile from "$lib/extension/entities/MaintenanceProfile.js";
|
||||
|
||||
export default class MaintenanceSettings {
|
||||
#isInitialized = false;
|
||||
#activeProfileId = null;
|
||||
|
||||
constructor() {
|
||||
void this.#initializeSettings();
|
||||
}
|
||||
|
||||
async #initializeSettings() {
|
||||
MaintenanceSettings.#controller.subscribeToChanges(settings => {
|
||||
this.#activeProfileId = settings.activeProfile || null;
|
||||
});
|
||||
|
||||
this.#activeProfileId = await MaintenanceSettings.#controller.readSetting("activeProfile", null);
|
||||
this.#isInitialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the active maintenance profile.
|
||||
*
|
||||
* @return {Promise<string|null>}
|
||||
*/
|
||||
async resolveActiveProfileId() {
|
||||
if (!this.#isInitialized && !this.#activeProfileId) {
|
||||
this.#activeProfileId = await MaintenanceSettings.#controller.readSetting(
|
||||
"activeProfile",
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
if (!this.#activeProfileId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.#activeProfileId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the active maintenance profile if it is set.
|
||||
*
|
||||
* @return {Promise<MaintenanceProfile|null>}
|
||||
*/
|
||||
async resolveActiveProfileAsObject() {
|
||||
const resolvedProfileId = await this.resolveActiveProfileId();
|
||||
|
||||
return (await MaintenanceProfile.readAll())
|
||||
.find(profile => profile.id === resolvedProfileId) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the active maintenance profile.
|
||||
*
|
||||
* @param {string|null} profileId ID of the profile to set as active. If `null`, the active profile will be considered
|
||||
* unset.
|
||||
*
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async setActiveProfileId(profileId) {
|
||||
this.#activeProfileId = profileId;
|
||||
|
||||
await MaintenanceSettings.#controller.writeSetting("activeProfile", profileId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Controller for interaction with the settings stored in the extension's storage.
|
||||
*
|
||||
* @type {ConfigurationController}
|
||||
*/
|
||||
static #controller = new ConfigurationController("maintenance");
|
||||
|
||||
/**
|
||||
* Subscribe to the changes in the maintenance-related settings.
|
||||
*
|
||||
* @param {function({activeProfileId: string|null}): void} callback Callback to call when the settings change. The new settings are
|
||||
* passed as an argument.
|
||||
*
|
||||
* @return {function(): void} Unsubscribe function.
|
||||
*/
|
||||
static subscribe(callback) {
|
||||
return MaintenanceSettings.#controller.subscribeToChanges(settings => {
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user