-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use new app window manager that stores app window state to storage
- Loading branch information
1 parent
174e119
commit 03fc780
Showing
10 changed files
with
207 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
} | ||
} |
Oops, something went wrong.