Skip to content

Commit

Permalink
[actions] Fix missing timer identifier for UI scripts (#367)
Browse files Browse the repository at this point in the history
Caused by openhab/openhab-core#4289.

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 authored Jul 28, 2024
1 parent a9ca3e7 commit 795862a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class ScriptExecution {
* @returns {*} a native openHAB Timer
*/
static createTimer (identifier, zdt, functionRef, ...params) {
ThreadsafeTimers.setIdentifier(console.loggerName); // eslint-disable-line no-undef
// Support method overloading as identifier is optional
if (typeof identifier === 'string' && functionRef != null) {
const callbackFn = () => functionRef(...params);
Expand Down
21 changes: 19 additions & 2 deletions test/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ jest.mock('../src/osgi');

describe('actions.js', () => {
describe('ScriptExecution', () => {
const defaultIdentifier = 'script-1';

beforeAll(() => {
jest.spyOn(JavaScriptExecution, 'callScript');
jest.spyOn(JavaScriptExecution, 'createTimer');

console.loggerName = defaultIdentifier;
ThreadsafeTimers = {
setIdentifier: jest.fn(),
createTimer: jest.fn()
};
});
Expand All @@ -31,16 +35,29 @@ describe('actions.js', () => {

ScriptExecution.createTimer(identifier, zdt, functionRef, 'prop1');

expect(ThreadsafeTimers.setIdentifier).toHaveBeenCalledWith(defaultIdentifier);
expect(ThreadsafeTimers.createTimer).toHaveBeenCalled();
expect(JavaScriptExecution.callScript).not.toHaveBeenCalled();

jest.clearAllMocks();

ScriptExecution.createTimer(zdt, functionRef, 'prop1');

expect(ThreadsafeTimers.setIdentifier).toHaveBeenCalledWith(defaultIdentifier);
expect(ThreadsafeTimers.createTimer).toHaveBeenCalled();
expect(JavaScriptExecution.callScript).not.toHaveBeenCalled();
});

it('sets default identifier for ThreadsafeTimers.', () => {
const zdt = {};
const functionRef = (foo) => foo;

ScriptExecution.createTimer(zdt, functionRef, 'prop1');

expect(ThreadsafeTimers.setIdentifier).toHaveBeenCalledWith(defaultIdentifier);

ScriptExecution.createTimer(zdt, functionRef, 'prop1');

expect(ThreadsafeTimers.setIdentifier).toHaveBeenCalledWith(defaultIdentifier);
});
});

describe('Transformation', () => {
Expand Down
2 changes: 1 addition & 1 deletion types/actions/actions.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 795862a

Please sign in to comment.