Skip to content

Commit a2ab6af

Browse files
authored
Merge pull request #90 from mapswipe/fix/remove-group-and-task-listeners
Fix/remove group and task listeners
2 parents 93e9684 + def91f8 commit a2ab6af

1 file changed

Lines changed: 39 additions & 55 deletions

File tree

src/views/ProjectView.vue

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<script lang="ts">
22
import { defineComponent } from 'vue'
3-
import { goOffline, goOnline, off, onValue, set } from 'firebase/database'
3+
import { get, off, onValue, set } from 'firebase/database'
44
import {
5-
db,
65
getGroupsQuery,
76
getProjectRef,
87
getProjectContributionsRef,
@@ -54,7 +53,6 @@ export default defineComponent({
5453
tutorial: null,
5554
tutorialTasks: null,
5655
to: null,
57-
unsubscribeGroup: null,
5856
}
5957
},
6058
provide() {
@@ -92,7 +90,6 @@ export default defineComponent({
9290
9391
this.completedGroupId = this.group.groupId
9492
95-
goOnline(db)
9693
set(getResultsRef(this.project.projectId, this.group.groupId, this.user.uid), entry)
9794
.then(() => {
9895
this.showSnackbar(
@@ -117,7 +114,7 @@ export default defineComponent({
117114
computed: {
118115
...mapStores(useCurrentUserStore),
119116
options() {
120-
const options = this.project?.customOptions ?? this.project?.answerLabels;
117+
const options = this.project?.customOptions ?? this.project?.answerLabels
121118
const completedOptions = options?.map(this.completeOptions)
122119
return completedOptions
123120
},
@@ -156,37 +153,36 @@ export default defineComponent({
156153
this.projectContributions = data
157154
})
158155
},
159-
bindTaskGroup() {
160-
this.unsubscribeGroup = onValue(getGroupsQuery(this.projectId), (snapshot) => {
161-
const data = snapshot.val() || {}
162-
const flatGroups = Object.values(data).flat()
163-
const completed = Object.keys(this.projectContributions)
164-
const available = flatGroups.filter(
165-
(g) => g.requiredCount > 0 && !completed.includes(g.groupId),
156+
async bindTaskGroup() {
157+
this.tasks = null
158+
const snapshot = await get(getGroupsQuery(this.projectId))
159+
const data = snapshot.val() || {}
160+
const flatGroups = Object.values(data).flat()
161+
const completed = Object.keys(this.projectContributions)
162+
const available = flatGroups.filter(
163+
(g) => g.requiredCount > 0 && !completed.includes(g.groupId),
164+
)
165+
if (!available.length) {
166+
this.nextDialog = false
167+
this.to = this.i18nRoute({ name: 'projects' })
168+
this.showDialog(
169+
this.$t('projectView.noTasksAvailable'),
170+
this.$t('projectView.goBackToProjects', { user: this.user.displayName }),
171+
this.leaveProject,
172+
true,
173+
false,
166174
)
167-
if (!available.length) {
168-
this.nextDialog = false
169-
this.to = this.i18nRoute({ name: 'projects' })
170-
this.showDialog(
171-
this.$t('projectView.noTasksAvailable'),
172-
this.$t('projectView.goBackToProjects', { user: this.user.displayName }),
173-
this.leaveProject,
174-
true,
175-
false,
176-
)
177-
} else {
178-
this.hideDialog()
179-
}
180-
const random = available[Math.floor(Math.random() * available.length)]
181-
this.group = random
182-
this.bindTasks()
183-
})
175+
} else {
176+
this.hideDialog()
177+
}
178+
const random = available[Math.floor(Math.random() * available.length)]
179+
this.group = random
180+
await this.bindTasks()
184181
},
185-
bindTasks() {
186-
onValue(getTasksRef(this.projectId, this.group?.groupId), (snapshot) => {
187-
const data = snapshot.val()
188-
this.tasks = decompressTasks(data)
189-
})
182+
async bindTasks() {
183+
const snapshot = await get(getTasksRef(this.projectId, this.group?.groupId))
184+
const data = snapshot.val()
185+
this.tasks = decompressTasks(data)
190186
},
191187
bindTutorial(tutorialId) {
192188
onValue(getProjectRef(tutorialId), (snapshot) => {
@@ -195,12 +191,11 @@ export default defineComponent({
195191
this.bindTutorialTasks(tutorialId)
196192
})
197193
},
198-
bindTutorialTasks(tutorialId) {
199-
onValue(getTasksRef(tutorialId, '101'), (snapshot) => {
200-
const data = snapshot.val()
201-
this.tutorialTasks = decompressTasks(data)
202-
this.mode = 'contribute'
203-
})
194+
async bindTutorialTasks(tutorialId) {
195+
const snapshot = await get(getTasksRef(tutorialId, '101'))
196+
const data = snapshot.val()
197+
this.tutorialTasks = decompressTasks(data)
198+
this.mode = 'contribute'
204199
},
205200
completeOptions(option, index) {
206201
option.title ??= option.label
@@ -213,13 +208,9 @@ export default defineComponent({
213208
this.matchIcon(option.icon) || option.icon || `mdi-numeric-${option.shortkey}-box`
214209
return option
215210
},
216-
continueMapping() {
211+
async continueMapping() {
217212
this.nextDialog = false
218-
// Disconnect and reconnect to force fresh group data
219-
if (this.unsubscribeGroup) {
220-
this.unsubscribeGroup()
221-
}
222-
this.bindTaskGroup()
213+
await this.bindTaskGroup()
223214
this.mode = 'contribute'
224215
},
225216
i18nRoute,
@@ -233,9 +224,7 @@ export default defineComponent({
233224
if (!this.completedGroupId) {
234225
return true
235226
} else {
236-
const updated =
237-
this.projectContributions[this.completedGroupId] &&
238-
this.completedGroupId !== this.group?.groupId
227+
const updated = Boolean(this.projectContributions?.[this.completedGroupId])
239228
/*
240229
Firebase rejects results that are produced at a speed of less than 125 ms per task. Therefore, we do not wait for
241230
updated projectContributions for speedy results.
@@ -244,9 +233,6 @@ export default defineComponent({
244233
return updated || tooSpeedy
245234
}
246235
},
247-
handleTaskComponentCreated() {
248-
goOffline(db)
249-
},
250236
},
251237
beforeRouteLeave(to, from, next) {
252238
if (this.mode === 'contribute' && to.name !== 'authentication') {
@@ -259,9 +245,7 @@ export default defineComponent({
259245
)
260246
} else {
261247
off(getProjectRef(this.projectId))
262-
off(getGroupsQuery(this.projectId))
263248
off(getProjectContributionsRef(this.user.uid, this.projectId))
264-
goOnline(db)
265249
next()
266250
}
267251
},
@@ -286,7 +270,7 @@ export default defineComponent({
286270
:tasks="tasks"
287271
:tutorial="tutorial"
288272
:tutorialTasks="tutorialTasks"
289-
@created="handleTaskComponentCreated"
273+
:key="group.groupId"
290274
/>
291275

292276
<v-dialog v-model="nextDialog" max-width="600" persistent>

0 commit comments

Comments
 (0)