Change event names

This commit is contained in:
Patrick Jentsch 2020-01-28 10:08:09 +01:00
parent 4b0e9392a7
commit f1ab50b283
3 changed files with 24 additions and 29 deletions

View File

@ -35,47 +35,42 @@ def disconnect():
connected_sessions.remove(request.sid) connected_sessions.remove(request.sid)
@socketio.on('subscribe_user_ressources') @socketio.on('user_ressources_init')
@login_required @login_required
def subscribe_user_ressources(): def subscribe_user_ressources():
socketio.start_background_task(user_ressource_subscription_handler, socketio.start_background_task(user_ressource_session_handler,
current_app._get_current_object(), current_app._get_current_object(),
current_user.id, request.sid) current_user.id, request.sid)
@socketio.on('subscribe_foreign_user_ressources') @socketio.on('foreign_user_ressources_init')
@login_required @login_required
@admin_required @admin_required
def subscribe_foreign_user_ressources(user_id): def subscribe_foreign_user_ressources(user_id):
socketio.start_background_task(user_ressource_subscription_handler, socketio.start_background_task(user_ressource_session_handler,
current_app._get_current_object(), current_app._get_current_object(),
user_id, request.sid, True) user_id, request.sid, True)
def user_ressource_subscription_handler(app, user_id, session_id, def user_ressource_session_handler(app, user_id, session_id, foreign=False):
foreign=False):
''' '''
' Sends initial corpus and job lists to the client. Afterwards it checks ' Sends initial corpus and job lists to the client. Afterwards it checks
' every 3 seconds if changes to the initial values appeared. If changes are ' every 3 seconds if changes to the initial values appeared. If changes are
' detected, a RFC 6902 compliant JSON patch gets send. ' detected, a RFC 6902 compliant JSON patch gets send.
' '
' NOTE: The initial values are send as a init-* events. ' NOTE: The initial values are send as a init events.
' The JSON patches are send as update-* events. ' The JSON patches are send as update events.
' > where '*' is either 'corpora' or 'jobs'
''' '''
init_events = {'corpora': 'init-foreign-corpora' if foreign init_events = {'corpora': 'foreign_corpora_init' if foreign
else 'init-corpora', else 'corpora_init',
'jobs': 'init-foreign-jobs' if foreign else 'init-jobs'} 'jobs': 'foreign_jobs_init' if foreign else 'jobs_init'}
update_events = {'corpora': 'update-foreign-corpora' if foreign update_events = {'corpora': 'foreign_corpora_update' if foreign
else 'update-corpora', else 'corpora_update',
'jobs': 'update-foreign-jobs' if foreign 'jobs': 'foreign_jobs_update' if foreign
else 'update-jobs'} else 'jobs_update'}
with app.app_context(): with app.app_context():
# Gather current values from database. # Gather current values from database.
user = User.query.get(user_id) user = User.query.get(user_id)
if user is None:
''' TODO: Handle this '''
return
corpora = {corpus.id: corpus.to_dict() for corpus in user.corpora} corpora = {corpus.id: corpus.to_dict() for corpus in user.corpora}
jobs = {job.id: job.to_dict() for job in user.jobs} jobs = {job.id: job.to_dict() for job in user.jobs}
# Send initial values to the user. # Send initial values to the user.

View File

@ -108,19 +108,19 @@ nopaque["toast"] = function(message, color="") {
// socket event handlers // socket event handlers
nopaque.socket.on('init-corpora', function(msg) { nopaque.socket.on('corpora_init', function(msg) {
nopaque.corpora = JSON.parse(msg); nopaque.corpora = JSON.parse(msg);
for (let subscriber of nopaque.corporaSubscribers) {subscriber._init(nopaque.corpora);} for (let subscriber of nopaque.corporaSubscribers) {subscriber._init(nopaque.corpora);}
}); });
nopaque.socket.on('init-jobs', function(msg) { nopaque.socket.on('jobs_init', function(msg) {
nopaque.jobs = JSON.parse(msg); nopaque.jobs = JSON.parse(msg);
for (let subscriber of nopaque.jobsSubscribers) {subscriber._init(nopaque.jobs);} for (let subscriber of nopaque.jobsSubscribers) {subscriber._init(nopaque.jobs);}
}); });
nopaque.socket.on('update-corpora', function(msg) { nopaque.socket.on('corpora_update', function(msg) {
var patch; var patch;
patch = JSON.parse(msg); patch = JSON.parse(msg);
@ -129,7 +129,7 @@ nopaque.socket.on('update-corpora', function(msg) {
}); });
nopaque.socket.on('update-jobs', function(msg) { nopaque.socket.on('jobs_update', function(msg) {
var patch; var patch;
patch = JSON.parse(msg); patch = JSON.parse(msg);
@ -138,19 +138,19 @@ nopaque.socket.on('update-jobs', function(msg) {
}); });
nopaque.socket.on('init-foreign-corpora', function(msg) { nopaque.socket.on('foreign_corpora_init', function(msg) {
nopaque.foreignCorpora = JSON.parse(msg); nopaque.foreignCorpora = JSON.parse(msg);
for (let subscriber of nopaque.foreignCorporaSubscribers) {subscriber._init(nopaque.foreignCorpora);} for (let subscriber of nopaque.foreignCorporaSubscribers) {subscriber._init(nopaque.foreignCorpora);}
}); });
nopaque.socket.on('init-foreign-jobs', function(msg) { nopaque.socket.on('foreign_jobs_init', function(msg) {
nopaque.foreignJobs = JSON.parse(msg); nopaque.foreignJobs = JSON.parse(msg);
for (let subscriber of nopaque.foreignJobsSubscribers) {subscriber._init(nopaque.foreignJobs);} for (let subscriber of nopaque.foreignJobsSubscribers) {subscriber._init(nopaque.foreignJobs);}
}); });
nopaque.socket.on('update-foreign-corpora', function(msg) { nopaque.socket.on('foreign_corpora_update', function(msg) {
var patch; var patch;
patch = JSON.parse(msg); patch = JSON.parse(msg);
@ -159,7 +159,7 @@ nopaque.socket.on('update-foreign-corpora', function(msg) {
}); });
nopaque.socket.on('update-foreign-jobs', function(msg) { nopaque.socket.on('foreign_jobs_update', function(msg) {
var patch; var patch;
patch = JSON.parse(msg); patch = JSON.parse(msg);
@ -175,5 +175,5 @@ document.addEventListener("DOMContentLoaded", function() {
{"alignment": "right", "constrainWidth": false, "coverTrigger": false}); {"alignment": "right", "constrainWidth": false, "coverTrigger": false});
nopaque.forms.init(); nopaque.forms.init();
nopaque.navigation.init(); nopaque.navigation.init();
nopaque.socket.emit("subscribe_user_ressources"); nopaque.socket.emit("user_ressources_init");
}); });

View File

@ -102,6 +102,6 @@
<script> <script>
var corpusList = new CorpusList("corpora", nopaque.foreignCorporaSubscribers); var corpusList = new CorpusList("corpora", nopaque.foreignCorporaSubscribers);
var jobList = new JobList("jobs", nopaque.foreignJobsSubscribers); var jobList = new JobList("jobs", nopaque.foreignJobsSubscribers);
nopaque.socket.emit("subscribe_foreign_user_ressources", {{ user.id }}); nopaque.socket.emit("foreign_user_ressources_init", {{ user.id }});
</script> </script>
{% endblock %} {% endblock %}