Skip to content

Commit

Permalink
fix: realtime updates failing in lists
Browse files Browse the repository at this point in the history
- 2 components triggering unsubscriptions to the same event causing data to be outdated in atleast one component view

- use composable to subscribe & add listeners for list updates

- maintain subscribed doctypes in a reactive object so that doctype subscription event is emitted only once

- remove doctype unsubscribe on onBeforeUnmount since ionic doesn't follow the usual component lifecycle - https://ionicframework.com/docs/vue/lifecycle & these ionic events are only available on IonPage and not the child components

(cherry picked from commit 046b6c1)
  • Loading branch information
ruchamahabal authored and mergify[bot] committed Dec 21, 2023
1 parent 3ce285a commit 7ccae18
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
13 changes: 2 additions & 11 deletions frontend/src/components/ListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ import {
computed,
reactive,
onMounted,
onBeforeUnmount,
} from "vue"
import {
modalController,
Expand All @@ -142,6 +141,7 @@ import ListFiltersActionSheet from "@/components/ListFiltersActionSheet.vue"
import CustomIonModal from "@/components/CustomIonModal.vue"
import useWorkflow from "@/composables/workflow"
import { useListUpdate } from "@/composables/realtime"
const props = defineProps({
doctype: {
Expand Down Expand Up @@ -352,17 +352,8 @@ onMounted(async () => {
const workflow = useWorkflow(props.doctype)
await workflow.workflowDoc.promise
workflowStateField.value = workflow.getWorkflowStateField()
fetchDocumentList()
socket.emit("doctype_subscribe", props.doctype)
socket.on("list_update", (data) => {
if (data?.doctype !== props.doctype) return
fetchDocumentList()
})
})
onBeforeUnmount(() => {
socket.off("list_update")
useListUpdate(socket, props.doctype, () => fetchDocumentList())
})
</script>
23 changes: 5 additions & 18 deletions frontend/src/components/RequestPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</template>

<script setup>
import { ref, inject, onMounted, computed, markRaw, onBeforeUnmount } from "vue"
import { ref, inject, onMounted, computed, markRaw } from "vue"
import TabButtons from "@/components/TabButtons.vue"
import RequestList from "@/components/RequestList.vue"
Expand All @@ -25,6 +25,8 @@ import { myClaims, teamClaims } from "@/data/claims"
import LeaveRequestItem from "@/components/LeaveRequestItem.vue"
import ExpenseClaimItem from "@/components/ExpenseClaimItem.vue"
import { useListUpdate } from "@/composables/realtime"
const activeTab = ref("My Requests")
const socket = inject("$socket")
Expand Down Expand Up @@ -57,22 +59,7 @@ const teamRequests = computed(() => {
})
onMounted(() => {
socket.emit("doctype_subscribe", "Leave Application")
socket.emit("doctype_subscribe", "Expense Claim")
socket.on("list_update", (data) => {
if (data.doctype === "Leave Application") {
myLeaves.reload()
teamLeaves.reload()
} else if (data.doctype === "Expense Claim") {
myClaims.reload()
teamClaims.reload()
}
})
})
onBeforeUnmount(() => {
socket.emit("doctype_unsubscribe", "Leave Application")
socket.emit("doctype_unsubscribe", "Expense Claim")
socket.off("list_update")
useListUpdate(socket, "Leave Application", () => teamLeaves.reload())
useListUpdate(socket, "Expense Claim", () => teamClaims.reload())
})
</script>
19 changes: 19 additions & 0 deletions frontend/src/composables/realtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { reactive } from "vue"

const subscribed = reactive({})

export function useListUpdate(socket, doctype, callback) {
subscribe(socket, doctype)
socket.on("list_update", (data) => {
if (data.doctype == doctype) {
callback(data.name)
}
})
}

function subscribe(socket, doctype) {
if (subscribed[doctype]) return

socket.emit("doctype_subscribe", doctype)
subscribed[doctype] = true
}

0 comments on commit 7ccae18

Please sign in to comment.