From a6c19fec155628e281788d8b1067200c19cbcdf7 Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Fri, 3 Jan 2025 20:03:56 +0700 Subject: [PATCH] Use Set() for local co-op list --- src/utils/bx-event-bus.ts | 4 +--- src/utils/gh-pages.ts | 20 +++++++++++++++----- src/utils/local-co-op-manager.ts | 10 +++++----- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/utils/bx-event-bus.ts b/src/utils/bx-event-bus.ts index 6a60f43..18b60f0 100644 --- a/src/utils/bx-event-bus.ts +++ b/src/utils/bx-event-bus.ts @@ -33,9 +33,7 @@ type ScriptEvents = { }; 'list.localCoOp.updated': { - data: { - data: any; - }; + ids: Set, }; }; diff --git a/src/utils/gh-pages.ts b/src/utils/gh-pages.ts index a34fe8b..bf22019 100755 --- a/src/utils/gh-pages.ts +++ b/src/utils/gh-pages.ts @@ -56,6 +56,8 @@ export class GhPagesUtils { BxEventBus.Script.emit('list.forcedNativeMkb.updated', { data: json, }); + } else { + window.localStorage.removeItem(key); } }); @@ -70,6 +72,7 @@ export class GhPagesUtils { } static getTouchControlCustomList() { + // TODO: use Set() const key = StorageKey.LIST_CUSTOM_TOUCH_LAYOUTS; NATIVE_FETCH(GhPagesUtils.getUrl('touch-layouts/ids.json')) @@ -84,7 +87,7 @@ export class GhPagesUtils { return customList; } - static getLocalCoOpList() { + static getLocalCoOpList(): Set { const supportedSchema = 1; const key = StorageKey.LIST_LOCAL_CO_OP; @@ -93,14 +96,21 @@ export class GhPagesUtils { .then(json => { if (json.$schemaVersion === supportedSchema) { window.localStorage.setItem(key, JSON.stringify(json)); - BxEventBus.Script.emit('list.localCoOp.updated', { data: json }); + const ids = new Set(Object.keys(json.data)); + BxEventBus.Script.emit('list.localCoOp.updated', { ids }); } else { window.localStorage.removeItem(key); - BxEventBus.Script.emit('list.localCoOp.updated', { data: { data: {} } }); + BxEventBus.Script.emit('list.localCoOp.updated', { ids: new Set() }); } }); - const customList = JSON.parse(window.localStorage.getItem(key) || '[]'); - return customList; + const info = JSON.parse(window.localStorage.getItem(key) || '{}'); + if (info.$schemaVersion !== supportedSchema) { + // Delete storage; + window.localStorage.removeItem(key); + return new Set(); + } + + return new Set(Object.keys(info.data || {})); } } diff --git a/src/utils/local-co-op-manager.ts b/src/utils/local-co-op-manager.ts index 3749f89..cae8926 100644 --- a/src/utils/local-co-op-manager.ts +++ b/src/utils/local-co-op-manager.ts @@ -5,17 +5,17 @@ export class LocalCoOpManager { private static instance: LocalCoOpManager; public static getInstance = () => LocalCoOpManager.instance ?? (LocalCoOpManager.instance = new LocalCoOpManager()); - private supportedIds: string[] = []; + private supportedIds: Set; constructor() { BxEventBus.Script.once('list.localCoOp.updated', e => { - this.supportedIds = Object.keys(e.data.data); - console.log('supportedIds', this.supportedIds); + this.supportedIds = e.ids; }); - GhPagesUtils.getLocalCoOpList(); + this.supportedIds = GhPagesUtils.getLocalCoOpList(); + console.log('this.supportedIds', this.supportedIds); } isSupported(productId: string) { - return this.supportedIds.includes(productId); + return this.supportedIds.has(productId); } }