Skip to content

Commit

Permalink
Fixed sendto with multiple instances
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Dec 11, 2023
1 parent 19e7d78 commit b9f5860
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Since v5.5.0 of the JavaScript adapter the following locations (relative to the

* (klein0r) Day of week as number always returns 1 (monday) to 7 (sunday)
* (klein0r) Fixed layout of script type selection
* (klein0r) Fixed sendto with multiple instances (for callback / timeout handling)

### 7.4.0 (2023-12-08)

Expand Down
60 changes: 45 additions & 15 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2713,28 +2713,59 @@ function sandBox(script, name, verbose, debug, context) {
try {
callback.call(sandbox, {error: 'timeout'}, options, _adapter);
} catch (e) {
errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
errorInCallback(e);
}
callback = null;
}
}, timeoutDuration);
}

sandbox.verbose && sandbox.log(`sendTo(adapter=${_adapter}, cmd=${cmd}, msg=${JSON.stringify(msg)})`, 'info');
adapter.sendTo(_adapter, cmd, msg, timeout && function (result) {
timeout && clearTimeout(timeout);
// If specific instance
if (_adapter.match(/\.[0-9]+$/)) {
sandbox.verbose && sandbox.log(`sendTo(instance=${_adapter}, cmd=${cmd}, msg=${JSON.stringify(msg)}, hasCallback=${typeof callback === 'function'})`, 'info');
adapter.sendTo(_adapter, cmd, msg, timeout && function (result) {
timeout && clearTimeout(timeout);

sandbox.verbose && result && sandbox.log(`sendTo => ${JSON.stringify(result)}`, 'debug');
sandbox.verbose && result && sandbox.log(`sendTo => ${JSON.stringify(result)}`, 'debug');

if (typeof callback === 'function') {
try {
callback.call(sandbox, result, options, _adapter);
} catch (e) {
errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
if (typeof callback === 'function') {
try {
callback.call(sandbox, result, options, _adapter);
} catch (e) {
errorInCallback(e);
}
callback = null;
}
callback = null;
}
}, options);
}, options);
} else {
// Send to all instances
context.adapter.getObjectView('system', 'instance', {startkey: `system.adapter.${_adapter}.`, endkey: `system.adapter.${_adapter}.\u9999`}, options, (err, res) => {
if (err || !res) {
sandbox.log(`sendTo failed: ${err.message}`, 'error');
return;
}

const instances = res.rows.map(item => item.id.substring('system.adapter.'.length));

instances.forEach(instance => {
sandbox.verbose && sandbox.log(`sendTo(instance=${instance}, cmd=${cmd}, msg=${JSON.stringify(msg)}, hasCallback=${typeof callback === 'function'})`, 'info');
adapter.sendTo(instance, cmd, msg, timeout && function (result) {
timeout && clearTimeout(timeout);

sandbox.verbose && result && sandbox.log(`sendTo => ${JSON.stringify(result)}`, 'debug');

if (typeof callback === 'function') {
try {
callback.call(sandbox, result, options, instance);
} catch (e) {
errorInCallback(e);
}
callback = null;
}
}, options);
});
});
}
},
sendto: function (_adapter, cmd, msg, callback) {
return sandbox.sendTo(_adapter, cmd, msg, callback);
Expand Down Expand Up @@ -3324,9 +3355,8 @@ function sandBox(script, name, verbose, debug, context) {
}
return;
}
let timeout = setTimeout(() => {
timeout = null;

const timeout = setTimeout(() => {
sandbox.verbose && sandbox.log('getHistory => timeout', 'debug');

if (typeof callback === 'function') {
Expand Down

0 comments on commit b9f5860

Please sign in to comment.