fix: get worker by workspace id

This commit is contained in:
linonetwo 2023-06-08 22:01:55 +08:00
parent ce9722416c
commit 037e6f7c15
3 changed files with 9 additions and 4 deletions

View file

@ -102,8 +102,8 @@ export class NativeService implements INativeService {
return toFilePath;
}
public executeZxScript$(zxWorkerArguments: IZxFileInput, wikiFolderLocation?: string): Observable<string> {
const zxWorker = this.wikiService.getWorker(wikiFolderLocation ?? this.workspaceService.getActiveWorkspaceSync()?.wikiFolderLocation ?? '');
public executeZxScript$(zxWorkerArguments: IZxFileInput, workspaceID?: string): Observable<string> {
const zxWorker = this.wikiService.getWorker(workspaceID ?? this.workspaceService.getActiveWorkspaceSync()?.id ?? '');
if (zxWorker === undefined) {
const error = new ZxNotInitializedError();
return new Observable<string>((observer) => {

View file

@ -29,9 +29,9 @@ export interface INativeService {
/**
* Execute zx script in a wiki worker and get result.
* @param zxWorkerArguments
* @param wikiFolderLocation Each wiki has its own worker, we use wiki's folder path to determine which worker to use. If not provided, will use current active workspace's wiki's path
* @param workspaceID Each wiki has its own worker, we use wiki's workspaceID to determine which worker to use. If not provided, will use current active workspace's ID
*/
executeZxScript$(zxWorkerArguments: IZxFileInput, wikiFolderLocation?: string): Observable<string>;
executeZxScript$(zxWorkerArguments: IZxFileInput, workspaceID?: string): Observable<string>;
getLocalHostUrlWithActualInfo(urlToReplace: string, workspaceID: string): Promise<string>;
handleFileProtocol(request: { url: string }, callback: (response: string) => void): Promise<void>;
log(level: string, message: string, meta?: Record<string, unknown>): Promise<void>;

View file

@ -217,6 +217,9 @@ function executeZxScript(file: IZxFileInput, zxPath: string): Observable<IZxWork
execution.on('close', function(code) {
observer.next({ type: 'control', actions: ZxWorkerControlActions.ended, message: `child process exited with code ${String(code)}` });
});
/**
* zx script may have multiple console.log, we need to extract some of them and execute them in the $tw context (`executeScriptInTWContext`). And send some of them back to frontend.
*/
execution.stdout?.on('data', (stdout: Buffer) => {
// if there are multiple console.log, their output will be concatenated into this stdout. And some of them are not intended to be executed. We use TW_SCRIPT_SEPARATOR to allow user determine the range they want to execute in the $tw context.
const message = String(stdout);
@ -228,11 +231,13 @@ function executeZxScript(file: IZxFileInput, zxPath: string): Observable<IZxWork
if (wikiInstance === undefined) {
observer.next({ type: 'stderr', message: `Error in executeZxScript(): $tw is undefined` });
} else {
// execute code in the $tw context
const context = getTWVmContext(wikiInstance);
const twExecutionResult = executeScriptInTWContext(content, context);
observer.next({ type: 'stdout', message: twExecutionResult.join('\n\n') });
}
} else {
// send some of them back to frontend.
observer.next({ type: 'stdout', message: content });
}
});