diff options
author | Marco Ambrosini <marcoambrosini@pm.me> | 2020-04-22 15:34:32 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-05-08 16:46:19 +0200 |
commit | 02fe3366e7d7f8d0624cf89ce48554ea7b9a453a (patch) | |
tree | 74bec80f646807c1b7301b85aa8b0f95d85e0575 /src/mixins/call.js | |
parent | 18b93d83e5e2e8058de386887998771cbbb04934 (diff) |
Move methods to call mixin
Signed-off-by: Marco Ambrosini <marcoambrosini@pm.me>
Diffstat (limited to 'src/mixins/call.js')
-rw-r--r-- | src/mixins/call.js | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/mixins/call.js b/src/mixins/call.js index 582301ae0..9e4b2f867 100644 --- a/src/mixins/call.js +++ b/src/mixins/call.js @@ -39,6 +39,97 @@ const call = { }, methods: { + + /** + * Updates data properties that depend on the CallParticipantModels. + * + * The data contains some properties that can not be dynamically + * computed but that depend on the current CallParticipantModels, so + * this function adds and removes elements and watchers as needed based + * on the given CallParticipantModels. + * + * @param {Array} models the array of CallParticipantModels + */ + updateDataFromCallParticipantModels(models) { + const addedModels = models.filter(model => !this.sharedDatas[model.attributes.peerId]) + const removedModelIds = Object.keys(this.sharedDatas).filter(sharedDataId => models.find(model => model.attributes.peerId === sharedDataId) === undefined) + + removedModelIds.forEach(removedModelId => { + this.$delete(this.sharedDatas, removedModelId) + + this.speakingUnwatchers[removedModelId]() + // Not reactive, but not a problem + delete this.speakingUnwatchers[removedModelId] + + this.screenUnwatchers[removedModelId]() + // Not reactive, but not a problem + delete this.screenUnwatchers[removedModelId] + + const index = this.speakers.findIndex(speaker => speaker.id === removedModelId) + this.speakers.splice(index, 1) + + this._setScreenAvailable(removedModelId, false) + }) + + addedModels.forEach(addedModel => { + const sharedData = { + promoted: false, + videoEnabled: true, + screenVisible: false, + } + + this.$set(this.sharedDatas, addedModel.attributes.peerId, sharedData) + + // Not reactive, but not a problem + this.speakingUnwatchers[addedModel.attributes.peerId] = this.$watch(function() { + return addedModel.attributes.speaking + }, function(speaking) { + this._setSpeaking(addedModel.attributes.peerId, speaking) + }) + + this.speakers.push({ + id: addedModel.attributes.peerId, + active: false, + }) + + // Not reactive, but not a problem + this.screenUnwatchers[addedModel.attributes.peerId] = this.$watch(function() { + return addedModel.attributes.screen + }, function(screen) { + this._setScreenAvailable(addedModel.attributes.peerId, screen) + }) + }) + }, + + _setSpeaking(peerId, speaking) { + if (speaking) { + // Move the speaker to the first element of the list + const index = this.speakers.findIndex(speaker => speaker.id === peerId) + const speaker = this.speakers[index] + speaker.active = true + this.speakers.splice(index, 1) + this.speakers.unshift(speaker) + + return + } + + // Set the speaker as not speaking + const index = this.speakers.findIndex(speaker => speaker.id === peerId) + const speaker = this.speakers[index] + speaker.active = false + + // Move the speaker after all the active speakers + if (index === 0) { + this.speakers.shift() + + const firstInactiveSpeakerIndex = this.speakers.findIndex(speaker => !speaker.active) + if (firstInactiveSpeakerIndex === -1) { + this.speakers.push(speaker) + } else { + this.speakers.splice(firstInactiveSpeakerIndex, 0, speaker) + } + } + }, }, watch: { |