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