From 6a9006ad5270d66b7aac508616f1035f16f26634 Mon Sep 17 00:00:00 2001 From: Zack Williamson Date: Sat, 28 Dec 2024 03:21:18 -0500 Subject: [PATCH] chore: refactor sort tags, add SortTagManager --- .../electron/events/when-ready.js | 4 + src/backend/sort-tags/sort-tag-manager.ts | 114 ++++++++++++++++++ src/gui/app/services/sort-tags.service.js | 113 +++-------------- 3 files changed, 135 insertions(+), 96 deletions(-) create mode 100644 src/backend/sort-tags/sort-tag-manager.ts diff --git a/src/backend/app-management/electron/events/when-ready.js b/src/backend/app-management/electron/events/when-ready.js index 65108e0f0..4bd3d4a80 100644 --- a/src/backend/app-management/electron/events/when-ready.js +++ b/src/backend/app-management/electron/events/when-ready.js @@ -180,6 +180,10 @@ exports.whenReady = async () => { customVariableManager.loadVariablesFromFile(); } + windowManagement.updateSplashScreenStatus("Loading sort tags..."); + const { SortTagManager } = require("../../../sort-tags/sort-tag-manager"); + SortTagManager.loadSortTags(); + // get importer in memory windowManagement.updateSplashScreenStatus("Loading importers..."); diff --git a/src/backend/sort-tags/sort-tag-manager.ts b/src/backend/sort-tags/sort-tag-manager.ts new file mode 100644 index 000000000..be72e01d1 --- /dev/null +++ b/src/backend/sort-tags/sort-tag-manager.ts @@ -0,0 +1,114 @@ +import logger from "../logwrapper"; +import profileManager from "../common/profile-manager"; +import frontendCommunicator from "../common/frontend-communicator"; +import { SettingsManager } from "../common/settings-manager"; + +interface SortTag { + id: string; + name: string; +} + +interface SortTagCache { + [context: string]: SortTag[] +} + +class SortTagManager { + sortTags: SortTagCache = { }; + + constructor() { + frontendCommunicator.on("sort-tags:get-sort-tags", () => { + return this.sortTags ?? { }; + }); + + frontendCommunicator.on("sort-tags:save-sort-tags", (request: { context: string, sortTags: SortTag[] }) => { + this.saveSortTagsForContext(request.context, request.sortTags); + }); + } + + getSortTagsDb() { + return profileManager.getJsonDbInProfile("sort-tags"); + } + + loadSortTags() { + logger.debug("Attempting to load tags"); + + try { + const sortTagsData = this.getSortTagsDb().getData("/"); + + if (sortTagsData) { + this.sortTags = sortTagsData; + } + + logger.debug(`Loaded tags.`); + } catch (err) { + logger.warn(`There was an error reading tags file.`, err); + } + + this.getLegacyEventAndCommandTags(); + } + + private getLegacyEventAndCommandTags() { + if (!SettingsManager.getSetting("LegacySortTagsImported")) { + SettingsManager.saveSetting("LegacySortTagsImported", true); + + const legacySortTags: SortTagCache = { + commands: [], + events: [] + }; + + try { + const commandsDb = profileManager.getJsonDbInProfile("/chat/commands"); + + legacySortTags.commands = commandsDb.getData("/sortTags"); + + commandsDb.delete("/sortTags"); + } catch { } + + try { + const eventsDb = profileManager.getJsonDbInProfile("/events/events"); + + legacySortTags.events = eventsDb.getData("/sortTags"); + + eventsDb.delete("/sortTags"); + } catch { } + + Object.keys(legacySortTags).forEach((context) => { + if (this.sortTags[context] == null) { + this.sortTags[context] = []; + } + + const tags = legacySortTags[context] + .filter(t => this.sortTags[context].every(st => st.id !== t.id)); + + if (tags.length > 0) { + this.sortTags[context] = [ + ...this.sortTags[context], + ...tags + ]; + } + }); + + this.saveAllSortTags(); + } + } + + saveSortTagsForContext(context: string, sortTags: SortTag[]) { + this.sortTags[context] = sortTags; + this.saveAllSortTags(); + } + + private saveAllSortTags() { + try { + this.getSortTagsDb().push("/", this.sortTags); + + frontendCommunicator.send("sort-tags:updated-sort-tags", this.sortTags); + logger.debug("Saved tags"); + } catch (error) { + logger.warn("There was an error saving tags", error); + } + } +} + +const sortTagManager = new SortTagManager(); + +export { sortTagManager as SortTagManager }; \ No newline at end of file diff --git a/src/gui/app/services/sort-tags.service.js b/src/gui/app/services/sort-tags.service.js index 1f1c9f664..4560da301 100644 --- a/src/gui/app/services/sort-tags.service.js +++ b/src/gui/app/services/sort-tags.service.js @@ -10,51 +10,21 @@ angular .module("firebotApp") - .factory("sortTagsService", function(logger, profileManager, - utilityService, settingsService, backendCommunicator) { + .factory("sortTagsService", function(utilityService, backendCommunicator) { const service = {}; /** - * @type {Object.} + * @type {Record} */ let sortTags = {}; /** - * @type {Record} + * @type {Record} */ const selectedSortTags = {}; - function getSortTagsDb() { - return profileManager - .getJsonDbInProfile("sort-tags"); - } - - function saveAllSortTags() { - try { - getSortTagsDb().push("/", sortTags); - - logger.debug(`Saved tags.`); - } catch (err) { - logger.warn(`There was an error saving tags.`, err); - } - } - service.loadSortTags = () => { - logger.debug(`Attempting to load tags...`); - - try { - const sortTagsData = getSortTagsDb().getData("/"); - - if (sortTagsData) { - sortTags = sortTagsData; - } - - logger.debug(`Loaded tags.`); - } catch (err) { - logger.warn(`There was an error reading tags file.`, err); - } - - service.getLegacyEventAndCommandTags(); + sortTags = backendCommunicator.fireEventSync("sort-tags:get-sort-tags"); }; /** @@ -97,19 +67,23 @@ tags: () => sortTagsForContext }, closeCallback: (tags) => { - sortTags[context] = tags; - saveAllSortTags(); + backendCommunicator.send("sort-tags:save-sort-tags", { + context, + sortTags: tags + }); } }); - saveAllSortTags(); }; - /** @param {SortTag} context */ + /** @param {string} context */ service.getSelectedSortTag = context => selectedSortTags[context]; /** @param {string} context */ - // eslint-disable-next-line no-confusing-arrow - service.getSelectedSortTagDisplay = context => (selectedSortTags[context] != null ? selectedSortTags[context].name : `All ${context}`); + service.getSelectedSortTagDisplay = context => ( + selectedSortTags[context] != null + ? selectedSortTags[context].name + : `All ${context}` + ); /** * @param {string} context @@ -119,62 +93,9 @@ selectedSortTags[context] = tag; }; - /** - * This asks the backend to give us any old event and/or command tags that need to be - * imported into the new model - */ - service.getLegacyEventAndCommandTags = () => { - - if (!settingsService.getSetting("LegacySortTagsImported")) { - - settingsService.saveSetting("LegacySortTagsImported", true); - - /**@type {Object.} */ - const legacySortTags = { - commands: [], - events: [] - }; - - try { - const commandsDb = profileManager.getJsonDbInProfile("/chat/commands"); - - legacySortTags.commands = commandsDb.getData("/sortTags"); - - commandsDb.delete("/sortTags"); - } catch (err) { - // silently fail - } - - try { - const eventsDb = profileManager.getJsonDbInProfile("/events/events"); - - legacySortTags.events = eventsDb.getData("/sortTags"); - - eventsDb.delete("/sortTags"); - } catch (err) { - // silently fail - } - - Object.keys(legacySortTags).forEach((context) => { - - if (sortTags[context] == null) { - sortTags[context] = []; - } - - const tags = legacySortTags[context] - .filter(t => sortTags[context].every(st => st.id !== t.id)); - - if (tags.length > 0) { - sortTags[context] = [ - ...sortTags[context], - ...tags - ]; - } - }); - - saveAllSortTags(); - } - }; + backendCommunicator.on("sort-tags:updated-sort-tags", (updatedSortTags) => { + sortTags = updatedSortTags; + }); return service; });