Skip to content

Commit

Permalink
ensure that setDataChangeCallback does not override existing callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Sergeev committed Nov 1, 2023
1 parent f147454 commit 68e2253
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,12 +1063,22 @@ export default class TemplateProcessor {

setDataChangeCallback(jsonPtr, cbFn) {
if(jsonPtr === "/"){
if (this.commonCallback !== undefined) {
return false;
}
this.commonCallback = cbFn;
return true;
}else if (jp.has(this.templateMeta, jsonPtr)) {
const node = jp.get(this.templateMeta, jsonPtr);
if (node.callback__ !== undefined){
return false
}
node.callback__ = cbFn;
return true;
}
return false;
}

removeDataChangeCallback(jsonPtr) {
if(jsonPtr === "/"){
this.commonCallback = undefined;
Expand Down Expand Up @@ -1137,7 +1147,7 @@ export default class TemplateProcessor {

let checkConditionCallback = async (data, jsonPtr) => {
let matchedCondition = await conditionExpression.evaluate(this.output);
if (matchedCondition) {
if (matchedCondition===true) {
this.removeDataChangeCallback('/');
clearTimeout(timeoutId); // Clear the timeout when condition is met
this.logger.debug(`received data change matching ${waitCondition} for ${jsonPtr} with data ${data}`);
Expand All @@ -1148,7 +1158,13 @@ export default class TemplateProcessor {
};

checkConditionCallback = checkConditionCallback.bind(this);
this.setDataChangeCallback("/", checkConditionCallback);
const callbackIsSet = this.setDataChangeCallback("/", checkConditionCallback);
if(callbackIsSet === false){
clearTimeout(timeoutId);
resolve({"error": {
message: "can't use wait condition because a callback is already set"
}});
}

//execute the callback once to evaluate if the condition is already met
checkConditionCallback({}, "/");
Expand Down
20 changes: 18 additions & 2 deletions src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ test("test 1", async () => {
});
await tp.initialize();
const received = [];
tp.setDataChangeCallback("/a", (data, jsonPtr) => {
expect(tp.setDataChangeCallback("/a", (data, jsonPtr) => {
received.push({data, jsonPtr})
});
})).toBe(true);
// can't set data change a
expect(tp.setDataChangeCallback("/a", (data, jsonPtr) => {
received.push({data, jsonPtr})
})).toBe(false);
tp.setDataChangeCallback("/b", (data, jsonPtr) => {
received.push({data, jsonPtr})
});
Expand Down Expand Up @@ -1436,3 +1440,15 @@ test("wait condition timeout", async () => {
}
}));
});

test("wait condition with commomCallback already set", async () => {
const tp = new TemplateProcessor({});
tp.setDataChangeCallback("/", (data, jsonPtr) => {});
await tp.initialize();
let conditionMatched = await tp.waitCondition('status$="done"', 500);
expect(StatedREPL.stringify(conditionMatched)).toEqual(StatedREPL.stringify({
"error": {
"message": "can't use wait condition because a callback is already set",
}
}));
});

0 comments on commit 68e2253

Please sign in to comment.