From 482b7bba069187d8bc4488c094ad68f9b85ea8d9 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 15 May 2018 08:06:43 -0700 Subject: [PATCH] fix: send ID as part of the event name (#571) --- common/interpreter/socket_interpret.js | 27 +++++++++++--------------- public/lib/interpreter.js | 6 +++--- server/routes/socket.js | 14 +++++-------- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/common/interpreter/socket_interpret.js b/common/interpreter/socket_interpret.js index 7be1d50621f597..6ab267a568dfe6 100644 --- a/common/interpreter/socket_interpret.js +++ b/common/interpreter/socket_interpret.js @@ -26,9 +26,9 @@ export function socketInterpreterProvider({ functions, handlers, - onFunctionNotFound: (chain, context) => { + onFunctionNotFound: (ast, context) => { // Get the name of the function that wasn't found - const functionName = chain.chain[0].function; + const functionName = ast.chain[0].function; // Get the list of functions that are known elsewhere return Promise.resolve(referableFunctions).then(referableFunctionMap => { @@ -42,26 +42,21 @@ export function socketInterpreterProvider({ return new Promise((resolve, reject) => { const listener = resp => { - // Resolve or reject the promise once we get our ID back - if (resp.id === id) { - socket.removeListener('resp', listener); - - if (resp.error) { - // cast error strings back into error instances - const err = resp.error instanceof Error ? resp.error : new Error(resp.error); - if (resp.stack) err.stack = resp.stack; - reject(err); - } else { - resolve(resp.value); - } + if (resp.error) { + // cast error strings back into error instances + const err = resp.error instanceof Error ? resp.error : new Error(resp.error); + if (resp.stack) err.stack = resp.stack; + reject(err); + } else { + resolve(resp.value); } }; - socket.on('resp', listener); + socket.once(`resp:${id}`, listener); // Go run the remaining AST and context somewhere else, meaning either the browser or the server, depending on // where this file was loaded - socket.emit('run', { ast: chain, context: context, id: id }); + socket.emit('run', { ast, context, id }); }); }); }, diff --git a/public/lib/interpreter.js b/public/lib/interpreter.js index 974437c753e59c..f509698d37f5e1 100644 --- a/public/lib/interpreter.js +++ b/public/lib/interpreter.js @@ -23,8 +23,8 @@ export function interpretAst(ast, context) { .then(interpretFn => interpretFn(ast, context)); } -socket.on('run', msg => { - interpretAst(msg.ast, msg.context).then(resp => { - socket.emit('resp', { value: resp, id: msg.id }); +socket.on('run', ({ ast, context, id }) => { + interpretAst(ast, context).then(value => { + socket.emit(`resp:${id}`, { value }); }); }); diff --git a/server/routes/socket.js b/server/routes/socket.js index 7de6dfce41e3d9..f1b1ff7c3d566a 100644 --- a/server/routes/socket.js +++ b/server/routes/socket.js @@ -30,7 +30,7 @@ export function socketApi(server) { socket.emit('functionList', functionsRegistry.toJS()); }); - const handler = msg => { + const handler = ({ ast, context, id }) => { Promise.all([getClientFunctions, authHeader]).then(([clientFunctions, authHeader]) => { if (server.plugins.security) request.headers.authorization = authHeader; @@ -42,16 +42,12 @@ export function socketApi(server) { socket: socket, }); - return interpret(msg.ast, msg.context) - .then(resp => { - socket.emit('resp', { - id: msg.id, - value: resp, - }); + return interpret(ast, context) + .then(value => { + socket.emit(`resp:${id}`, { value }); }) .catch(e => { - socket.emit('resp', { - id: msg.id, + socket.emit(`resp:${id}`, { error: e.message, stack: e.stack, });