[jsscripting] Name timers created by polyfills (#13576)

* [jsscripting] Name timers created by polyfills

Fixes #13478.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2022-10-24 23:40:44 +02:00 committed by GitHub
parent 50d946e467
commit eda5a562a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,16 +3,16 @@
'use strict';
// Append the script file name OR rule UID depending on which is available
const defaultLoggerName = "org.openhab.automation.script" + (globalThis["javax.script.filename"] ? ".file." + globalThis["javax.script.filename"].replace(/^.*[\\\/]/, '') : globalThis["ruleUID"] ? ".ui." + globalThis["ruleUID"] : "");
const defaultIdentifier = "org.openhab.automation.script" + (globalThis["javax.script.filename"] ? ".file." + globalThis["javax.script.filename"].replace(/^.*[\\\/]/, '') : globalThis["ruleUID"] ? ".ui." + globalThis["ruleUID"] : "");
const System = Java.type('java.lang.System');
const ZonedDateTime = Java.type('java.time.ZonedDateTime');
const formatRegExp = /%[sdj%]/g;
function createLogger(name = defaultLoggerName) {
function createLogger(name = defaultIdentifier) {
return Java.type("org.slf4j.LoggerFactory").getLogger(name);
}
//user configurable
// User configurable
let log = createLogger();
function stringify(value) {
@ -82,6 +82,8 @@
const counters = {};
const timers = {};
// Polyfills for common NodeJS functions
const console = {
'assert': function (expression, message) {
if (!expression) {
@ -159,7 +161,7 @@
}
},
//Allow user customizable logging names
// Allow user customizable logging names
set loggerName(name) {
log = createLogger(name);
this._loggerName = name;
@ -173,6 +175,7 @@
function setTimeout(cb, delay) {
const args = Array.prototype.slice.call(arguments, 2);
return ThreadsafeTimers.createTimerWithArgument(
defaultIdentifier + '.setTimeout',
ZonedDateTime.now().plusNanos(delay * 1000000),
args,
function (args) {
@ -191,6 +194,7 @@
const args = Array.prototype.slice.call(arguments, 2);
const delayNanos = delay * 1000000
let timer = ThreadsafeTimers.createTimerWithArgument(
defaultIdentifier + '.setInterval',
ZonedDateTime.now().plusNanos(delayNanos),
args,
function (args) {
@ -207,14 +211,14 @@
clearTimeout(timer);
}
//Polyfil common functions onto the global object
// Polyfill common NodeJS functions onto the global object
globalThis.console = console;
globalThis.setTimeout = setTimeout;
globalThis.clearTimeout = clearTimeout;
globalThis.setInterval = setInterval;
globalThis.clearInterval = clearInterval;
//Support legacy NodeJS libraries
// Support legacy NodeJS libraries
globalThis.global = globalThis;
globalThis.process = { env: { NODE_ENV: '' } };
})(this);