rn-verto-typescript
Version:
Verto Free Switch with React Native Type Script
151 lines (150 loc) • 5.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ConferenceActionEvent_enum_1 = require("../enums/ConferenceActionEvent.enum");
class ConferenceLiveArray {
constructor(verto, liveArrayChannel, conferenceName, callbacks = {}) {
this.hashTable = {};
this.orderedCallIds = [];
this.lastSerialNumber = 0;
this.serialNumberErrors = 0;
this.verto = verto;
this.liveArrayChannel = liveArrayChannel;
this.conferenceName = conferenceName;
// TODO
this.callbacks = {
onBootstrappedMembers: (x) => x,
onAddedMember: (x) => x,
onModifiedMember: (x) => x,
onRemovedMember: (x) => x,
...callbacks,
};
this.subscription = verto.subscribe(liveArrayChannel, {
handler: this.handleEvent.bind(this),
userData: this,
});
this.destroyed = false;
this.bootstrap();
}
destroy() {
this.verto.unsubscribe(this.subscription.eventChannel);
this.destroyed = true;
}
// TODO value parameter
insertValue(callId, value, insertAt) {
if (this.hashTable[callId]) {
return;
}
this.hashTable[callId] = value;
if (insertAt === undefined || insertAt < 0 || insertAt >= this.orderedCallIds.length) {
this.orderedCallIds = [...this.orderedCallIds, callId];
return;
}
this.orderedCallIds = this.orderedCallIds.reduce((accumulator, currentCallId, currentIndex) => {
if (currentIndex === insertAt) {
return [...accumulator, callId, currentCallId];
}
return [...accumulator, currentCallId];
}, []);
}
deleteValue(callId) {
if (!this.hashTable[callId]) {
return false;
}
this.orderedCallIds = this.orderedCallIds.filter(existingCallId => existingCallId !== callId);
delete this.hashTable[callId];
return true;
}
checkSerialNumber(serialNumber) {
if (this.lastSerialNumber > 0 && serialNumber !== (this.lastSerialNumber + 1)) {
this.serialNumberErrors += 1;
if (this.serialNumberErrors < 3) {
this.bootstrap();
}
return false;
}
if (serialNumber > 0) {
this.lastSerialNumber = serialNumber;
}
return true;
}
// TODO dataArray parameter
handleBootingEvent(eventSerialNumber, dataArray) {
if (!this.checkSerialNumber(eventSerialNumber)) {
return;
}
dataArray.forEach((data) => {
const [callId, value] = data;
this.insertValue(callId, value);
});
this.callbacks.onBootstrappedMembers({ dataArray });
}
handleAddingEvent(eventSerialNumber, value, callId, index) {
if (!this.checkSerialNumber(eventSerialNumber)) {
return;
}
// TODO Control below eventSerialNumber parameter type
this.insertValue(callId || eventSerialNumber.toString(), value, index);
this.callbacks.onAddedMember(value);
}
handleModifyingEvent(eventSerialNumber, value, callId, index) {
if (!this.checkSerialNumber(eventSerialNumber)) {
return;
}
// TODO Control below eventSerialNumber parameter type
this.insertValue(callId || eventSerialNumber.toString(), value, index);
this.callbacks.onModifiedMember(value);
}
handleDeleteEvent(eventSerialNumber, callId, index) {
if (!this.checkSerialNumber(eventSerialNumber)) {
return;
}
const eventIndexIsInvalid = (index === null) || (index === undefined) || (index < 0);
// TODO Control below eventSerialNumber parameter type
const localIndex = this.orderedCallIds.indexOf(callId || eventSerialNumber.toString());
// TODO Control below eventSerialNumber parameter type
const isDiffAfterBoot = this.deleteValue(callId || eventSerialNumber.toString());
if (!isDiffAfterBoot) {
return;
}
const report = { callId, index: eventIndexIsInvalid ? localIndex : index };
this.callbacks.onRemovedMember(report);
}
// TODO event parameter
handleEvent(event, liveArray) {
const { wireSerno: serialNumber, arrIndex: arrayIndex, name: conferenceName, data: payload, hashKey: callId, action, } = event.data;
if (conferenceName !== liveArray.conferenceName) {
return;
}
switch (action) {
case ConferenceActionEvent_enum_1.ConferenceActionEvent.boot:
this.handleBootingEvent(serialNumber, payload);
break;
case ConferenceActionEvent_enum_1.ConferenceActionEvent.add:
this.handleAddingEvent(serialNumber, payload, callId, arrayIndex);
break;
case ConferenceActionEvent_enum_1.ConferenceActionEvent.modify:
if (arrayIndex || callId) {
this.handleModifyingEvent(serialNumber, payload, callId, arrayIndex);
}
break;
case ConferenceActionEvent_enum_1.ConferenceActionEvent.delete:
if (arrayIndex || callId) {
this.handleDeleteEvent(serialNumber, callId, arrayIndex);
}
break;
default:
console.log('Ignoring not implemented live array action', action);
break;
}
}
bootstrap() {
this.verto.broadcast(this.liveArrayChannel, {
liveArray: {
command: 'bootstrap',
context: this.liveArrayChannel,
name: this.conferenceName,
},
});
}
}
exports.default = ConferenceLiveArray;