Change the user session SocketIO Logic

This commit is contained in:
Patrick Jentsch
2022-07-04 14:09:17 +02:00
parent b8bf004684
commit 76924956de
20 changed files with 200 additions and 99 deletions

View File

@ -1,21 +1,30 @@
class App {
constructor() {
this.data = {users: {}};
this.eventListeners = {'users.patch': []};
this.promises = {users: {}};
this.socket = io({transports: ['websocket'], upgrade: false});
this.socket.on('users.patch', patch => this.usersPatchHandler(patch));
this.socket.on('PATCH', (patch) => {this.data = jsonpatch.applyPatch(this.data, patch).newDocument;});
}
get users() {
return this.data.users;
}
addEventListener(type, listener) {
if (!(type in this.eventListeners)) {
throw `Unknown event type: ${type}`;
subscribeUser(userId) {
if (userId in this.promises.users) {
return this.promises.users[userId];
}
this.eventListeners[type].push(listener);
this.promises.users[userId] = new Promise((resolve, reject) => {
this.socket.emit('SUBSCRIBE /users/<user_id>', userId, response => {
if (response.code === 200) {
this.data.users[userId] = response.payload;
resolve(this.data.users[userId]);
} else {
reject(response);
}
});
});
return this.promises.users[userId];
}
flash(message, category) {
@ -50,29 +59,4 @@ class App {
toastCloseActionElement = toast.el.querySelector('.toast-action[data-action="close"]');
toastCloseActionElement.addEventListener('click', () => {toast.dismiss();});
}
getUserById(userId) {
if (userId in this.promises.users) {
return this.promises.users[userId];
}
this.promises.users[userId] = new Promise((resolve, reject) => {
this.socket.emit('users.user.get', userId, response => {
if (response.code === 200) {
this.data.users[userId] = response.payload;
resolve(this.data.users[userId]);
} else {
reject(response);
}
});
});
return this.promises.users[userId];
}
usersPatchHandler(patch) {
let listener;
this.data = jsonpatch.applyPatch(this.data, patch).newDocument;
//this.data = jsonpatch.apply_patch(this.data, patch);
for (listener of this.eventListeners['users.patch']) {listener(patch);}
}
}

View File

@ -1,16 +1,17 @@
class JobStatusNotifier {
constructor(userId) {
this.userId = userId;
app.socket.on('PATCH', (patch) => {this.onPATCH(patch);});
}
usersPatchHandler(patch) {
onPATCH(patch) {
let filteredPatch;
let jobId;
let match;
let operation;
let re;
re = new RegExp(`^/users/${this.userId}/jobs/([A-Za-z0-9]*)/status$`)
re = new RegExp(`^/users/${this.userId}/jobs/([A-Za-z0-9]*)/status$`);
filteredPatch = patch
.filter(operation => operation.op === 'replace')
.filter(operation => re.test(operation.path));

View File

@ -16,7 +16,7 @@ class CorpusDisplay extends RessourceDisplay {
this.setNumTokens(corpus.num_tokens);
}
usersPatchHandler(patch) {
onPATCH(patch) {
let filteredPatch;
let operation;
let re;

View File

@ -18,7 +18,7 @@ class JobDisplay extends RessourceDisplay {
this.setTitle(job.title);
}
usersPatchHandler(patch) {
onPATCH(patch) {
let filteredPatch;
let operation;
let re;

View File

@ -2,13 +2,13 @@ class RessourceDisplay {
constructor(displayElement) {
this.displayElement = displayElement;
this.userId = this.displayElement.dataset.userId;
app.addEventListener('users.patch', patch => this.usersPatchHandler(patch));
app.getUserById(this.userId).then(user => this.init(user));
app.socket.on('PATCH', (patch) => {this.onPATCH(patch);});
app.subscribeUser(this.userId).then((user) => {this.init(user);});
}
init(user) {throw 'Not implemented';}
usersPatchHandler(patch) {throw 'Not implemented';}
onPATCH(patch) {throw 'Not implemented';}
setElement(element, value) {
switch (element.tagName) {

View File

@ -96,7 +96,7 @@ class CorpusFileList extends RessourceList {
}
}
usersPatchHandler(patch) {
onPATCH(patch) {
let corpusFileId;
let filteredPatch;
let match;

View File

@ -88,7 +88,7 @@ class CorpusList extends RessourceList {
}
}
usersPatchHandler(patch) {
onPATCH(patch) {
let corpusId;
let filteredPatch;
let match;

View File

@ -54,5 +54,5 @@ class JobInputList extends RessourceList {
}
}
usersPatchHandler(patch) {return;}
onPATCH(patch) {return;}
}

View File

@ -94,7 +94,7 @@ class JobList extends RessourceList {
}
}
usersPatchHandler(patch) {
onPATCH(patch) {
let filteredPatch;
let jobId;
let match;

View File

@ -57,7 +57,7 @@ class JobResultList extends RessourceList {
}
}
usersPatchHandler(patch) {
onPATCH(patch) {
let filteredPatch;
let operation;
let re;

View File

@ -89,7 +89,7 @@ class QueryResultList extends RessourceList {
}
}
usersPatchHandler(patch) {
onPATCH(patch) {
let filteredPatch;
let match;
let operation;

View File

@ -91,10 +91,10 @@ class RessourceList {
this.userId = this.listjs.listContainer.dataset.userId;
this.listjs.list.addEventListener('click', event => this.onclick(event));
if (this.userId) {
app.addEventListener('users.patch', patch => this.usersPatchHandler(patch));
app.getUserById(this.userId).then(
user => this.init(user),
error => {throw JSON.stringify(error);}
app.socket.on('PATCH', (patch) => {this.onPATCH(patch);});
app.subscribeUser(this.userId).then(
(user) => {this.init(user);},
(error) => {throw JSON.stringify(error);}
);
}
}
@ -117,7 +117,7 @@ class RessourceList {
onclick(event) {throw 'Not implemented';}
usersPatchHandler(patch) {throw 'Not implemented';}
onPATCH(patch) {throw 'Not implemented';}
add(ressources) {
let values = Array.isArray(ressources) ? ressources : [ressources];