mirror of
https://github.com/koloml/philomena-tagging-assistant.git
synced 2026-11-08 00:18:19 +00:00
Converting storage helper to TS, adding types for subscribe functions
This commit is contained in:
1 parent
7bb71807bc
commit
c6d75e2b2a
3 files changed
+28
-32
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user