Make the AppClient a subscriber of current_user jobs

This commit is contained in:
Patrick Jentsch 2021-02-09 13:59:43 +01:00
parent 846ba31023
commit 63ec304263

View File

@ -3,6 +3,20 @@ class AppClient {
this.socket = io({transports: ['websocket']}); this.socket = io({transports: ['websocket']});
this.users = {}; this.users = {};
this.users.self = this.loadUser(currentUserId); this.users.self = this.loadUser(currentUserId);
this.users.self.eventListeners.job.addEventListener((eventType, payload) => this.jobEventHandler(eventType, payload));
}
jobEventHandler(eventType, payload) {
switch (eventType) {
case 'init':
break;
case 'patch':
this.jobPatch(payload);
break;
default:
console.error(`[AppClient.jobEventHandler] Unknown event type: ${eventType}`);
break;
}
} }
loadUser(userId) { loadUser(userId) {
@ -14,8 +28,17 @@ class AppClient {
this.socket.emit('start_user_session', userId); this.socket.emit('start_user_session', userId);
return user; return user;
} }
}
jobPatch(patch) {
if (this.users.self.data.settings.job_status_site_notifications === 'none') {return;}
let jobStatusPatches = patch.filter(operation => operation.op === 'replace' && /^\/jobs\/(\d+)\/status$/.test(operation.path));
for (let operation of jobStatusPatches) {
let [match, jobId] = operation.path.match(/^\/jobs\/(\d+)\/status$/);
if (this.users.self.data.settings.job_status_site_notifications === "end" && !['complete', 'failed'].includes(operation.value)) {continue;}
nopaque.flash(`[<a href="/jobs/${jobId}">${this.users.self.data.jobs[jobId].title}</a>] New status: ${operation.value}`, 'job');
}
}
}
class User { class User {
constructor() { constructor() {
@ -40,8 +63,6 @@ class User {
} }
init(data) { init(data) {
console.log('### User.init ###');
console.log(data);
this.data = data; this.data = data;
for (let [corpusId, eventListeners] of Object.entries(this.eventListeners.corpus)) { for (let [corpusId, eventListeners] of Object.entries(this.eventListeners.corpus)) {
@ -76,8 +97,6 @@ class User {
} }
patch(patch) { patch(patch) {
console.log('### User.patch ###');
console.log(patch);
this.data = jsonpatch.apply_patch(this.data, patch); this.data = jsonpatch.apply_patch(this.data, patch);
let corporaPatch = patch.filter(operation => operation.path.startsWith("/corpora")); let corporaPatch = patch.filter(operation => operation.path.startsWith("/corpora"));
@ -121,16 +140,6 @@ class User {
} }
} }
} }
for (let operation of jobsPatch) {
if (operation.op !== 'replace') {continue;}
// Matches the only path that should be handled here: /jobs/{jobId}/status
if (/^\/jobs\/(\d+)\/status$/.test(operation.path)) {
let [match, jobId] = operation.path.match(/^\/jobs\/(\d+)\/status$/);
if (this.data.settings.job_status_site_notifications === "end" && !['complete', 'failed'].includes(operation.value)) {continue;}
nopaque.flash(`[<a href="/jobs/${jobId}">${this.data.jobs[jobId].title}</a>] New status: ${operation.value}`, 'job');
}
}
} }
} }