-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added external API to contribute start/stop trace server implementation
Added new external api for trace server startup and shutdown The new API allows adopting extension to register server startup handler and shutdown handlers and a validator function to check whether a trace file at given location is applicable Fixes #159 Signed-off-by: Alex Doan <[email protected]>
- Loading branch information
1 parent
8add693
commit b2c8f77
Showing
3 changed files
with
111 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*************************************************************************************** | ||
* Copyright (c) 2023 BlackBerry Limited and others. | ||
* | ||
* Licensed under the MIT license. See LICENSE file in the project root for details. | ||
***************************************************************************************/ | ||
export interface TraceServerContributor { | ||
startServer: () => Promise<void>; | ||
stopServer?: () => Promise<void>; | ||
isApplicable?: (pathToTrace: string) => boolean; | ||
} | ||
|
||
export class TraceServerManager { | ||
private traceServersContributors: TraceServerContributor[] = []; | ||
private isManagerDisposed = false; | ||
private indexOfTraceServerContributor = -1; | ||
|
||
/** | ||
* Add new contributor (groups of functions that runs on adopting extension to start/stop its trace server) to the manager | ||
* | ||
* @param contributor Contributor object that contains startServer, stopServer handlers and a traceValidator | ||
*/ | ||
addTraceServerContributor(contributor: TraceServerContributor): void { | ||
if (!this.isDisposed()) { | ||
this.traceServersContributors.push(contributor); | ||
} | ||
} | ||
|
||
/** | ||
* Look for appropriate startServer handler and execute it, also assign the index of current contributor | ||
* | ||
* @param pathToTrace path to trace file | ||
*/ | ||
async startServer(pathToTrace: string): Promise<void> { | ||
this.indexOfTraceServerContributor = -1; | ||
if (!this.isDisposed()) { | ||
// find an adopting extension that has successfully validated the trace | ||
let index = this.traceServersContributors.findIndex( | ||
traceServerContributor => traceServerContributor.isApplicable?.(pathToTrace) | ||
); | ||
if (index === -1) { | ||
// find an adopting extension with no validator | ||
index = this.traceServersContributors.findIndex( | ||
traceServerContributor => !traceServerContributor.isApplicable | ||
); | ||
} | ||
// if found | ||
if (index !== -1) { | ||
await this.traceServersContributors[index].startServer(); | ||
this.indexOfTraceServerContributor = index; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* execute server stopping handler | ||
*/ | ||
async stopServer(): Promise<void> { | ||
if (this.indexOfTraceServerContributor !== -1) { | ||
await this.traceServersContributors[this.indexOfTraceServerContributor].stopServer?.(); | ||
this.indexOfTraceServerContributor = -1; | ||
} | ||
} | ||
|
||
/** | ||
* remove all contributors, set manager to disposed status | ||
*/ | ||
dispose(): void { | ||
if (!this.isDisposed()) { | ||
this.traceServersContributors = []; | ||
this.isManagerDisposed = true; | ||
this.indexOfTraceServerContributor = -1; | ||
} | ||
} | ||
|
||
/** | ||
* whether manager is disposed | ||
* | ||
* @returns disposed status | ||
*/ | ||
isDisposed(): boolean { | ||
return this.isManagerDisposed; | ||
} | ||
} |