@observertc/observer-js
Version:
Server Side NodeJS Library for processing ObserveRTC Samples
51 lines (50 loc) • 1.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OnAllCallObserverUpdater = void 0;
class OnAllCallObserverUpdater {
observer;
name = 'OnAllCallObserverUpdater';
description = 'Call Observer\'s update() method when all of the ObservedCalls are updated';
_updatedCalls = new Set();
closed = false;
constructor(observer) {
this.observer = observer;
this._onNewObservedCall = this._onNewObservedCall.bind(this);
this.observer.once('close', () => {
this.observer.off('newcall', this._onNewObservedCall);
});
this.observer.on('newcall', this._onNewObservedCall);
}
close() {
if (this.closed)
return;
this.closed = true;
this._updatedCalls.clear();
}
_onNewObservedCall(observedCall) {
if (this.closed)
return;
const onUpdate = () => this._onObservedCallUpdated(observedCall);
observedCall.once('close', () => {
observedCall.off('update', onUpdate);
this._updatedCalls.delete(observedCall.callId);
this._updateIfEveryCallUpdated();
});
observedCall.on('update', onUpdate);
}
_onObservedCallUpdated(observedCall) {
if (observedCall.closed)
return;
this._updatedCalls.add(observedCall.callId);
this._updateIfEveryCallUpdated();
}
_updateIfEveryCallUpdated() {
if (this._updatedCalls.size < this.observer.observedCalls.size)
return;
else if (this.closed)
return;
this._updatedCalls.clear();
this.observer.update();
}
}
exports.OnAllCallObserverUpdater = OnAllCallObserverUpdater;