Skip to content

Commit

Permalink
used concurrency handler to make fetch call worker script
Browse files Browse the repository at this point in the history
  • Loading branch information
ritikramuka committed Oct 27, 2023
1 parent b2568e6 commit 5d9d994
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
33 changes: 33 additions & 0 deletions src/web/client/WebExtensionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,39 @@ class WebExtensionContext implements IWebExtensionContext {
return this._vscodeWorkspaceState.get(key);
}

public async getWorkerScript(workerUrl : URL) : Promise<any> {

Check warning on line 677 in src/web/client/WebExtensionContext.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Unexpected any. Specify a different type

Check warning on line 677 in src/web/client/WebExtensionContext.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Unexpected any. Specify a different type

Check warning on line 677 in src/web/client/WebExtensionContext.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Unexpected any. Specify a different type
try {
this.telemetry.sendInfoTelemetry(
telemetryEventNames.WEB_EXTENSION_FETCH_WORKER_SCRIPT,
{ workerUrl: workerUrl.toString() }
);

const response = await this.concurrencyHandler.handleRequest(
workerUrl
)

if (!response.ok) {
throw new Error(
`Failed to fetch worker script '${workerUrl.toString()}': ${response.statusText}`
);
}

this.telemetry.sendInfoTelemetry(
telemetryEventNames.WEB_EXTENSION_FETCH_WORKER_SCRIPT_SUCCESS,
{ workerUrl: workerUrl.toString() }
);

return await response.text();
} catch (error) {
this.telemetry.sendErrorTelemetry(
telemetryEventNames.WEB_EXTENSION_FETCH_WORKER_SCRIPT_FAILED,
this.getWorkerScript.name,
(error as Error)?.message,
error as Error
);
}
}

public setWorker(worker: Worker) {
this._worker = worker;
}
Expand Down
56 changes: 27 additions & 29 deletions src/web/client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,40 +311,38 @@ export function createWebWorkerInstance(

const workerUrl = new URL(webworkerMain.toString());

fetch(workerUrl)
.then((response) => response.text())
.then((workerScript) => {
const workerBlob = new Blob([workerScript], {
type: "application/javascript",
});
WebExtensionContext.getWorkerScript(workerUrl)
.then((workerScript) => {
const workerBlob = new Blob([workerScript], {
type: "application/javascript",
});

const workerUrl = URL.createObjectURL(workerBlob);
const urlObj = URL.createObjectURL(workerBlob);

WebExtensionContext.setWorker(new Worker(workerUrl));
WebExtensionContext.setWorker(new Worker(urlObj));

if (WebExtensionContext.worker !== undefined) {
WebExtensionContext.worker.onmessage = (event) => {
const { data } = event;
if (WebExtensionContext.worker !== undefined) {
WebExtensionContext.worker.onmessage = (event) => {
const { data } = event;

WebExtensionContext.containerId = event.data.containerId;
WebExtensionContext.containerId = event.data.containerId;

if (data.type === Constants.workerEventMessages.REMOVE_CONNECTED_USER) {
WebExtensionContext.removeConnectedUserInContext(
data.userId
);
}
if (data.type === Constants.workerEventMessages.UPDATE_CONNECTED_USERS) {
WebExtensionContext.updateConnectedUsersInContext(
data.containerId,
data.userName,
data.userId,
data.entityId
);
}
};
}
})
// TODO: add Telemetry
if (data.type === Constants.workerEventMessages.REMOVE_CONNECTED_USER) {
WebExtensionContext.removeConnectedUserInContext(
data.userId
);
}
if (data.type === Constants.workerEventMessages.UPDATE_CONNECTED_USERS) {
WebExtensionContext.updateConnectedUsersInContext(
data.containerId,
data.userName,
data.userId,
data.entityId
);
}
};
}
});
}

export async function showSiteVisibilityDialog() {
Expand Down
3 changes: 3 additions & 0 deletions src/web/client/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@ export enum telemetryEventNames {
WEB_EXTENSION_FETCH_GET_OR_CREATE_SHARED_WORK_SPACE_ERROR = 'webExtensionFetchGetOrCreateSharedWorkSpaceError',
WEB_EXTENSION_POPULATE_SHARED_WORKSPACE_SYSTEM_ERROR = 'webExtensionPopulateSharedWorkSpaceSystemError',
WEB_EXTENSION_POPULATE_SHARED_WORKSPACE_SUCCESS = 'webExtensionPopulateSharedWorkSpaceSuccess',
WEB_EXTENSION_FETCH_WORKER_SCRIPT = 'webExtensionFetchWorkerScript',
WEB_EXTENSION_FETCH_WORKER_SCRIPT_SUCCESS = 'webExtensionFetchWorkerScriptSuccess',
WEB_EXTENSION_FETCH_WORKER_SCRIPT_FAILED = 'webExtensionFetchWorkerScriptFailed',
}

0 comments on commit 5d9d994

Please sign in to comment.