Skip to content

Commit

Permalink
fix: send ID as part of the event name (elastic#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson authored and Rashid Khan committed May 15, 2018
1 parent f2cf96e commit 482b7bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
27 changes: 11 additions & 16 deletions common/interpreter/socket_interpret.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 });
});
});
},
Expand Down
6 changes: 3 additions & 3 deletions public/lib/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
14 changes: 5 additions & 9 deletions server/routes/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
});
Expand Down

0 comments on commit 482b7bb

Please sign in to comment.