Skip to content

Commit

Permalink
refactor revivers (#690)
Browse files Browse the repository at this point in the history
* refactor message handler to easily add revivers, e.g. mathjs.reviver

* fix catch of messagehandler

* move reviver-specific definitions into Genie.Revivers
  • Loading branch information
hhaensel authored Nov 18, 2023
1 parent ec5c1ee commit ab7e8ef
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
35 changes: 27 additions & 8 deletions assets/js/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ function newSocketConnection(host = Genie.Settings.websockets_exposed_host) {
Genie.WebChannels.initialize();
Genie.WebChannels.socket = newSocketConnection();

// --------------- Revivers ---------------

Genie.Revivers = {};
Genie.Revivers.pipeRevivers = (revivers) => (key, value) => revivers.reduce((v, f) => f(key, v), value);

Genie.Revivers.rebuildReviver = function() {
Genie.Revivers.reviver = Genie.Revivers.pipeRevivers(Genie.Revivers.revivers)
}

Genie.Revivers.addReviver = function(reviver) {
Genie.Revivers.revivers.push(reviver)
Genie.Revivers.rebuildReviver()
}

Genie.Revivers.revive_undefined = function(key, value) {
if (value == '__undefined__') {
return undefined;
} else {
return value;
}
}

Genie.Revivers.revivers = [Genie.Revivers.revive_undefined]
Genie.Revivers.rebuildReviver()

window.addEventListener('beforeunload', _ => {
if (isDev()) {
console.info('Preparing to unload');
Expand Down Expand Up @@ -149,13 +174,7 @@ Genie.WebChannels.messageHandlers.push(event => {
}

if (ed.startsWith('{') && ed.endsWith('}')) {
window.parse_payload(JSON.parse(ed, function (key, value) {
if (value == '__undefined__') {
return undefined;
} else {
return value;
}
}));
window.parse_payload(JSON.parse(ed, Genie.Revivers.reviver));
} else if (ed.startsWith(Genie.Settings.webchannels_eval_command)) {
return Function('"use strict";return (' + ed.substring(Genie.Settings.webchannels_eval_command.length).trim() + ')')();
} else if (ed == 'Subscription: OK') {
Expand All @@ -165,7 +184,7 @@ Genie.WebChannels.messageHandlers.push(event => {
}
} catch (ex) {
console.error(ex);
console.error(ed);
console.error(event.data);
}
});

Expand Down
32 changes: 25 additions & 7 deletions assets/js/webthreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,30 @@ function push(body, headers = {}) {
pull();
}


Genie.Revivers = {};
Genie.Revivers.pipeRevivers = (revivers) => (key, value) => revivers.reduce((v, f) => f(key, v), value);

Genie.Revivers.rebuildReviver = function() {
Genie.Revivers.reviver = Genie.Revivers.pipeRevivers(Genie.Revivers.revivers)
}

Genie.Revivers.addReviver = function(reviver) {
Genie.Revivers.revivers.push(reviver)
Genie.Revivers.rebuildReviver()
}

Genie.Revivers.revive_undefined = function(key, value) {
if (value == '__undefined__') {
return undefined;
} else {
return value;
}
}

Genie.Revivers.revivers = [Genie.Revivers.revive_undefined]
Genie.Revivers.rebuildReviver()

Genie.WebChannels.processingHandlers.push(function(json_data){
window.parse_payload(json_data);
});
Expand Down Expand Up @@ -166,13 +190,7 @@ function processMessage(message) {
} else if (message == 'Subscription: OK') {
window.subscription_ready();
} else if (message.startsWith('{') && message.endsWith('}')) {
window.parse_payload(JSON.parse(message, function (key, value) {
if (value == '__undefined__') {
return undefined;
} else {
return value;
}
}));
window.parse_payload(JSON.parse(message, Genie.Revivers.reviver));
} else {
window.process_payload(message);
}
Expand Down

2 comments on commit ab7e8ef

@essenciary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Version 5.22.0 already exists

Please sign in to comment.