aboutsummaryrefslogtreecommitdiff
path: root/src/background/repositories/ReadyFrameRepository.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2021-09-26 11:54:11 +0900
committerGitHub <noreply@github.com>2021-09-26 11:54:11 +0900
commit9154972485c24a5a90782cef17f75b3a79a13774 (patch)
tree4bc3d608f498176f50a74ca7fc03d2d501ac7920 /src/background/repositories/ReadyFrameRepository.ts
parentcbf4b37bd0d5ba277d6400ed460d6a086ae1d7bb (diff)
parent91d4712e676782bb58fd7eb03fdc1f85111fca04 (diff)
Merge pull request #1264 from ueokande/fix-establish-connection-issue
Search a content from frames successfully loaded
Diffstat (limited to 'src/background/repositories/ReadyFrameRepository.ts')
-rw-r--r--src/background/repositories/ReadyFrameRepository.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/background/repositories/ReadyFrameRepository.ts b/src/background/repositories/ReadyFrameRepository.ts
new file mode 100644
index 0000000..c993858
--- /dev/null
+++ b/src/background/repositories/ReadyFrameRepository.ts
@@ -0,0 +1,70 @@
+import MemoryStorage from "../infrastructures/MemoryStorage";
+
+const REPOSITORY_KEY = "readyFrameRepository";
+
+type State = { [tabId: number]: { [frameId: number]: number } };
+
+export default interface ReadyFrameRepository {
+ addFrameId(tabId: number, frameId: number): Promise<void>;
+
+ removeFrameId(tabId: number, frameId: number): Promise<void>;
+
+ getFrameIds(tabId: number): Promise<number[] | undefined>;
+}
+
+export class ReadyFrameRepositoryImpl implements ReadyFrameRepository {
+ private cache: MemoryStorage;
+
+ constructor() {
+ this.cache = new MemoryStorage();
+ }
+
+ addFrameId(tabId: number, frameId: number): Promise<void> {
+ let state: State | undefined = this.cache.get(REPOSITORY_KEY);
+ if (typeof state === "undefined") {
+ state = {};
+ }
+ const tab = state[tabId] || {};
+ tab[frameId] = (tab[frameId] || 0) + 1;
+ state[tabId] = tab;
+ this.cache.set(REPOSITORY_KEY, state);
+ return Promise.resolve();
+ }
+
+ removeFrameId(tabId: number, frameId: number): Promise<void> {
+ const state: State | undefined = this.cache.get(REPOSITORY_KEY);
+ if (typeof state === "undefined") {
+ return Promise.resolve();
+ }
+ const ids = state[tabId];
+ if (typeof ids === "undefined") {
+ return Promise.resolve();
+ }
+ const tab = state[tabId] || {};
+ tab[frameId] = (tab[frameId] || 0) - 1;
+ if (tab[frameId] == 0) {
+ delete tab[frameId];
+ }
+ if (Object.keys(tab).length === 0) {
+ delete state[tabId];
+ }
+
+ this.cache.set(REPOSITORY_KEY, state);
+ return Promise.resolve();
+ }
+
+ getFrameIds(tabId: number): Promise<number[] | undefined> {
+ const state: State | undefined = this.cache.get(REPOSITORY_KEY);
+ if (typeof state === "undefined") {
+ return Promise.resolve(undefined);
+ }
+ const tab = state[tabId];
+ if (typeof tab === "undefined") {
+ return Promise.resolve(undefined);
+ }
+ const frameIds = Object.keys(tab)
+ .map((v) => Number(v))
+ .sort();
+ return Promise.resolve(frameIds);
+ }
+}