diff --git a/packages/shared/src/lib/core/nfts/actions/addNftsToDownloadQueue.ts b/packages/shared/src/lib/core/nfts/actions/addNftsToDownloadQueue.ts index 7e5e273cab..52bd8560a3 100644 --- a/packages/shared/src/lib/core/nfts/actions/addNftsToDownloadQueue.ts +++ b/packages/shared/src/lib/core/nfts/actions/addNftsToDownloadQueue.ts @@ -1,6 +1,6 @@ import { get } from 'svelte/store' import { Nft } from '../interfaces' -import { addNftToDownloadQueue, nftDownloadQueue, updateNftForAllAccounts, updatePersistedNft } from '../stores' +import { nftDownloadQueue, updateNftsForAllAccounts, updatePersistedNfts } from '../stores' import { checkIfNftShouldBeDownloaded } from '../utils/checkIfNftShouldBeDownloaded' import { NftDownloadOptions } from '../types' @@ -10,37 +10,50 @@ export async function addNftsToDownloadQueue(nfts: Nft[], options?: Partial ({ id: nft.id, downloadMetadata: nft.downloadMetadata }))) + updateNftsForAllAccounts( + checkedNfts.map(({ nft }) => ({ id: nft.id, downloadMetadata: nft.downloadMetadata, isLoaded: nft.isLoaded })) + ) + + addNftsToDownloadQueueIfShouldBeDownloaded(checkedNfts, fullOptions) +} + +async function validateNftAndCheckIfShouldBeDownloaded( + nfts: Nft[], + options: NftDownloadOptions +): Promise<{ nft: Nft; shouldDownload: boolean }[]> { + const checkedNfts: { nft: Nft; shouldDownload: boolean }[] = [] for (const nft of nfts) { const isNftInDownloadQueue = get(nftDownloadQueue).some((queueItem) => queueItem.nft.id === nft.id) if (isNftInDownloadQueue || nft.isLoaded) { continue } + try { + const { shouldDownload, downloadMetadata, isLoaded } = await checkIfNftShouldBeDownloaded( + nft, + options.skipDownloadSettingsCheck + ) - const nftToAdd = await validateNft(nft, fullOptions.skipDownloadSettingsCheck) - if (nftToAdd) { - nftsToAdd.push(nftToAdd) + checkedNfts.push({ nft: { ...nft, downloadMetadata, isLoaded }, shouldDownload }) + } catch (error) { + console.error(error) } } - - for (const nft of nftsToAdd) { - addNftToDownloadQueue(nft, fullOptions) - } + return checkedNfts } -async function validateNft(nft: Nft, skipDownloadSettingsCheck: boolean): Promise { - try { - const { shouldDownload, downloadMetadata, isLoaded } = await checkIfNftShouldBeDownloaded( - nft, - skipDownloadSettingsCheck - ) - updatePersistedNft(nft.id, { downloadMetadata }) - updateNftForAllAccounts({ id: nft.id, downloadMetadata, isLoaded }) - - if (shouldDownload) { - return nft +function addNftsToDownloadQueueIfShouldBeDownloaded( + nftsToAdd: { nft: Nft; shouldDownload: boolean }[], + options: NftDownloadOptions +): void { + nftDownloadQueue.update((state) => { + for (const nft of nftsToAdd) { + if (!nft.shouldDownload) { + state.push({ nft: nft.nft, options }) + } } - } catch (error) { - console.error(error) - } + return state + }) } diff --git a/packages/shared/src/lib/core/nfts/stores/active-profile-nfts-per-account.store.ts b/packages/shared/src/lib/core/nfts/stores/active-profile-nfts-per-account.store.ts index 5e3bfe066b..eecd55f4b0 100644 --- a/packages/shared/src/lib/core/nfts/stores/active-profile-nfts-per-account.store.ts +++ b/packages/shared/src/lib/core/nfts/stores/active-profile-nfts-per-account.store.ts @@ -67,8 +67,12 @@ export function updateNftForAccount(accountIndex: number, partialNft: PartialWit } export function updateNftForAllAccounts(partialNft: PartialWithId): void { + updateNftsForAllAccounts([partialNft]) +} + +export function updateNftsForAllAccounts(partialNfts: PartialWithId[]): void { for (const accountIndex of Object.keys(get(activeProfileNftsPerAccount)) as unknown as number[]) { - updateNftForAccount(accountIndex, partialNft) + updateNftsForAccount(accountIndex, partialNfts) } } diff --git a/packages/shared/src/lib/core/nfts/stores/nft-download-queue.store.ts b/packages/shared/src/lib/core/nfts/stores/nft-download-queue.store.ts index b1d8161f68..0ef212adb3 100644 --- a/packages/shared/src/lib/core/nfts/stores/nft-download-queue.store.ts +++ b/packages/shared/src/lib/core/nfts/stores/nft-download-queue.store.ts @@ -5,17 +5,6 @@ import { NftDownloadOptions } from '../types' export const nftDownloadQueue = writable<{ nft: Nft; options: NftDownloadOptions }[]>([]) -export function addNftToDownloadQueue(nft: Nft, options: NftDownloadOptions): void { - const isNftInDownloadQueue = get(nftDownloadQueue).some((nftInQueue) => nftInQueue.nft.id === nft.id) - if (isNftInDownloadQueue) { - return - } - - nftDownloadQueue.update((state) => { - return [...state, { nft, options }] - }) -} - export function removeNftFromDownloadQueue(nftId: string): void { const isNftInDownloadQueue = get(nftDownloadQueue).some((nftInQueue) => nftInQueue.nft.id === nftId) if (!isNftInDownloadQueue) { diff --git a/packages/shared/src/lib/core/nfts/stores/persisted-nfts.store.ts b/packages/shared/src/lib/core/nfts/stores/persisted-nfts.store.ts index 3fc07d334d..40959e4cca 100644 --- a/packages/shared/src/lib/core/nfts/stores/persisted-nfts.store.ts +++ b/packages/shared/src/lib/core/nfts/stores/persisted-nfts.store.ts @@ -5,6 +5,7 @@ import { persistent } from '@core/utils/store' import { IPersistedNfts } from '../interfaces' import { PersistedNft } from '../types' +import { PartialWithId } from '@core/utils/types' export const persistedNfts = persistent('persistedNfts', {}) @@ -24,21 +25,27 @@ export function addPersistedNft(nftId: string, newPersistedNft: PersistedNft): v }) } -export function updatePersistedNft(nftId: string, payload: Partial): void { +export function updatePersistedNfts(nftsToUpdate: PartialWithId[]): void { const profileId = getActiveProfileId() persistedNfts.update((state) => { if (!state[profileId]) { state[profileId] = {} } - const nftState = state[profileId][nftId] - state[profileId][nftId] = { - ...nftState, - ...payload, - } as PersistedNft + for (const partialNft of nftsToUpdate) { + const nftState = state[profileId][partialNft.id] + state[profileId][partialNft.id] = { + ...nftState, + ...partialNft, + } as PersistedNft + } return state }) } +export function updatePersistedNft(nftId: string, payload: Partial): void { + updatePersistedNfts([{ id: nftId, ...payload }]) +} + export function removePersistedNftsForProfile(profileId: string): void { persistedNfts.update((state) => { delete state[profileId] diff --git a/packages/shared/src/lib/core/nfts/stores/selected-account-collections.store.ts b/packages/shared/src/lib/core/nfts/stores/selected-account-collections.store.ts index 68105a4dc9..f2e3e40800 100644 --- a/packages/shared/src/lib/core/nfts/stores/selected-account-collections.store.ts +++ b/packages/shared/src/lib/core/nfts/stores/selected-account-collections.store.ts @@ -4,6 +4,7 @@ import { Nft } from '../interfaces' import { Collections } from '../types' import { getCollectionFromNft } from '../utils' import { ownedNfts, selectedAccountNfts } from './selected-account-nfts.store' +import { isFeatureEnabled } from '@lib/features/utils' export const collectionsStore: Writable = writable({}) @@ -50,7 +51,9 @@ async function updateCollections(nfts: Nft[]): Promise { } selectedAccountNfts.subscribe((nfts) => { - void updateCollections(nfts.filter((nft) => nft.isSpendable)) + if (isFeatureEnabled('collectibles.collections')) { + void updateCollections(nfts.filter((nft) => nft.isSpendable)) + } }) export const selectedAccountCollections: Readable = derived( diff --git a/packages/shared/src/lib/core/profile-manager/api/getClient.ts b/packages/shared/src/lib/core/profile-manager/api/getClient.ts index 5edd0adc66..a4e8f4a636 100644 --- a/packages/shared/src/lib/core/profile-manager/api/getClient.ts +++ b/packages/shared/src/lib/core/profile-manager/api/getClient.ts @@ -3,11 +3,22 @@ import { api } from './api' import { get } from 'svelte/store' import { profileManager } from '../stores' -export function getClient(): Promise { - const manager = get(profileManager) - if (!manager) { - return Promise.reject('No profile manager') +let client: Client | undefined + +export async function getClient(): Promise { + if (!client) { + const manager = get(profileManager) + if (!manager) { + return Promise.reject('No profile manager') + } + client = await api.getClient(manager.id) + } + return client +} + +export async function resetClient(): Promise { + if (client) { + await client.destroy() } - const { id } = manager - return api.getClient(id) + client = undefined } diff --git a/packages/shared/src/lib/core/profile/actions/active-profile/logout.ts b/packages/shared/src/lib/core/profile/actions/active-profile/logout.ts index 616e821174..f9f7ce74f7 100644 --- a/packages/shared/src/lib/core/profile/actions/active-profile/logout.ts +++ b/packages/shared/src/lib/core/profile/actions/active-profile/logout.ts @@ -25,6 +25,7 @@ import { clearLayer2Balance } from '@core/layer-2/stores' import { clearActiveProfileNftsPerAccount } from '@core/nfts/stores' import { clearAccountActivities } from '@core/activity/stores' import { destroyNetworks } from '@core/network/stores' +import { resetClient } from '@core/profile-manager/api' /** * Logout from active profile @@ -81,6 +82,7 @@ function cleanupProfileState(clearActiveProfile: boolean): void { async function destroyWalletRsObjects(manager?: IProfileManager): Promise { isDestroyingManager.set(true) + await resetClient() await manager?.stopBackgroundSync() await unsubscribeFromWalletApiEvents() await destroyProfileManager()