Skip to content

Commit

Permalink
fix PersistentMap util
Browse files Browse the repository at this point in the history
  • Loading branch information
zhelvis committed Oct 17, 2023
1 parent 9a35ea4 commit 0809fce
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
20 changes: 10 additions & 10 deletions packages/tswebextension/src/lib/common/utils/persistent-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { isBackgroundPersistent } from './is-background-persistent';
* {@link PersistentMap.set} and {@link PersistentMap.delete} save the data with debounce.
*/
export class PersistentMap<K, V> extends Map<K, V> {
// TODO: delete after migration to MV3
// TODO: delete after migration to event-driven background
private static isBackgroundPersistent = isBackgroundPersistent();

private isStorageInit = false;

/**
* Saves the in-memory data to the {@link storage}.
*/
Expand Down Expand Up @@ -45,14 +47,15 @@ export class PersistentMap<K, V> extends Map<K, V> {
* @returns Promise that resolves when the data is loaded.
*/
public async init(): Promise<void> {
// TODO: delete condition after migration to MV3
// TODO: delete after migration to event-driven background
if (PersistentMap.isBackgroundPersistent) {
return;
}

const data = (await this.storage.get(this.key))?.[this.key];

if (typeof data === 'undefined') {
this.isStorageInit = true;
return;
}

Expand All @@ -63,15 +66,15 @@ export class PersistentMap<K, V> extends Map<K, V> {
data.forEach(([key, value]) => {
super.set(key, value);
});

this.isStorageInit = true;
}

/** @inheritdoc */
override set(key: K, value: V): this {
super.set(key, value);

// TODO: delete condition after migration to MV3
if (!PersistentMap.isBackgroundPersistent) {
// FIXME: missing this in derived class.
if (this.isStorageInit) {
this.save();
}

Expand All @@ -82,8 +85,7 @@ export class PersistentMap<K, V> extends Map<K, V> {
override delete(key: K): boolean {
const isDeleted = super.delete(key);

// TODO: delete condition after migration to MV3
if (!PersistentMap.isBackgroundPersistent) {
if (this.isStorageInit) {
this.save();
}

Expand All @@ -94,7 +96,7 @@ export class PersistentMap<K, V> extends Map<K, V> {
override clear(): void {
super.clear();

if (!PersistentMap.isBackgroundPersistent) {
if (this.isStorageInit) {
this.storage.remove(this.key);
}
}
Expand All @@ -120,5 +122,3 @@ export class PersistentMap<K, V> extends Map<K, V> {
return Array.isArray(data) && data.length === 2;
}
}

Object.setPrototypeOf(PersistentMap.prototype, Map.prototype);
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class TabContext {

/**
* Blocked request count.
* TODO: use persistent value here.
*/
public blockedRequestCount = 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import browser, { type ExtensionTypes, type Tabs } from 'webextension-polyfill';
import type { CosmeticResult, MatchingResult, NetworkRule } from '@adguard/tsurlfilter';

import { PersistentMap } from '../../../common/utils/persistent-map';
import { EventChannel } from '../../../common/utils/channels';
import type { DocumentApi } from '../document-api';
import { FrameRequestContext, TabContext } from './tab-context';
Expand All @@ -18,7 +17,7 @@ export type TabFrameRequestContext = FrameRequestContext & {
* Tabs API. Wrapper around browser.tabs API.
*/
export class TabsApi {
public context = new PersistentMap<number, TabContext>(browser.storage.session, 'tabs');
public context = new Map<number, TabContext>();

public onCreate = new EventChannel<TabContext>();

Expand Down Expand Up @@ -59,8 +58,6 @@ export class TabsApi {
* Initializes tabs API and starts listening for tab & window events.
*/
public async start(): Promise<void> {
await this.context.init();

browser.tabs.onCreated.addListener(this.handleTabCreate);
browser.tabs.onRemoved.addListener(this.handleTabDelete);
browser.tabs.onUpdated.addListener(this.handleTabUpdate);
Expand Down

0 comments on commit 0809fce

Please sign in to comment.