Skip to content

Commit 49aa6a8

Browse files
Google AI Edgecopybara-github
authored andcommitted
Refactor how worker is initialized
PiperOrigin-RevId: 895415083
1 parent 9641052 commit 49aa6a8

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/ui/src/components/visualizer/app_service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ export class AppService {
128128

129129
testMode = false;
130130

131+
onPortal = false;
132+
131133
private groupNodeChildrenCountThresholdFromUrl: string | null = null;
132134

133135
private paneIdToGraph: Record<string, Graph> = {};
@@ -143,7 +145,6 @@ export class AppService {
143145
private readonly uiStateService: UiStateService,
144146
private readonly workerService: WorkerService,
145147
) {
146-
this.listenToWorker();
147148
this.init();
148149
}
149150

@@ -1106,7 +1107,8 @@ export class AppService {
11061107
this.init();
11071108
}
11081109

1109-
private listenToWorker() {
1110+
setupWorker() {
1111+
this.workerService.init(this.onPortal);
11101112
this.workerService.worker.addEventListener('message', (event) => {
11111113
const workerEvent = event.data as WorkerEvent;
11121114
switch (workerEvent.eventType) {

src/ui/src/components/visualizer/model_graph_visualizer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ export class ModelGraphVisualizer implements OnInit, OnDestroy, OnChanges {
107107
/** The sources (file paths) of node data. */
108108
@Input() nodeDataSources: string[] = [];
109109

110+
/** Whether the visualizer is running on AI Edge Portal. */
111+
@Input() onPortal = false;
112+
110113
/** Triggered when the title is clicked. */
111114
@Output() readonly titleClicked = new EventEmitter<void>();
112115

@@ -253,6 +256,8 @@ export class ModelGraphVisualizer implements OnInit, OnDestroy, OnChanges {
253256
}
254257

255258
ngOnInit() {
259+
this.appService.onPortal = this.onPortal;
260+
this.appService.setupWorker();
256261
this.visualizerThemeService.init(this.el);
257262

258263
this.appService.config.set(this.config || {});

src/ui/src/components/visualizer/title_bar.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export class TitleBar {
108108
});
109109
}
110110

111+
get onPortal(): boolean {
112+
return this.appService.onPortal;
113+
}
114+
111115
private async runNdpExtension(
112116
extension: NodeDataProviderExtension,
113117
runName: string,

src/ui/src/components/visualizer/worker_service.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@
1616
* ==============================================================================
1717
*/
1818

19-
import {Injectable} from '@angular/core';
19+
import {Injectable, OnDestroy} from '@angular/core';
2020

2121
/**
2222
* Service to manage web worker.
2323
*/
2424
@Injectable()
25-
export class WorkerService {
26-
readonly worker: Worker;
25+
export class WorkerService implements OnDestroy {
26+
worker!: Worker;
2727

28-
constructor() {
28+
init(onPortal: boolean) {
29+
if (this.worker) {
30+
this.worker.terminate();
31+
}
2932
this.worker = new Worker(
3033
new URL('./worker/worker', import.meta.url), {type: 'classic'});
3134
}
35+
36+
ngOnDestroy() {
37+
this.worker.terminate();
38+
}
3239
}

0 commit comments

Comments
 (0)