-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure node worker is used only in desktop bundle of ipynb extension
- Loading branch information
1 parent
15e541b
commit 11213f1
Showing
6 changed files
with
108 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { DeferredPromise, generateUuid } from './helper'; | ||
import { NotebookSerializerBase } from './notebookSerializer'; | ||
|
||
export class NotebookSerializer extends NotebookSerializerBase { | ||
private experimentalSave = vscode.workspace.getConfiguration('ipynb').get('experimental.serialization', false); | ||
private worker?: import('node:worker_threads').Worker; | ||
private tasks = new Map<string, DeferredPromise<Uint8Array>>(); | ||
|
||
constructor(context: vscode.ExtensionContext) { | ||
super(context); | ||
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => { | ||
if (e.affectsConfiguration('ipynb.experimental.serialization')) { | ||
this.experimentalSave = vscode.workspace.getConfiguration('ipynb').get('experimental.serialization', false); | ||
} | ||
})); | ||
} | ||
|
||
override dispose() { | ||
try { | ||
void this.worker?.terminate(); | ||
} catch { | ||
// | ||
} | ||
super.dispose(); | ||
} | ||
|
||
public override async serializeNotebook(data: vscode.NotebookData, token: vscode.CancellationToken): Promise<Uint8Array> { | ||
if (this.disposed) { | ||
return new Uint8Array(0); | ||
} | ||
|
||
if (this.experimentalSave) { | ||
return this.serializeViaWorker(data); | ||
} | ||
|
||
return super.serializeNotebook(data, token); | ||
} | ||
|
||
private async startWorker() { | ||
if (this.disposed) { | ||
throw new Error('Serializer disposed'); | ||
} | ||
if (this.worker) { | ||
return this.worker; | ||
} | ||
const { Worker } = await import('node:worker_threads'); | ||
const outputDir = getOutputDir(this.context); | ||
this.worker = new Worker(vscode.Uri.joinPath(this.context.extensionUri, outputDir, 'notebookSerializerWorker.js').fsPath, {}); | ||
this.worker.on('exit', (exitCode) => { | ||
if (!this.disposed) { | ||
console.error(`IPynb Notebook Serializer Worker exited unexpectedly`, exitCode); | ||
} | ||
this.worker = undefined; | ||
}); | ||
this.worker.on('message', (result: { data: Uint8Array; id: string }) => { | ||
const task = this.tasks.get(result.id); | ||
if (task) { | ||
task.complete(result.data); | ||
this.tasks.delete(result.id); | ||
} | ||
}); | ||
this.worker.on('error', (err) => { | ||
if (!this.disposed) { | ||
console.error(`IPynb Notebook Serializer Worker errored unexpectedly`, err); | ||
} | ||
}); | ||
return this.worker; | ||
} | ||
private async serializeViaWorker(data: vscode.NotebookData): Promise<Uint8Array> { | ||
const worker = await this.startWorker(); | ||
const id = generateUuid(); | ||
|
||
const deferred = new DeferredPromise<Uint8Array>(); | ||
this.tasks.set(id, deferred); | ||
worker.postMessage({ data, id }); | ||
|
||
return deferred.p; | ||
} | ||
} | ||
|
||
|
||
function getOutputDir(context: vscode.ExtensionContext): string { | ||
const main = context.extension.packageJSON.main as string; | ||
return main.indexOf('/dist/') !== -1 ? 'dist' : 'out'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { NotebookSerializerBase } from './notebookSerializer'; | ||
|
||
export class NotebookSerializer extends NotebookSerializerBase { | ||
} |