1
0
mirror of https://github.com/koloml/philomena-tagging-assistant.git synced 2026-09-23 04:19:39 +00:00

Converting storage helper to TS, adding types for subscribe functions

This commit is contained in:
2025-02-07 03:23:21 +04:00
parent 7bb71807bc
commit c6d75e2b2a
3 changed files with 28 additions and 32 deletions

View File

@@ -1,4 +1,4 @@
import StorageHelper from "$lib/browser/StorageHelper";
import StorageHelper, { type StorageChangeSubscriber } from "$lib/browser/StorageHelper";
export default class ConfigurationController {
readonly #configurationName: string;
@@ -18,7 +18,7 @@ export default class ConfigurationController {
*
* @return The setting value or the default value if the setting does not exist.
*/
async readSetting<Type = any, DefaultType = any>(settingName: string, defaultValue: DefaultType|null = null): Promise<Type|DefaultType> {
async readSetting<Type = any, DefaultType = any>(settingName: string, defaultValue: DefaultType | null = null): Promise<Type | DefaultType> {
const settings = await ConfigurationController.#storageHelper.read(this.#configurationName, {});
return settings[settingName] ?? defaultValue;
}
@@ -61,7 +61,7 @@ export default class ConfigurationController {
* @return {function(): void} Unsubscribe function.
*/
subscribeToChanges(callback: (record: Record<string, any>) => void): () => void {
const changesSubscriber = (changes: Record<string, chrome.storage.StorageChange>) => {
const subscriber: StorageChangeSubscriber = changes => {
if (!changes[this.#configurationName]) {
return;
}
@@ -69,9 +69,9 @@ export default class ConfigurationController {
callback(changes[this.#configurationName].newValue);
}
ConfigurationController.#storageHelper.subscribe(changesSubscriber);
ConfigurationController.#storageHelper.subscribe(subscriber);
return () => ConfigurationController.#storageHelper.unsubscribe(changesSubscriber);
return () => ConfigurationController.#storageHelper.unsubscribe(subscriber);
}
static #storageHelper = new StorageHelper(chrome.storage.local);

View File

@@ -1,4 +1,4 @@
import StorageHelper from "$lib/browser/StorageHelper";
import StorageHelper, { type StorageChangeSubscriber } from "$lib/browser/StorageHelper";
import type StorageEntity from "$lib/extension/base/StorageEntity";
export default class EntitiesController {
@@ -71,7 +71,7 @@ export default class EntitiesController {
/**
* Watch the changes made to the storage and call the callback when the entity changes.
*/
const storageChangesSubscriber = (changes: Record<string, chrome.storage.StorageChange>) => {
const subscriber: StorageChangeSubscriber = changes => {
if (!changes[entityName]) {
return;
}
@@ -80,8 +80,8 @@ export default class EntitiesController {
.then(callback);
}
this.#storageHelper.subscribe(storageChangesSubscriber);
this.#storageHelper.subscribe(subscriber);
return () => this.#storageHelper.unsubscribe(storageChangesSubscriber);
return () => this.#storageHelper.unsubscribe(subscriber);
}
}