Track client connections and autostop server add-ons when all clients leave

Reviewed By: mweststrate

Differential Revision: D34045584

fbshipit-source-id: 1ad0cfffb9d304f0359c973d76d6956f7e932f72
This commit is contained in:
Andrey Goncharov
2022-02-28 03:50:34 -08:00
committed by Facebook GitHub Bot
parent 9113006851
commit 3b390b74ff
9 changed files with 87 additions and 46 deletions

View File

@@ -155,34 +155,39 @@ export class PluginManager {
}
}
async startServerAddOn(pluginName: string) {
async startServerAddOn(pluginName: string, owner: string) {
console.debug('PluginManager.startServerAddOn', pluginName);
// TODO: Get owner from websocket connection ID
const fakeOwner = 'test';
const existingServerAddOn = this.serverAddOns.get(pluginName);
if (existingServerAddOn) {
console.debug(
'PluginManager.startServerAddOn -> already started, adding an owner',
pluginName,
fakeOwner,
owner,
);
existingServerAddOn.addOwner(fakeOwner);
existingServerAddOn.addOwner(owner);
return;
}
const newServerAddOn = await ServerAddOn.start(pluginName, fakeOwner);
const newServerAddOn = await ServerAddOn.start(pluginName, owner, () =>
this.serverAddOns.delete(pluginName),
);
this.serverAddOns.set(pluginName, newServerAddOn);
}
stopServerAddOn(pluginName: string) {
stopServerAddOn(pluginName: string, owner: string) {
console.debug('PluginManager.stopServerAddOn', pluginName);
const serverAddOn = this.serverAddOns.get(pluginName);
if (!serverAddOn) {
console.debug('PluginManager.stopServerAddOn -> not started', pluginName);
return;
}
// TODO: Get owner from websocket connection ID
const fakeOwner = 'test';
serverAddOn.removeOwner(fakeOwner);
serverAddOn.removeOwner(owner);
}
stopAllServerAddOns(owner: string) {
console.debug('PluginManager.stopAllServerAddOns');
this.serverAddOns.forEach((serverAddOn) => {
serverAddOn.removeOwner(owner);
});
}
}