Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDB-9964: Clear results of closed tabs from memory #264

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 60 additions & 45 deletions Yasgui/packages/yasgui/src/PersistentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,88 +20,103 @@ function getDefaults(): PersistedJson {
}

export default class PersistentConfig {
private persistedJson!: PersistedJson;
private storageId: string | undefined;
private yasgui: Yasgui;
private storage: YStorage;

private activeTabId: string | undefined;
private endpointHistory: string[];
private lastClosedTab: boolean;

constructor(yasgui: Yasgui) {
this.yasgui = yasgui;
this.storageId = this.yasgui.getStorageId(this.yasgui.config.persistenceLabelConfig);
this.storage = new YStorage(storageNamespace);
this.fromStorage();
const persistence = this.getPersistedJson();
this.endpointHistory = persistence.endpointHistory || [];
this.activeTabId = persistence.active;
this.lastClosedTab = !!persistence.lastClosedTab;
// this.fromStorage();
this.registerListeners();
}

private getPersistedJson(): PersistedJson {
return this.storage.get<PersistedJson>(this.storageId) || getDefaults();
}

public setActive(id: string) {
this.persistedJson.active = id;
this.toStorage();
const persistedJson = this.getPersistedJson();
persistedJson.active = id;
this.activeTabId = id;
this.toStorage(persistedJson);
}
public getActiveId(): string | undefined {
return this.persistedJson.active;
return this.activeTabId;
}
public addToTabList(tabId: string, index?: number) {
if (index !== undefined && this.persistedJson.tabs.length > index) {
this.persistedJson.tabs.splice(index, 0, tabId);
const persistedJson = this.getPersistedJson();
if (index !== undefined && persistedJson.tabs.length > index) {
persistedJson.tabs.splice(index, 0, tabId);
} else {
this.persistedJson.tabs.push(tabId);
persistedJson.tabs.push(tabId);
}

this.toStorage();
this.toStorage(persistedJson);
}
public setTabOrder(tabs: string[]) {
this.persistedJson.tabs = tabs;
this.toStorage();
const persistedJson = this.getPersistedJson();
persistedJson.tabs = tabs;
this.toStorage(persistedJson);
}
public getEndpointHistory() {
return this.persistedJson.endpointHistory;
return this.endpointHistory || [];
}
public retrieveLastClosedTab() {
const tabCopy = this.persistedJson.lastClosedTab;
this.lastClosedTab = false;
const persistedJson = this.getPersistedJson();
const tabCopy = persistedJson.lastClosedTab;
if (tabCopy === undefined) return tabCopy;
this.persistedJson.lastClosedTab = undefined;
persistedJson.lastClosedTab = undefined;
return tabCopy;
}
public hasLastClosedTab() {
return !!this.persistedJson.lastClosedTab;
return this.lastClosedTab;
}
public deleteTab(tabId: string) {
const i = this.persistedJson.tabs.indexOf(tabId);
const persistedJson = this.getPersistedJson();
const i = persistedJson.tabs.indexOf(tabId);
if (i > -1) {
this.persistedJson.tabs.splice(i, 1);
persistedJson.tabs.splice(i, 1);
}
if (this.tabIsActive(tabId)) {
this.persistedJson.active = undefined;
persistedJson.active = undefined;
this.activeTabId = persistedJson.active;
}
this.persistedJson.lastClosedTab = { index: i, tab: this.persistedJson.tabConfig[tabId] };
delete this.persistedJson.tabConfig[tabId];
this.toStorage();
persistedJson.lastClosedTab = { index: i, tab: persistedJson.tabConfig[tabId] };
this.lastClosedTab = true;
delete persistedJson.tabConfig[tabId];
this.toStorage(persistedJson);
}

private registerListeners() {
this.yasgui.on("tabChange", (_yasgui, tab) => {
this.persistedJson.tabConfig[tab.getId()] = tab.getPersistedJson();
this.toStorage();
const persistedJson = this.getPersistedJson();
persistedJson.tabConfig[tab.getId()] = tab.getPersistedJson();
this.toStorage(persistedJson);
});
this.yasgui.on("endpointHistoryChange", (_yasgui, history) => {
this.persistedJson.endpointHistory = history;
this.toStorage();
const persistedJson = this.getPersistedJson();
persistedJson.endpointHistory = history;
this.endpointHistory = persistedJson.endpointHistory;
this.toStorage(persistedJson);
});
}

public toStorage() {
public toStorage(persistedJson: PersistedJson) {
const onQuotaExceeded = this.yasgui.getHandleLocalStorageQuotaFull
? this.yasgui.getHandleLocalStorageQuotaFull()
: this.handleLocalStorageQuotaFull;
this.storage.set(this.storageId, this.persistedJson, this.yasgui.config.persistencyExpire, onQuotaExceeded);
}
private fromStorage(): PersistedJson {
this.persistedJson = this.storage.get<PersistedJson>(this.storageId) || getDefaults();
/**
* Modify some settings for backwards compatability
*/
if (!this.persistedJson.endpointHistory) {
this.persistedJson.endpointHistory = [];
}
return this.persistedJson;
this.storage.set(this.storageId, persistedJson, this.yasgui.config.persistencyExpire, onQuotaExceeded);
}

private handleLocalStorageQuotaFull(_e: any) {
Expand All @@ -110,10 +125,10 @@ export default class PersistentConfig {
}

public getTabs() {
return this.persistedJson.tabs;
return this.getPersistedJson().tabs;
}
public getTab(tabId: string) {
return this.persistedJson.tabConfig[tabId];
return this.getPersistedJson().tabConfig[tabId];
}

/**
Expand All @@ -122,19 +137,19 @@ export default class PersistentConfig {
* Then we'd like to forward that config to this object, so we can simply keep initializing from this persistence class
*/
public setTab(tabId: string, tabConfig: Tab.PersistedJson) {
this.persistedJson.tabs.push(tabId);
this.persistedJson.tabConfig[tabId] = tabConfig;
this.persistedJson.active = tabId;
// this.persistedJson.tabs.push(tabId);
// this.persistedJson.tabConfig[tabId] = tabConfig;
// this.persistedJson.active = tabId;
}
public tabIsActive(tabId: string) {
return tabId === this.persistedJson.active;
return tabId === this.activeTabId;
}
public currentId() {
return this.persistedJson.active;
return this.activeTabId;
}

public getTabConfig() {
return this.persistedJson.tabConfig;
return this.getPersistedJson().tabConfig;
}
public static clear() {
const storage = new YStorage(storageNamespace);
Expand Down
7 changes: 7 additions & 0 deletions Yasgui/packages/yasgui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ export class Yasgui extends EventEmitter {
//draw tab content
if (!this._tabs[tabId]) {
this._tabs[tabId] = new Tab(this, Tab.getDefaults(this));
} else {
// loads the response from storage.
let response = this.persistentConfig.getTab(tabId).yasr.response;
this._tabs[tabId].getYasr()?.setResponse(response, undefined, undefined, undefined, undefined, undefined,false);
}
this._tabs[tabId].show();
this.emit("yasqeReady", this._tabs[tabId], this._tabs[tabId].getYasqe());
Expand All @@ -291,6 +295,9 @@ export class Yasgui extends EventEmitter {
this.emit("tabSelect", this, tabId);
this.persistentConfig.setActive(tabId);
}
// Removes data from memory, it will be loaded if the tab is selected again.
// @ts-ignore
tab.getYasr()?.results?.response = null;
}
return tab;
}
Expand Down