feat(workspace): Move observer notifications to workspaces storage

This commit is contained in:
Kristijan Ribarić 2024-10-05 21:16:07 +02:00
parent 863dcfa705
commit 86578cb4d3
3 changed files with 14 additions and 11 deletions

View file

@ -209,7 +209,6 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
this._workspaceCache = null;
await this._propagateWorkspaceData();
await this._updateWorkspacesChangeContextMenu();
Services.obs.notifyObservers(null, "zen-workspace-removed", windowID);
}
isWorkspaceActive(workspace) {
@ -559,7 +558,6 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
workspaceData.name = workspaceName;
workspaceData.icon = icon?.label;
await this.saveWorkspace(workspaceData);
Services.obs.notifyObservers(null, "zen-workspace-updated", workspaceData.uuid);
await this._propagateWorkspaceData();
}
@ -683,7 +681,6 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
}
let workspaceData = this._createWorkspaceData(name, isDefault, icon);
await this.saveWorkspace(workspaceData);
Services.obs.notifyObservers(null, "zen-workspace-added", workspaceData.uuid);
await this.changeWorkspace(workspaceData);
}
@ -760,7 +757,6 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
let userContextId = parseInt(event.target.getAttribute('data-usercontextid'));
workspace.containerTabId = userContextId;
await this.saveWorkspace(workspace);
Services.obs.notifyObservers(null, "zen-workspace-updated", workspace.uuid);
await this._propagateWorkspaceData();
}

View file

@ -17,7 +17,7 @@ var ZenWorkspacesStorage = {
container_id INTEGER,
position INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
)
`);
@ -47,7 +47,7 @@ var ZenWorkspacesStorage = {
}
},
async saveWorkspace(workspace) {
async saveWorkspace(workspace, notifyObservers = true) {
await PlacesUtils.withConnectionWrapper('ZenWorkspacesStorage.saveWorkspace', async (db) => {
const now = Date.now();
@ -114,6 +114,10 @@ var ZenWorkspacesStorage = {
});
});
});
if(notifyObservers) {
Services.obs.notifyObservers(null, "zen-workspace-updated", workspace.uuid);
}
},
async getWorkspaces() {
@ -131,7 +135,7 @@ var ZenWorkspacesStorage = {
}));
},
async removeWorkspace(uuid) {
async removeWorkspace(uuid, notifyObservers = true) {
await PlacesUtils.withConnectionWrapper('ZenWorkspacesStorage.removeWorkspace', async (db) => {
await db.execute(
`
@ -150,6 +154,10 @@ var ZenWorkspacesStorage = {
timestamp: Math.floor(now / 1000)
});
});
if(notifyObservers) {
Services.obs.notifyObservers(null, "zen-workspace-removed", uuid);
}
},
async wipeAllWorkspaces() {

View file

@ -68,7 +68,7 @@ ZenWorkspacesStore.prototype.changeItemID = async function (oldID, newID) {
const workspace = workspaces.find(ws => ws.uuid === oldID);
if (workspace) {
workspace.uuid = newID;
await ZenWorkspacesStorage.saveWorkspace(workspace);
await ZenWorkspacesStorage.saveWorkspace(workspace,false);
// Mark the new ID as changed for sync
await ZenWorkspacesStorage.markChanged(newID);
}
@ -138,7 +138,7 @@ ZenWorkspacesStore.prototype.create = async function (record) {
containerTabId: record.containerTabId,
position: record.position
};
await ZenWorkspacesStorage.saveWorkspace(workspace);
await ZenWorkspacesStorage.saveWorkspace(workspace,false);
} catch (error) {
this._log.error(`Error creating workspace with ID ${record.id}`, error);
throw error;
@ -165,8 +165,7 @@ ZenWorkspacesStore.prototype.update = async function (record) {
*/
ZenWorkspacesStore.prototype.remove = async function (record) {
try {
await ZenWorkspacesStorage.removeWorkspace(record.id);
// Changes are already marked within ZenWorkspacesStorage.removeWorkspace
await ZenWorkspacesStorage.removeWorkspace(record.id, false);
} catch (error) {
this._log.error(`Error removing workspace with ID ${record.id}`, error);
throw error;