-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for reusing running server.
The frontend subscribes to a server-sent-event stream and will refresh the page when instructed. PiperOrigin-RevId: 669092712
- Loading branch information
1 parent
8797ab2
commit 21874a7
Showing
2 changed files
with
73 additions
and
0 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
67 changes: 67 additions & 0 deletions
67
src/ui/src/components/visualizer/server_director_service.ts
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,67 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 The Model Explorer Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ============================================================================== | ||
*/ | ||
|
||
import {safeLocation} from 'safevalues/dom'; | ||
import {IS_EXTERNAL} from '../../common/flags'; | ||
|
||
import {Injectable} from '@angular/core'; | ||
|
||
enum DirectiveName { | ||
RefreshPage = 'refreshPage', | ||
} | ||
|
||
declare interface DirectiveBase { | ||
name: DirectiveName; | ||
} | ||
|
||
declare interface RefreshPageDirective extends DirectiveBase { | ||
name: DirectiveName.RefreshPage; | ||
url: string; | ||
} | ||
|
||
type Directive = RefreshPageDirective; | ||
|
||
/** A service for handling directives streaming from ME server. */ | ||
@Injectable() | ||
export class ServerDirectorService { | ||
init() { | ||
if (IS_EXTERNAL) { | ||
// Listen to the streaming events (directives) from the following source | ||
// that the server has established. | ||
const eventSource = new EventSource('/apistream/server_director'); | ||
eventSource.addEventListener('message', (e) => { | ||
if (!e.data) { | ||
return; | ||
} | ||
const directive = JSON.parse(e.data) as Directive; | ||
switch (directive.name) { | ||
// Refresh page with the given url. | ||
case DirectiveName.RefreshPage: | ||
safeLocation.setHref( | ||
window.location, | ||
directive.url, | ||
); | ||
|
||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
} | ||
} | ||
} |