Skip to content

Commit b4e35fc

Browse files
committed
feat: improve reactive subscriptions to fields, improve naming of methods and their interface
1 parent 7e4e946 commit b4e35fc

4 files changed

Lines changed: 148 additions & 41 deletions

File tree

custom/GlobalJobApi.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { useBackgroundJobApi } from './useBackgroundJobApi';
2727
const jobStore = useBackgroundJobApi();
2828
2929
const dialogRef = ref<any>(null);
30+
let unsubscribeJobUpdates: (() => void) | undefined;
3031
3132
const props = defineProps<{
3233
meta: {
@@ -69,7 +70,7 @@ onMounted(() => {
6970
// @ts-ignore
7071
window.OpenJobInfoPopup = openJobInfo;
7172
72-
websocket.subscribe('/background-jobs', (data) => {
73+
unsubscribeJobUpdates = websocket.subscribe('/background-jobs-job-update', (data) => {
7374
if (data.jobId === jobStore.currentJob?.id) {
7475
if (data.status) {
7576
jobStore.updateCurrentJob({ status: data.status });
@@ -90,6 +91,6 @@ onBeforeUnmount(() => {
9091
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
9192
// @ts-ignore
9293
if (window.OpenJobInfoPopup) delete window.OpenJobInfoPopup;
93-
websocket.unsubscribe('/background-jobs');
94+
unsubscribeJobUpdates?.();
9495
});
9596
</script>

custom/JobInfoPopup.vue

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ import { getTimeAgoString, callAdminForthApi, getCustomComponent} from '@/utils'
6969
import { useI18n } from 'vue-i18n';
7070
import StateToIcon from './StateToIcon.vue';
7171
import { useAdminforth } from '@/adminforth';
72-
import { onBeforeUnmount, ref, watch } from 'vue';
72+
import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
7373
import websocket from '@/websocket';
7474
import { useBackgroundJobApi } from './useBackgroundJobApi';
7575
@@ -115,13 +115,11 @@ function createStateFieldSubscription(
115115
callback: (data: any) => void,
116116
) {
117117
const paths = getUniqueFieldNames(fieldNames).map(pathFactory);
118-
for (const path of paths) {
119-
websocket.subscribe(path, callback);
120-
}
118+
const pathCleanups = paths.map((path) => websocket.subscribe(path, callback));
121119
122120
const unsubscribe = () => {
123-
for (const path of paths) {
124-
websocket.unsubscribe(path);
121+
for (const cleanup of pathCleanups) {
122+
cleanup();
125123
}
126124
subscriptionCleanups.delete(unsubscribe);
127125
};
@@ -155,6 +153,50 @@ function handleTaskStateFieldUpdate(data: TaskStateFieldUpdate) {
155153
};
156154
}
157155
156+
function handleTaskStatusUpdate(data: { taskIndex: number; status: string }) {
157+
if (!jobTasks.value[data.taskIndex]) {
158+
return;
159+
}
160+
161+
jobTasks.value[data.taskIndex].status = data.status;
162+
}
163+
164+
function handleJobUpdate(data: {
165+
jobId: string;
166+
status?: IJob['status'];
167+
progress?: string;
168+
finishedAt?: Date;
169+
state?: Record<string, any>;
170+
}) {
171+
if (data.jobId !== props.job.id) {
172+
return;
173+
}
174+
175+
if (data.status) {
176+
props.job.status = data.status;
177+
}
178+
if (data.progress !== undefined) {
179+
props.job.progress = data.progress;
180+
}
181+
if (data.finishedAt) {
182+
props.job.finishedAt = data.finishedAt;
183+
}
184+
if (data.state) {
185+
props.job.state = {
186+
...props.job.state,
187+
...data.state,
188+
};
189+
}
190+
if (jobStore.currentJob?.id === props.job.id) {
191+
jobStore.updateCurrentJob({
192+
status: props.job.status,
193+
progress: props.job.progress,
194+
finishedAt: props.job.finishedAt,
195+
state: props.job.state,
196+
});
197+
}
198+
}
199+
158200
function subscribeToJobStateFields(fieldNames: string[]) {
159201
return createStateFieldSubscription(
160202
fieldNames,
@@ -241,6 +283,13 @@ watch(
241283
{ immediate: true }
242284
);
243285
286+
onMounted(() => {
287+
const taskStatusPath = `/background-jobs-task-update/${props.job.id}`;
288+
subscriptionCleanups.add(websocket.subscribe(taskStatusPath, handleTaskStatusUpdate));
289+
290+
subscriptionCleanups.add(websocket.subscribe('/background-jobs-job-update', handleJobUpdate));
291+
});
292+
244293
onBeforeUnmount(() => {
245294
for (const unsubscribe of Array.from(subscriptionCleanups)) {
246295
unsubscribe();

custom/NavbarJobs.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="cursor-pointer hover:scale-110 transition-transform" @click="isDropdownOpen = !isDropdownOpen">
44
<div class="relative flex items-center justify-center" v-if="jobs.length > 0">
55
<Tooltip>
6-
<IconBriefcaseSolid class="w-7 h-7 text-lightNavbarIcons dark:text-darkNavbarIcons" />
6+
<IconCheckCircleOutline class="w-7 h-7 text-lightNavbarIcons dark:text-darkNavbarIcons" />
77
<template #tooltip>
88
{{ t('All jobs completed') }}
99
</template>
@@ -48,8 +48,8 @@
4848
<script setup lang="ts">
4949
import type { AdminUser } from 'adminforth';
5050
import { onMounted, onUnmounted, ref, computed } from 'vue';
51-
import { IconCheckCircleOutline, IconBriefcaseSolid } from '@iconify-prerendered/vue-flowbite';
5251
import { Tooltip } from '@/afcl';
52+
import { IconCheckCircleOutline } from '@iconify-prerendered/vue-flowbite';
5353
import { useI18n } from 'vue-i18n';
5454
import JobsList from './JobsList.vue';
5555
import type { IJob } from './utils';
@@ -69,6 +69,7 @@
6969
const isDropdownOpen = ref(false);
7070
const jobs = ref<IJob[]>([]);
7171
const dropdownRef = ref<HTMLElement | null>(null);
72+
let unsubscribeJobUpdates: (() => void) | undefined;
7273
7374
onClickOutside(dropdownRef, () => {
7475
isDropdownOpen.value = false;
@@ -85,7 +86,7 @@
8586
8687
8788
onMounted(async () => {
88-
websocket.subscribe('/background-jobs', (data) => {
89+
unsubscribeJobUpdates = websocket.subscribe('/background-jobs-job-update', (data) => {
8990
const jobIndex = jobs.value.findIndex(job => job.id === data.jobId);
9091
if (jobIndex !== -1) {
9192
if (data.status) {
@@ -130,7 +131,7 @@
130131
131132
132133
onUnmounted(() => {
133-
websocket.unsubscribe('/background-jobs');
134+
unsubscribeJobUpdates?.();
134135
});
135136
136137
</script>
@@ -147,4 +148,4 @@
147148
opacity: 0;
148149
}
149150
}
150-
</style>
151+
</style>

0 commit comments

Comments
 (0)