Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 90 additions & 44 deletions packages/extension-polkagate/src/util/workers/sharedWorker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2019-2025 @polkadot/extension-polkagate authors & contributors
// SPDX-License-Identifier: Apache-2.0

//@ts-nocheck
// @ts-nocheck

import { createAssets } from '@polkagate/apps-config/assets';

Expand All @@ -15,6 +15,52 @@ import getValidatorsInformation from './shared-helpers/getValidatorsInformation.

const assetsChains = createAssets();

// Queue system
class RequestQueue {
constructor () {
this.queue = [];
this.isProcessing = false;
}

add (task) {
this.queue.push(task);
console.info(`Shared worker: Task added to queue. Queue length: ${this.queue.length}`);

if (!this.isProcessing) {
this.processNext().catch(console.error);
}
}

async processNext () {
if (this.queue.length === 0) {
this.isProcessing = false;
console.info('Shared worker: Queue empty, processing complete');

return;
}

this.isProcessing = true;
const task = this.queue.shift();

console.info(`Shared worker: Processing task. Remaining in queue: ${this.queue.length}`);

try {
await task();
} catch (error) {
console.error('Shared worker: Error processing task', error);
}

// Process next task
this.processNext().catch(console.error);
}

getQueueLength () {
return this.queue.length;
}
}

const requestQueue = new RequestQueue();

// Handle connections to the shared worker
onconnect = (/** @type {{ ports: any[]; }} */ event) => {
const port = event.ports[0]; // Get the MessagePort from the connection
Expand All @@ -28,60 +74,60 @@ onconnect = (/** @type {{ ports: any[]; }} */ event) => {

console.info('Shared worker, message received for:', functionName, parameters);

try {
switch (functionName) {
case FETCHING_ASSETS_FUNCTION_NAMES.RELAY: {
getAssetOnRelayChain(...params, port).catch(console.error);
break;
}

case FETCHING_ASSETS_FUNCTION_NAMES.MULTI_ASSET: {
// eslint-disable-next-line no-case-declarations
const assetsToBeFetched = assetsChains[parameters.chainName];

/** if assetsToBeFetched === undefined then we don't fetch assets by default at first, but will fetch them on-demand later in account details page*/
if (!assetsToBeFetched) {
console.info(`Shared worker, getAssetOnMultiAssetChain: No assets to be fetched on ${parameters.chainName}`);

return port.postMessage(JSON.stringify({ functionName })); // FIXME: if this happens, should be handled in caller
// Add the request to the queue
requestQueue.add(async () => {
try {
switch (functionName) {
case FETCHING_ASSETS_FUNCTION_NAMES.RELAY: {
await getAssetOnRelayChain(...params, port);
break;
}

getAssetOnMultiAssetChain(assetsToBeFetched, ...params, port).catch(console.error);
break;
}
case FETCHING_ASSETS_FUNCTION_NAMES.MULTI_ASSET: {
const assetsToBeFetched = assetsChains[parameters.chainName];

case FETCHING_ASSETS_FUNCTION_NAMES.ASSET_HUB: {
if (!parameters.assetsToBeFetched) {
console.warn('getAssetOnAssetHub: No assets to be fetched on, but just Native Token');
/** if assetsToBeFetched === undefined then we don't fetch assets by default at first, but will fetch them on-demand later in account details page*/
if (!assetsToBeFetched) {
console.info(`Shared worker, getAssetOnMultiAssetChain: No assets to be fetched on ${parameters.chainName}`);
port.postMessage(JSON.stringify({ functionName }));

parameters.assetsToBeFetched = [];
return;
}

await getAssetOnMultiAssetChain(assetsToBeFetched, ...params, port);
break;
}

getAssetOnAssetHub(...params, port).catch(console.error);
break;
}
case FETCHING_ASSETS_FUNCTION_NAMES.ASSET_HUB: {
if (!parameters.assetsToBeFetched) {
console.warn('getAssetOnAssetHub: No assets to be fetched on, but just Native Token');
parameters.assetsToBeFetched = [];
}

case 'getNFTs':
getNFTs(...params, port).catch(console.error);
break;
await getAssetOnAssetHub(...params, port);
break;
}

case 'getValidatorsInformation':
getValidatorsInformation(...params, port).catch(console.error);
break;
case 'getNFTs':
await getNFTs(...params, port);
break;

case 'getPool':
getPool(...params, port).catch(console.error);
break;
case 'getValidatorsInformation':
await getValidatorsInformation(...params, port);
break;

default:
console.error('unknown function sent to shared worker!');
case 'getPool':
await getPool(...params, port);
break;

return port.postMessage(JSON.stringify({ functionName }));
default:
console.error('unknown function sent to shared worker!');
port.postMessage(JSON.stringify({ functionName }));
}
} catch (error) {
console.error(`Error while shared worker running ${functionName}`, error);
port.postMessage(JSON.stringify({ functionName, error: error.message }));
}
} catch (error) {
console.error(`Error while shared worker probably running ${functionName}`, error);

return port.postMessage(JSON.stringify({ functionName }));
}
});
};
};
Loading