Skip to content

Commit 2614199

Browse files
authored
[rules] runRule: Pass args as Java HashMap instead of JS object (#528)
When rule A calls rule B through `rules.runRule` and passes arguments, rule B executes in its own, dedicated thread. This causes an illegal multi-threaded access to the args object, so instead of passing the args object directly, construct a Java HashMap from it. Related to openhab/openhab-core#5069. Signed-off-by: Florian Hotze <dev@florianhotze.com>
1 parent 39689c5 commit 2614199

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/rules/rules.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
const SCRIPT_TYPE = 'application/javascript';
7070
const GENERATED_RULE_ITEM_TAG = 'GENERATED_RULE_ITEM';
7171

72+
const HashMap = Java.type('java.util.HashMap');
73+
7274
const items = require('../items/items');
7375
const { randomUUID, jsArrayToJavaSet } = require('../utils');
7476
const log = require('../log')('rules');
@@ -179,11 +181,11 @@ function removeRule (uid) {
179181
*
180182
* @memberof rules
181183
* @param {string} uid the UID of the rule to run
182-
* @param {object} [args={}] args optional dict of data to pass to the called rule
183-
* @param {boolean} [cond=true] when true, the called rule will only run if it's conditions are met
184+
* @param {Record<string, unknown>} [args={}] args optional dict of data to pass to the called rule
185+
* @param {boolean} [conditions=true] when true, the called rule will only run if it's conditions are met
184186
* @throws {Error} throws an error if the rule does not exist or is not initialized.
185187
*/
186-
function runRule (uid, args = {}, cond = true) {
188+
function runRule (uid, args = {}, conditions = true) {
187189
const status = ruleManager.getStatus(uid);
188190
if (!status) {
189191
throw Error('There is no rule with UID ' + uid);
@@ -192,7 +194,7 @@ function runRule (uid, args = {}, cond = true) {
192194
throw Error('Rule ' + uid + ' is UNINITIALIZED');
193195
}
194196

195-
ruleManager.runNow(uid, cond, args);
197+
ruleManager.runNow(uid, conditions, new HashMap(args));
196198
}
197199

198200
/**

types/rules/rules.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ export function removeRule(uid: string): boolean;
168168
*
169169
* @memberof rules
170170
* @param {string} uid the UID of the rule to run
171-
* @param {object} [args={}] args optional dict of data to pass to the called rule
172-
* @param {boolean} [cond=true] when true, the called rule will only run if it's conditions are met
171+
* @param {Record<string, unknown>} [args={}] args optional dict of data to pass to the called rule
172+
* @param {boolean} [conditions=true] when true, the called rule will only run if it's conditions are met
173173
* @throws {Error} throws an error if the rule does not exist or is not initialized.
174174
*/
175-
export function runRule(uid: string, args?: object, cond?: boolean): void;
175+
export function runRule(uid: string, args?: Record<string, unknown>, conditions?: boolean): void;
176176
/**
177177
* Tests to see if the rule with the given UID is enabled or disabled. Throws
178178
* and error if the rule doesn't exist.

types/rules/rules.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)