Use Set() for local co-op list

This commit is contained in:
redphx 2025-01-03 20:03:56 +07:00
parent 6448a00271
commit a6c19fec15
3 changed files with 21 additions and 13 deletions

View File

@ -33,9 +33,7 @@ type ScriptEvents = {
}; };
'list.localCoOp.updated': { 'list.localCoOp.updated': {
data: { ids: Set<string>,
data: any;
};
}; };
}; };

View File

@ -56,6 +56,8 @@ export class GhPagesUtils {
BxEventBus.Script.emit('list.forcedNativeMkb.updated', { BxEventBus.Script.emit('list.forcedNativeMkb.updated', {
data: json, data: json,
}); });
} else {
window.localStorage.removeItem(key);
} }
}); });
@ -70,6 +72,7 @@ export class GhPagesUtils {
} }
static getTouchControlCustomList() { static getTouchControlCustomList() {
// TODO: use Set()
const key = StorageKey.LIST_CUSTOM_TOUCH_LAYOUTS; const key = StorageKey.LIST_CUSTOM_TOUCH_LAYOUTS;
NATIVE_FETCH(GhPagesUtils.getUrl('touch-layouts/ids.json')) NATIVE_FETCH(GhPagesUtils.getUrl('touch-layouts/ids.json'))
@ -84,7 +87,7 @@ export class GhPagesUtils {
return customList; return customList;
} }
static getLocalCoOpList() { static getLocalCoOpList(): Set<string> {
const supportedSchema = 1; const supportedSchema = 1;
const key = StorageKey.LIST_LOCAL_CO_OP; const key = StorageKey.LIST_LOCAL_CO_OP;
@ -93,14 +96,21 @@ export class GhPagesUtils {
.then(json => { .then(json => {
if (json.$schemaVersion === supportedSchema) { if (json.$schemaVersion === supportedSchema) {
window.localStorage.setItem(key, JSON.stringify(json)); 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 { } else {
window.localStorage.removeItem(key); 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) || '[]'); const info = JSON.parse(window.localStorage.getItem(key) || '{}');
return customList; if (info.$schemaVersion !== supportedSchema) {
// Delete storage;
window.localStorage.removeItem(key);
return new Set();
}
return new Set(Object.keys(info.data || {}));
} }
} }

View File

@ -5,17 +5,17 @@ export class LocalCoOpManager {
private static instance: LocalCoOpManager; private static instance: LocalCoOpManager;
public static getInstance = () => LocalCoOpManager.instance ?? (LocalCoOpManager.instance = new LocalCoOpManager()); public static getInstance = () => LocalCoOpManager.instance ?? (LocalCoOpManager.instance = new LocalCoOpManager());
private supportedIds: string[] = []; private supportedIds: Set<string>;
constructor() { constructor() {
BxEventBus.Script.once('list.localCoOp.updated', e => { BxEventBus.Script.once('list.localCoOp.updated', e => {
this.supportedIds = Object.keys(e.data.data); this.supportedIds = e.ids;
console.log('supportedIds', this.supportedIds);
}); });
GhPagesUtils.getLocalCoOpList(); this.supportedIds = GhPagesUtils.getLocalCoOpList();
console.log('this.supportedIds', this.supportedIds);
} }
isSupported(productId: string) { isSupported(productId: string) {
return this.supportedIds.includes(productId); return this.supportedIds.has(productId);
} }
} }