Skip to content

Commit

Permalink
don't discard msg, clone if needed #96
Browse files Browse the repository at this point in the history
  • Loading branch information
586837r committed Feb 7, 2020
1 parent e91ce67 commit 635db65
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
28 changes: 19 additions & 9 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,25 @@ function assignEvalTypedStruct(RED, node, msg, dest, keys) {
...[...arguments].slice(3));
}

function nodeSendMultiple(node, msgs, outputs = 1) {
const count = msgs.length;
const successCount = msgs.filter(m => m).length;
function nodeSendMultiple(RED, node, inputMsg, prototypes, outputs = 1) {
const count = prototypes.length;
const successCount = prototypes.filter(m => m).length;

// if there are too many msgs put them in the last output payload
if(msgs.length > outputs) {
const result = msgs.slice(0, outputs);
const last = msgs.slice(outputs-1).filter(x => x);
if(prototypes.length > outputs) {
const result = prototypes.slice(0, outputs);
const last = prototypes.slice(outputs-1).filter(x => x);
result[outputs-1] = { payload: last };
msgs = result;
prototypes = result;
}

const msgs = prototypes.map(proto => {
const msg = RED.util.cloneMessage(inputMsg);
for(const [k,v] of Object.entries(proto)) {
msg[k] = v;
}
return msg;
});

node.status({
shape: 'dot',
Expand Down Expand Up @@ -351,14 +359,16 @@ function nodeSend(node, msg, value, statusText = 'success') {
}

function nodeWarn(node, error) {
if(typeof error !== 'object') error = new Error(String(error));
if(error == null) console.trace("WHAT THE FUCK WARN");
if(typeof error !== 'object' || error == null) error = new Error(String(error));
node.status({shape: 'dot', fill: 'yellow', text: ellipse(error.message || String(error), 32)});
// node.warn(error.stack || error.message || String(error));
node.warn(error.message || String(error));
}

function nodeError(node, error, value, statusText) {
if(typeof error !== 'object') error = new Error(String(error));
if(error == null) console.trace("WHAT THE FUCK ERROR");
if(typeof error !== 'object' || error == null) error = new Error(String(error));
node.status({ shape: 'dot', fill: 'red', text: ellipse(statusText || error.stack || error.message || String(error), 32)});
// node.error(error.stack || error.message || String(error), {error: error, value: error.value || value});
node.error(error.message || String(error), {error: error, value: error.value || value});
Expand Down
3 changes: 1 addition & 2 deletions nodes/alexa-remote-echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ module.exports = function (RED) {
if(!tools.nodeSetup(this, input, true)) return;

this.on('input', function (msg) {
// TODO: change {} to msg, caution! errors!
const send = tools.nodeGetSendCb(this, {});
const send = tools.nodeGetSendCb(this, msg);
const error = tools.nodeGetErrorCb(this);
if(this.account.state.code !== 'READY') return error('account not initialised');
this.status({ shape: 'dot', fill: 'grey', text: 'sending' });
Expand Down
2 changes: 1 addition & 1 deletion nodes/alexa-remote-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function (RED) {
if(!tools.nodeSetup(this, input, true)) return;

this.on('input', function (msg) {
const send = tools.nodeGetSendCb(this, {});
const send = tools.nodeGetSendCb(this, msg);
const error = tools.nodeGetErrorCb(this);
if(this.account.state.code !== 'READY') return error('account not initialised');
this.status({ shape: 'dot', fill: 'grey', text: 'sending' });
Expand Down
3 changes: 1 addition & 2 deletions nodes/alexa-remote-other.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ module.exports = function (RED) {
if(!tools.nodeSetup(this, input, true)) return;

this.on('input', function (msg) {
// TODO: change {} to msg, caution! errors!
const send = tools.nodeGetSendCb(this, {});
const send = tools.nodeGetSendCb(this, msg);
const error = tools.nodeGetErrorCb(this);
if(this.account.state.code !== 'READY') return error('account not initialised');
this.status({ shape: 'dot', fill: 'grey', text: 'sending' });
Expand Down
11 changes: 5 additions & 6 deletions nodes/alexa-remote-smarthome.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ module.exports = function (RED) {
if(!tools.nodeSetup(this, input, true)) return;

this.on('input', function (msg) {
// TODO: change {} to msg, caution! errors!
const send = tools.nodeGetSendCb(this, {});
const send = tools.nodeGetSendCb(this, msg);
const error = tools.nodeGetErrorCb(this);
if(this.account.state.code !== 'READY') return error('Account not initialised!');
this.status({ shape: 'dot', fill: 'grey', text: 'sending' });
Expand All @@ -35,7 +34,7 @@ module.exports = function (RED) {
case 'query': {
if(!Array.isArray(value)) return invalid();

// i don't know either
// i don't know either (that's what amazon expects...)
const getIdForQuery = (entity) => entity.type === 'APPLIANCE' ? entity.applianceId : entity.entityId;

const queries = value.length === 0 ? msg.payload : value;
Expand Down Expand Up @@ -109,7 +108,7 @@ module.exports = function (RED) {
if(!state) {
if(reportErrors) {
const errorObj = errorById.get(id) || { message: `no response for smarthome entity "${entity.name}" (${id})!`};
error(errorObj.message, errorObj);
error(errorObj.message || errorObj.code || JSON.stringify(errorObj), errorObj);
}
return null;
}
Expand Down Expand Up @@ -145,7 +144,7 @@ module.exports = function (RED) {
}

const msgs = queries.map((query,i) => mapQueryToMsg(entities[i], query));
tools.nodeSendMultiple(this, msgs, this.outputs);
tools.nodeSendMultiple(RED, this, msg, msgs, this.outputs);
}).catch(error);
}
case 'action': {
Expand Down Expand Up @@ -257,7 +256,7 @@ module.exports = function (RED) {

return controlResponse;
});
tools.nodeSendMultiple(this, msgs, this.outputs);
tools.nodeSendMultiple(RED, this, msg, msgs, this.outputs);
}).catch(error);
}
case 'discover': {
Expand Down

0 comments on commit 635db65

Please sign in to comment.