@observertc/observer-js
Version:
Server Side NodeJS Library for processing ObserveRTC Samples
54 lines (53 loc) • 1.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OnAllClientCallUpdater = void 0;
class OnAllClientCallUpdater {
_observedCall;
name = 'OnAllClientCallUpdater';
description = 'Call the update() method of the ObservedCall once all client has been updated';
_updatedClients = new Set();
closed = false;
constructor(_observedCall) {
this._observedCall = _observedCall;
this._onNewObservedClient = this._onNewObservedClient.bind(this);
this._observedCall.once('close', () => {
this._observedCall.off('newclient', this._onNewObservedClient);
});
this._observedCall.on('newclient', this._onNewObservedClient);
}
close() {
if (this.closed)
return;
this.closed = true;
// do nothing, as close once emitted unsubscription happened
this._updatedClients.clear();
}
_onNewObservedClient(observedClient) {
if (this.closed)
return;
const onUpdate = () => this._onClientUpdate(observedClient);
observedClient.once('close', () => {
observedClient.off('update', onUpdate);
this._updatedClients.delete(observedClient.clientId);
this._updateIfEveryClientUpdated();
});
observedClient.on('update', onUpdate);
}
_onClientUpdate(observedClient) {
if (observedClient.closed)
return;
this._updatedClients.add(observedClient.clientId);
this._updateIfEveryClientUpdated();
}
_updateIfEveryClientUpdated() {
if (this._updatedClients.size < this._observedCall.observedClients.size) {
return;
}
else if (this.closed) {
return;
}
this._updatedClients.clear();
this._observedCall.update();
}
}
exports.OnAllClientCallUpdater = OnAllClientCallUpdater;