Skip to content

Commit

Permalink
fix(participants): move interval update of talking time counter to th…
Browse files Browse the repository at this point in the history
…e store

Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Mar 13, 2024
1 parent a67611b commit 39868a1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 37 deletions.
39 changes: 9 additions & 30 deletions src/components/RightSidebar/Participants/Participant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

<!-- Phone participant dial action -->
<div v-if="isInCall && canBeModerated && isPhoneActor"
id="participantNavigationId"
:id="participantNavigationId"
class="participant-row__dial-actions">
<NcButton v-if="!participant.inCall"
type="success"
Expand Down Expand Up @@ -464,8 +464,6 @@ export default {
isUserNameTooltipVisible: false,
isStatusTooltipVisible: false,
permissionsEditor: false,
speakingInterval: null,
timeSpeaking: null,
disabled: false,
}
},
Expand Down Expand Up @@ -864,22 +862,17 @@ export default {
}
return ''
},
},
watch: {
isParticipantSpeaking(speaking) {
if (speaking) {
if (!this.speakingInterval) {
this.speakingInterval = setInterval(this.computeElapsedTime, 1000)
}
} else {
if (speaking === undefined) {
this.timeSpeaking = 0
}
clearInterval(this.speakingInterval)
this.speakingInterval = null
timeSpeaking() {
if (!this.participantSpeakingInformation || this.isParticipantSpeaking === undefined) {
return 0
}
return this.participantSpeakingInformation.totalCountedTime
},
},
watch: {
phoneCallStatus(value) {
if (!value || !(value === 'ringing' || value === 'accepted')) {
this.disabled = false
Expand Down Expand Up @@ -1012,20 +1005,6 @@ export default {
}
},
computeElapsedTime() {
if (!this.participantSpeakingInformation) {
return null
}
const { speaking, lastTimestamp, totalCountedTime } = this.participantSpeakingInformation
if (!speaking) {
this.timeSpeaking = totalCountedTime
} else {
this.timeSpeaking = Date.now() - lastTimestamp + totalCountedTime
}
},
async dialOutPhoneNumber() {
try {
this.disabled = true
Expand Down
63 changes: 56 additions & 7 deletions src/store/participantsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const state = {
* when quickly switching to a new conversation.
*/
cancelFetchParticipants: null,
speakingInterval: null,
}

const getters = {
Expand Down Expand Up @@ -415,25 +416,59 @@ const mutations = {
* @param {boolean} data.speaking - whether the participant is speaking or not
*/
setSpeaking(state, { token, attendeeId, speaking }) {
// create a dummy object for current call
if (!state.speaking[token]) {
Vue.set(state.speaking, token, {})
}

const currentTimestamp = Date.now()
if (!state.speaking[token][attendeeId]) {
Vue.set(state.speaking[token], attendeeId, { speaking: null, lastTimestamp: 0, totalCountedTime: 0 })
Vue.set(state.speaking[token], attendeeId, { speaking, lastTimestamp: currentTimestamp, totalCountedTime: 0 })
return
}

const currentTimestamp = Date.now()
const currentSpeakingState = state.speaking[token][attendeeId].speaking
if (currentSpeakingState === speaking) {
return
}

if (!currentSpeakingState && speaking) {
state.speaking[token][attendeeId].speaking = true
// false -> true, speaking has started, update last timestamp
state.speaking[token][attendeeId].lastTimestamp = currentTimestamp
} else if (currentSpeakingState && !speaking) {
// when speaking has stopped, update the total talking time
state.speaking[token][attendeeId].speaking = false
// true -> false, speaking has stopped, update total counted time
state.speaking[token][attendeeId].totalCountedTime += (currentTimestamp - state.speaking[token][attendeeId].lastTimestamp)
}
state.speaking[token][attendeeId].speaking = speaking
},

/**
* Sets the speaking interval of a participant in a conversation / call.
*
* @param {object} state - current store state.
* @param {number} interval - interval id for participant update
*/
setSpeakingInterval(state, interval) {
Vue.set(state, 'speakingInterval', interval)
},

/**
* Update the total time of speaking for a participant.
*
* @param {object} state - current store state.
* @param {string} token - the conversation token participant is speaking in.
*/
updateTimeSpeaking(state, token) {
if (!state.speaking[token] || !state.speakingInterval) {
return
}

const currentTimestamp = Date.now()
for (const attendeeId in state.speaking[token]) {
if (state.speaking[token][attendeeId].speaking) {
state.speaking[token][attendeeId].totalCountedTime += (currentTimestamp - state.speaking[token][attendeeId].lastTimestamp)
state.speaking[token][attendeeId].lastTimestamp = currentTimestamp
}
}
},

/**
Expand All @@ -445,7 +480,14 @@ const mutations = {
* @param {string} data.token - the conversation token.
*/
purgeSpeakingStore(state, { token }) {
Vue.delete(state.speaking, token)
if (state.speaking[token]) {
Vue.delete(state.speaking, token)
}

if (state.speakingInterval) {
clearInterval(state.speakingInterval)
Vue.set(state, 'speakingInterval', null)
}
},

/**
Expand Down Expand Up @@ -1038,6 +1080,13 @@ const actions = {

setSpeaking(context, { token, attendeeId, speaking }) {
context.commit('setSpeaking', { token, attendeeId, speaking })

if (!context.state.speakingInterval) {
const interval = setInterval(() => {
context.commit('updateTimeSpeaking', token)
}, 1000)
context.commit('setSpeakingInterval', interval)
}
},

purgeSpeakingStore(context, { token }) {
Expand Down

0 comments on commit 39868a1

Please sign in to comment.