From 3e5266ca7b6c8c0be21221ce314948e0f88e896e Mon Sep 17 00:00:00 2001 From: KoloMl Date: Sun, 21 Jun 2026 15:29:42 +0400 Subject: [PATCH] Expose storage in EntitiesController for testing purposes - Storage can now be mocked - Class can be loaded even if chrome storage is not present globally - It will not throw an error if storage not found in runtime --- src/lib/extension/EntitiesController.ts | 36 +++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/lib/extension/EntitiesController.ts b/src/lib/extension/EntitiesController.ts index 8a680e5..8c4978c 100644 --- a/src/lib/extension/EntitiesController.ts +++ b/src/lib/extension/EntitiesController.ts @@ -2,7 +2,9 @@ import StorageHelper, { type StorageChangeSubscriber } from "$lib/browser/Storag import type StorageEntity from "$lib/extension/base/StorageEntity"; export default class EntitiesController { - static #storageHelper = new StorageHelper(chrome.storage.local); + static storage: StorageHelper | null = typeof chrome !== 'undefined' + ? new StorageHelper(chrome.storage.local) + : null; /** * Read all entities of the given type from the storage. Build the entities from the raw data and return them. @@ -14,7 +16,11 @@ export default class EntitiesController { * @return List of entities of the given type. */ static async readAllEntities>(entityName: string, entityClass: new (...any: any[]) => Type): Promise { - const rawEntities = await this.#storageHelper.read(entityName, {}); + if (!this.storage) { + throw new Error('Missing storage!'); + } + + const rawEntities = await this.storage.read(entityName, {}); if (!rawEntities || Object.keys(rawEntities).length === 0) { return []; @@ -32,10 +38,14 @@ export default class EntitiesController { * @param entity Entity to update. */ static async updateEntity(entityName: string, entity: StorageEntity): Promise { - this.#storageHelper.write( + if (!this.storage) { + throw new Error('Missing storage!'); + } + + this.storage.write( entityName, Object.assign( - await this.#storageHelper.read( + await this.storage.read( entityName, {} ), { @@ -52,9 +62,13 @@ export default class EntitiesController { * @param entityId ID of the entity to delete. */ static async deleteEntity(entityName: string, entityId: string): Promise { - const entities = await this.#storageHelper.read(entityName, {}); + if (!this.storage) { + throw new Error('Missing storage!'); + } + + const entities = await this.storage.read(entityName, {}); delete entities[entityId]; - this.#storageHelper.write(entityName, entities); + this.storage.write(entityName, entities); } /** @@ -68,6 +82,12 @@ export default class EntitiesController { * @return Unsubscribe function. */ static subscribeToEntity>(entityName: string, entityClass: new (...any: any[]) => Type, callback: (entities: Type[]) => void): () => void { + if (!this.storage) { + throw new Error('Missing storage!'); + } + + const storage = this.storage; + /** * Watch the changes made to the storage and call the callback when the entity changes. */ @@ -80,8 +100,8 @@ export default class EntitiesController { .then(callback); } - this.#storageHelper.subscribe(subscriber); + storage.subscribe(subscriber); - return () => this.#storageHelper.unsubscribe(subscriber); + return () => storage.unsubscribe(subscriber); } }