Skip to content

Commit

Permalink
feat: support multiple events of same type
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Bell committed Oct 28, 2022
1 parent 3456297 commit f1178fe
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const eventLister = (context, event: EventResult): Promise<EventResult> => {
event: { method, section },
} = record;

if (name === `${section}.${method}`) {
if (name === `${section}.${method}` && isExpectedEvent(record, event)) {
resolve(updateEventResult(true, record, event));
unsubscribe();
}
Expand All @@ -205,6 +205,35 @@ const eventLister = (context, event: EventResult): Promise<EventResult> => {
});
};

const isExpectedEvent = (record, event: Readonly<EventResult>): boolean => {
// Clone event result and apply actual event (record) to determine match
let clone : EventResult = { ...event };
clone.data = []; // data must be reset due to lack of deep copy of array with spread operator
updateEventResult(true, record, clone);

const { attributes, data, result } = clone;

// Check whether event is expected based on result/attributes (simplified logic from messageBuilder)
if (result)
return isExpectedEventResult(clone);
else if (attributes)
return attributes.every((attribute, i) => {
try {
const { value, isRange, threshold, xcmOutcome } = attribute;

if (xcmOutcome)
return xcmOutcome === data[i].xcmOutcome;
else if (value && data[i])
return isExpectedEventAttribute(value, data[i].value, isRange, threshold);
} catch (e) {
console.log(e);
return false;
}
});

return false;
};

const isExpectedEventResult = (event: EventResult): boolean => {
const { result, record, strict } = event;

Expand Down

0 comments on commit f1178fe

Please sign in to comment.