Skip to content

Commit

Permalink
build: migration update.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Jan 7, 2025
1 parent 9d6faa3 commit 0b60a0a
Showing 1 changed file with 57 additions and 24 deletions.
81 changes: 57 additions & 24 deletions apps/mobile/src/core/services/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ export type IManageToken = {
tokenId: string;
};

function makeManageTokenKey(x: IManageToken): `${string}-${string}` {
return `${x.chainId}-${x.tokenId}`;
function makeManageTokenKey(x: IManageToken) {
// return `${x.chainId}-${x.tokenId}`;
return JSON.stringify(x);
}

function decodeManageTokenKey(x: string): IManageToken {
const [chainId, tokenId] = JSON.parse(x);
return { chainId, tokenId };
}

export type IDefiOrToken = {
Expand All @@ -69,8 +75,14 @@ export type IDefiOrToken = {
type: 'token' | 'defi';
};

function makeDefiOrTokenKey(x: IDefiOrToken): `${string}-${string}-${string}` {
return `${x.chainid}-${x.id}-${x.type}`;
function makeDefiOrTokenKey(x: IDefiOrToken) {
// return `${x.chainid}-${x.id}-${x.type}`;
return JSON.stringify(x);
}

function decodeDefiOrTokenKey(x: string): IDefiOrToken {
const [chainid, id, type] = JSON.parse(x);
return { chainid, id, type };
}

export type ITokenSetting = {
Expand Down Expand Up @@ -250,14 +262,11 @@ export class PreferenceService {
),
};

Object.entries(tokenManageSettingMap).forEach(([eoaAddr, setting]) => {
Object.values(tokenManageSettingMap).forEach(setting => {
(['pinedQueue', 'foldTokens', 'unfoldTokens'] as const).forEach(key => {
setting[key]?.forEach(item => {
const k = makeManageTokenKey(item);
if (!sets.pinedQueue.has(k)) {
lists[key].push(item);
sets[key].add(k);
}
if (!sets[key].has(k)) sets[key].add(k);
});

if (!__DEV__) delete setting[key];
Expand All @@ -267,28 +276,52 @@ export class PreferenceService {
key => {
setting[key]?.forEach(item => {
const k = makeDefiOrTokenKey(item);
if (!sets[key].has(k)) {
lists[key].push(item);
sets[key].add(k);
}
if (!sets[key].has(k)) sets[key].add(k);
});

if (!__DEV__) delete setting[key];
},
);
});

this.store.pinedQueue = lists.pinedQueue;
// console.debug(
// '[preference::_migrate] this.store.pinedQueue',
// this.store.pinedQueue,
// );
this.store.foldTokens = lists.foldTokens;
this.store.unfoldTokens = lists.unfoldTokens;
this.store.includeDefiAndTokens = lists.includeDefiAndTokens;
this.store.excludeDefiAndTokens = lists.excludeDefiAndTokens;

this.store.tokenManageSettingMap = tokenManageSettingMap;
priority_process: {
// pinedQueue > foldTokens > unfoldTokens
sets.pinedQueue.forEach(k => {
sets.foldTokens.delete(k);
sets.unfoldTokens.delete(k);
});
sets.foldTokens.forEach(k => {
sets.unfoldTokens.delete(k);
});

lists.pinedQueue = [...sets.pinedQueue].map(k => decodeManageTokenKey(k));
lists.foldTokens = [...sets.foldTokens].map(k => decodeManageTokenKey(k));
lists.unfoldTokens = [...sets.unfoldTokens].map(k =>
decodeManageTokenKey(k),
);

// excludeDefiAndTokens > includeDefiAndTokens
sets.excludeDefiAndTokens.forEach(k => {
sets.includeDefiAndTokens.delete(k);
});

lists.excludeDefiAndTokens = [...sets.excludeDefiAndTokens].map(k =>
decodeDefiOrTokenKey(k),
);
lists.includeDefiAndTokens = [...sets.includeDefiAndTokens].map(k =>
decodeDefiOrTokenKey(k),
);
}

flush_back: {
this.store.pinedQueue = lists.pinedQueue;
this.store.foldTokens = lists.foldTokens;
this.store.unfoldTokens = lists.unfoldTokens;
this.store.includeDefiAndTokens = lists.includeDefiAndTokens;
this.store.excludeDefiAndTokens = lists.excludeDefiAndTokens;

this.store.tokenManageSettingMap = tokenManageSettingMap;
}
}

/* eslint-disable no-dupe-class-members */
Expand Down

0 comments on commit 0b60a0a

Please sign in to comment.