openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

[actions] Don't deprecate `ScriptExecution.createTimer` #171

Closed florian-h05 closed 1 year ago

florian-h05 commented 1 year ago

Reference https://community.openhab.org/t/js-scripting-why-are-we-deprecated-createtimer/140748. Refercence https://github.com/openhab/openhab-addons/pull/13695.

Description

This PR reverts the deprecation of createTimer from previous PR https://github.com/openhab/openhab-js/pull/169.

As discussed on the forum https://community.openhab.org/t/js-scripting-why-are-we-deprecated-createtimer/140748, the setTimeout and setInterval methods do not provide the functionality that advanced users need, and to avoid that those users and users coming from Rules DSL leave JS Scripting because of a missing advanced timer creation API, createTimer is reimplemented to be thread-safe in https://github.com/openhab/openhab-addons/pull/13695.

createTimerWithArgument is still deprecated, as there is no use case for it in JS (see https://github.com/openhab/openhab-addons/pull/13695#discussion_r1020755441).

This PR is backward compatible and does not require the addon to be on the latest add-on version with the thread-safe reimplementation of createTimer, but keep in mind that if not, this can lead to multithread errors.

Testing

Use the following file-based script:

const { actions, time } = require('openhab');

const now = time.toZDT();

// Note that all timers are scheduled to nearly the same time, so without the synchronization mechanism in place, this would lead to a multi-thread exception
actions.ScriptExecution.createTimer(now, () => {
    console.info("Hello world from createTimer without identifier")
});

actions.ScriptExecution.createTimer('createTimer', now, () => {
    console.info("Hello world from createTimer with identifier")
});

let arg1 = 'test';
actions.ScriptExecution.createTimerWithArgument(now, arg1, (newArg) => {
    console.info(`Hello world from createTimerWithArgument without identifier and argument: ${newArg}`);
});

actions.ScriptExecution.createTimerWithArgument('createTimerWithArgument', now, arg1, (newArg) => {
    console.info(`Hello world from createTimerWithArgument with identifier and argument: ${newArg}`);
});
florian-h05 commented 1 year ago

@rkoshak Could you possibly review at least the README changes?