Skip to content

Commit

Permalink
fix: use new app window manager that stores app window state to storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranroneill committed Dec 5, 2023
1 parent 174e119 commit 03fc780
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/extension/constants/Keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export const SETTINGS_ADVANCED_KEY: string = 'settings_advanced';
export const SETTINGS_APPEARANCE_KEY: string = 'settings_appearance';
export const SETTINGS_GENERAL_KEY: string = 'settings_general';
export const SESSION_ITEM_KEY_PREFIX: string = 'session_';
export const APP_WINDOW_KEY_PREFIX: string = 'app_window_';
7 changes: 7 additions & 0 deletions src/extension/enums/AppTypeEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum AppTypeEnum {
BackgroundApp = 'background_app',
MainApp = 'main_app',
RegistrationApp = 'registration_app',
}

export default AppTypeEnum;
1 change: 1 addition & 0 deletions src/extension/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as AccountsThunkEnum } from './AccountsThunkEnum';
export { default as AppTypeEnum } from './AppTypeEnum';
export { default as AssetsThunkEnum } from './AssetsThunkEnum';
export { default as ErrorCodeEnum } from './ErrorCodeEnum';
export { default as MessagesThunkEnum } from './MessagesThunkEnum';
Expand Down
6 changes: 3 additions & 3 deletions src/extension/services/AccountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class AccountService {
}

/**
* Public static functions
* public static functions
*/

/**
Expand Down Expand Up @@ -152,7 +152,7 @@ export default class AccountService {
}

/**
* Private functions
* private functions
*/

/**
Expand All @@ -165,7 +165,7 @@ export default class AccountService {
}

/**
* Public functions
* public functions
*/

/**
Expand Down
101 changes: 101 additions & 0 deletions src/extension/services/AppWindowManagerService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Windows } from 'webextension-polyfill';

// constants
import { APP_WINDOW_KEY_PREFIX } from '@extension/constants';

// enums
import { AppTypeEnum } from '@extension/enums';

// services
import StorageManager from './StorageManager';

// types
import { IBaseOptions, ILogger } from '@common/types';
import { IAppWindow } from '@extension/types';

/**
* Manages app windows in storage.
* @class
*/
export default class AppWindowManagerService {
// private variables
private readonly logger: ILogger | null;
private readonly storageManager: StorageManager;

constructor({ logger }: IBaseOptions) {
this.logger = logger || null;
this.storageManager = new StorageManager();
}

/**
* private functions
*/

/**
* Convenience function that simply creates the app window item key from the window ID.
* @param {number} id - the window ID.
* @returns {string} the app window item key.
*/
private createAppWindowItemKey(id: number): string {
return `${APP_WINDOW_KEY_PREFIX}${id}`;
}

/**
* public functions
*/

public async getAll(): Promise<IAppWindow[]> {
const items: Record<string, unknown> =
await this.storageManager.getAllItems();

return Object.keys(items).reduce<IAppWindow[]>(
(acc, key) =>
key.startsWith(APP_WINDOW_KEY_PREFIX)
? [...acc, items[key] as IAppWindow]
: acc,
[]
);
}

public async getById(id: number): Promise<IAppWindow | null> {
return await this.storageManager.getItem<IAppWindow>(
this.createAppWindowItemKey(id)
);
}

public async getByType(type: AppTypeEnum): Promise<IAppWindow[]> {
const appWindows: IAppWindow[] = await this.getAll();

return appWindows.filter((value) => value.type === type);
}

public async removeById(id: number): Promise<void> {
return await this.storageManager.remove(this.createAppWindowItemKey(id));
}

public async removeByType(type: AppTypeEnum): Promise<void> {
const appWindows: IAppWindow[] = await this.getByType(type);

return await this.storageManager.remove(
appWindows.map((value) => this.createAppWindowItemKey(value.windowId))
);
}

public async saveByBrowserWindowAndType(
window: Windows.Window,
type: AppTypeEnum
): Promise<void> {
if (!window.id) {
return;
}

return await this.storageManager.setItems({
[this.createAppWindowItemKey(window.id)]: {
left: window.left || 0,
top: window.top || 0,
type,
windowId: window.id,
},
});
}
}
Loading

0 comments on commit 03fc780

Please sign in to comment.