瀏覽代碼

update workerElectronCacheStorageService to workerElectronService

Abhinav 1 年之前
父節點
當前提交
3364420b15

+ 73 - 0
packages/shared/electron/service.ts

@@ -0,0 +1,73 @@
+import * as Comlink from 'comlink';
+import { LimitedCache } from '@ente/shared/storage/cacheStorage/types';
+import {
+    ProxiedWorkerLimitedCache,
+    WorkerSafeElectronClient,
+} from './worker/client';
+import { wrap } from 'comlink';
+import { deserializeToResponse, serializeResponse } from './worker/utils/proxy';
+import { runningInWorker } from '@ente/shared/platform';
+
+export interface LimitedElectronAPIs {
+    openDiskCache: (cacheName: string) => Promise<LimitedCache>;
+    deleteDiskCache: (cacheName: string) => Promise<boolean>;
+    getSentryUserID: () => Promise<string>;
+}
+
+class WorkerSafeElectronServiceImpl implements LimitedElectronAPIs {
+    proxiedElectron:
+        | Comlink.Remote<WorkerSafeElectronClient>
+        | WorkerSafeElectronClient;
+    ready: Promise<any>;
+
+    constructor() {
+        this.ready = this.init();
+    }
+    private async init() {
+        if (runningInWorker()) {
+            const workerSafeElectronClient =
+                wrap<typeof WorkerSafeElectronClient>(self);
+
+            this.proxiedElectron = await new workerSafeElectronClient();
+        } else {
+            this.proxiedElectron = new WorkerSafeElectronClient();
+        }
+    }
+    async openDiskCache(cacheName: string) {
+        await this.ready;
+        const cache = await this.proxiedElectron.openDiskCache(cacheName);
+        return {
+            match: transformMatch(cache.match.bind(cache)),
+            put: transformPut(cache.put.bind(cache)),
+            delete: cache.delete.bind(cache),
+        };
+    }
+
+    async deleteDiskCache(cacheName: string) {
+        await this.ready;
+        return await this.proxiedElectron.deleteDiskCache(cacheName);
+    }
+
+    async getSentryUserID() {
+        await this.ready;
+        return this.proxiedElectron.getSentryUserID();
+    }
+}
+
+export const WorkerSafeElectronService = new WorkerSafeElectronServiceImpl();
+
+function transformMatch(
+    fn: ProxiedWorkerLimitedCache['match']
+): LimitedCache['match'] {
+    return async (key: string) => {
+        return deserializeToResponse(await fn(key));
+    };
+}
+
+function transformPut(
+    fn: ProxiedWorkerLimitedCache['put']
+): LimitedCache['put'] {
+    return async (key: string, data: Response) => {
+        fn(key, await serializeResponse(data));
+    };
+}

+ 19 - 10
packages/shared/storage/cacheStorage/workerElectron/client.ts → packages/shared/electron/worker/client.ts

@@ -1,16 +1,21 @@
 import * as Comlink from 'comlink';
-import {
-    LimitedCache,
-    ProxiedLimitedCacheStorage,
-    ProxiedWorkerLimitedCache,
-} from '@ente/shared/storage/cacheStorage/types';
+import { LimitedCache } from '@ente/shared/storage/cacheStorage/types';
 import { serializeResponse, deserializeToResponse } from './utils/proxy';
 import ElectronAPIs from '@ente/shared/electron';
 
-export class WorkerElectronCacheStorageClient
-    implements ProxiedLimitedCacheStorage
-{
-    async open(cacheName: string) {
+export interface ProxiedLimitedElectronAPIs {
+    openDiskCache: (cacheName: string) => Promise<ProxiedWorkerLimitedCache>;
+    deleteDiskCache: (cacheName: string) => Promise<boolean>;
+    getSentryUserID: () => Promise<string>;
+}
+export interface ProxiedWorkerLimitedCache {
+    match: (key: string) => Promise<ArrayBuffer>;
+    put: (key: string, data: ArrayBuffer) => Promise<void>;
+    delete: (key: string) => Promise<boolean>;
+}
+
+export class WorkerSafeElectronClient implements ProxiedLimitedElectronAPIs {
+    async openDiskCache(cacheName: string) {
         const cache = await ElectronAPIs.openDiskCache(cacheName);
         return Comlink.proxy({
             match: Comlink.proxy(transformMatch(cache.match.bind(cache))),
@@ -19,9 +24,13 @@ export class WorkerElectronCacheStorageClient
         });
     }
 
-    async delete(cacheName: string) {
+    async deleteDiskCache(cacheName: string) {
         return await ElectronAPIs.deleteDiskCache(cacheName);
     }
+
+    async getSentryUserID() {
+        return await ElectronAPIs.getSentryUserID();
+    }
 }
 
 function transformMatch(

+ 0 - 0
packages/shared/storage/cacheStorage/workerElectron/utils/proxy.ts → packages/shared/electron/worker/utils/proxy.ts


+ 0 - 0
packages/shared/storage/cacheStorage/workerElectron/utils/transferHandler.ts → packages/shared/electron/worker/utils/transferHandler.ts


+ 2 - 2
packages/shared/sentry/utils.ts

@@ -1,4 +1,4 @@
-import ElectronAPIs from '@ente/shared/electron';
+import { WorkerSafeElectronService } from '@ente/shared/electron/service';
 import {
     getLocalSentryUserID,
     setLocalSentryUserID,
@@ -12,7 +12,7 @@ import { HttpStatusCode } from 'axios';
 
 export async function getSentryUserID() {
     if (isElectron()) {
-        return await ElectronAPIs.getSentryUserID();
+        return await WorkerSafeElectronService.getSentryUserID();
     } else {
         let anonymizeUserID = getLocalSentryUserID();
         if (!anonymizeUserID) {

+ 10 - 21
packages/shared/storage/cacheStorage/factory.ts

@@ -1,28 +1,17 @@
 import { LimitedCacheStorage } from './types';
-import { runningInElectron, runningInWorker } from '@ente/shared/platform';
-import { WorkerElectronCacheStorageService } from './workerElectron/service';
-import ElectronAPIs from '@ente/shared/electron';
-
+import { runningInElectron } from '@ente/shared/platform';
+import { WorkerSafeElectronService } from '@ente/shared/electron/service';
 class cacheStorageFactory {
-    workerElectronCacheStorageServiceInstance: WorkerElectronCacheStorageService;
     getCacheStorage(): LimitedCacheStorage {
         if (runningInElectron()) {
-            if (runningInWorker()) {
-                if (!this.workerElectronCacheStorageServiceInstance) {
-                    this.workerElectronCacheStorageServiceInstance =
-                        new WorkerElectronCacheStorageService();
-                }
-                return this.workerElectronCacheStorageServiceInstance;
-            } else {
-                return {
-                    open(cacheName) {
-                        return ElectronAPIs.openDiskCache(cacheName);
-                    },
-                    delete(cacheName) {
-                        return ElectronAPIs.deleteDiskCache(cacheName);
-                    },
-                };
-            }
+            return {
+                open(cacheName) {
+                    return WorkerSafeElectronService.openDiskCache(cacheName);
+                },
+                delete(cacheName) {
+                    return WorkerSafeElectronService.deleteDiskCache(cacheName);
+                },
+            };
         } else {
             return transformBrowserCacheStorageToLimitedCacheStorage(caches);
         }

+ 0 - 10
packages/shared/storage/cacheStorage/types.ts

@@ -8,13 +8,3 @@ export interface LimitedCache {
     put: (key: string, data: Response) => Promise<void>;
     delete: (key: string) => Promise<boolean>;
 }
-
-export interface ProxiedLimitedCacheStorage {
-    open: (cacheName: string) => Promise<ProxiedWorkerLimitedCache>;
-    delete: (cacheName: string) => Promise<boolean>;
-}
-export interface ProxiedWorkerLimitedCache {
-    match: (key: string) => Promise<ArrayBuffer>;
-    put: (key: string, data: ArrayBuffer) => Promise<void>;
-    delete: (key: string) => Promise<boolean>;
-}

+ 0 - 55
packages/shared/storage/cacheStorage/workerElectron/service.ts

@@ -1,55 +0,0 @@
-import * as Comlink from 'comlink';
-import {
-    LimitedCache,
-    LimitedCacheStorage,
-    ProxiedWorkerLimitedCache,
-} from '../types';
-import { WorkerElectronCacheStorageClient } from './client';
-import { wrap } from 'comlink';
-import { deserializeToResponse, serializeResponse } from './utils/proxy';
-
-export class WorkerElectronCacheStorageService implements LimitedCacheStorage {
-    proxiedElectronCacheService: Comlink.Remote<WorkerElectronCacheStorageClient>;
-    ready: Promise<any>;
-
-    constructor() {
-        this.ready = this.init();
-    }
-    async init() {
-        const electronCacheStorageProxy =
-            wrap<typeof WorkerElectronCacheStorageClient>(self);
-
-        this.proxiedElectronCacheService =
-            await new electronCacheStorageProxy();
-    }
-    async open(cacheName: string) {
-        await this.ready;
-        const cache = await this.proxiedElectronCacheService.open(cacheName);
-        return {
-            match: transformMatch(cache.match.bind(cache)),
-            put: transformPut(cache.put.bind(cache)),
-            delete: cache.delete.bind(cache),
-        };
-    }
-
-    async delete(cacheName: string) {
-        await this.ready;
-        return await this.proxiedElectronCacheService.delete(cacheName);
-    }
-}
-
-function transformMatch(
-    fn: ProxiedWorkerLimitedCache['match']
-): LimitedCache['match'] {
-    return async (key: string) => {
-        return deserializeToResponse(await fn(key));
-    };
-}
-
-function transformPut(
-    fn: ProxiedWorkerLimitedCache['put']
-): LimitedCache['put'] {
-    return async (key: string, data: Response) => {
-        fn(key, await serializeResponse(data));
-    };
-}

+ 2 - 2
packages/shared/worker/comlinkWorker.ts

@@ -1,5 +1,5 @@
 import { expose, Remote, wrap } from 'comlink';
-import { WorkerElectronCacheStorageClient } from '@ente/shared/storage/cacheStorage/workerElectron/client';
+import { WorkerSafeElectronClient } from '@ente/shared/electron/worker/client';
 import { addLocalLog } from '@ente/shared/logging';
 
 export class ComlinkWorker<T extends new () => InstanceType<T>> {
@@ -17,7 +17,7 @@ export class ComlinkWorker<T extends new () => InstanceType<T>> {
         addLocalLog(() => `Initiated ${this.name}`);
         const comlink = wrap<T>(this.worker);
         this.remote = new comlink() as Promise<Remote<InstanceType<T>>>;
-        expose(WorkerElectronCacheStorageClient, this.worker);
+        expose(WorkerSafeElectronClient, this.worker);
     }
 
     public getName() {