Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/survey-core/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1400,3 +1400,20 @@ export class EventBase<Sender, Options = any> extends Event<
Sender,
Options
> { }

export class EventAsync<Sender, Options = any> extends EventBase <Sender, Options> {
public async fire(sender: Sender, options: Options, onAsyncCallback?: () => void): Promise<Options> {
if (!this.callbacks) return options;
const callbacks = [].concat(this.callbacks);
for (var i = 0; i < callbacks.length; i++) {
let res = callbacks[i](sender, options);
if (res && res instanceof Promise) {
onAsyncCallback && onAsyncCallback();
onAsyncCallback = null;
await res;
}
if (!this.callbacks) return options;
}
return options;
}
}
56 changes: 54 additions & 2 deletions packages/survey-core/tests/basetests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComputedUpdater, Base, Event, ArrayChanges, IPropertyArrayValueChangedEvent, EventBase, IPropertyValueChangedEvent } from "../src/base";
import { ComputedUpdater, Base, Event, ArrayChanges, IPropertyArrayValueChangedEvent, EventBase, IPropertyValueChangedEvent, EventAsync } from "../src/base";
import { ItemValue } from "../src/itemvalue";
import { ILocalizableOwner, LocalizableString } from "../src/localizablestring";
import { property, Serializer } from "../src/jsonobject";
Expand Down Expand Up @@ -63,7 +63,59 @@ QUnit.test("Do not add function with the same instance several times", function
event.fire(null, null);
assert.equal(counter, 1, "function should not be called the second time");
});

QUnit.test("Add async event", function (assert) {
let done = assert.async();
interface ResultOptions {
counter: number;
}
var event = new EventAsync<any, ResultOptions>();
var func1 = (sender: any, options: ResultOptions) => {
return new Promise((resolve) => {
setTimeout(() => {
options.counter++;
resolve(null);
}, 1);
});
};
var func2 = (sender: any, options: ResultOptions) => {
return new Promise((resolve) => {
setTimeout(() => {
options.counter++;
resolve(null);
}, 1);
});
};
var options: ResultOptions = { counter: 0 };
let counter = 0;
event.add(func1);
event.add(func2);
let fireResult = event.fire(null, options, () => { counter++; });
fireResult.then((options) => {
assert.equal(options.counter, 2, "function called 2 times");
assert.equal(counter, 1, "onAsyncCallbacks called one time #2");
done();
});
assert.equal(counter, 1, "onAsyncCallbacks called one time");
});
QUnit.test("Add sync functions to async event", function (assert) {
let done = assert.async();
interface ResultOptions {
counter: number;
}
var event = new EventAsync<any, ResultOptions>();
var func1 = (sender: any, options: ResultOptions) => { options.counter++; };
var func2 = (sender: any, options: ResultOptions) => { options.counter++; };
var options: ResultOptions = { counter: 0 };
let counter = 0;
event.add(func1);
event.add(func2);
let fireResult = event.fire(null, options, () => { counter++; });
fireResult.then((options) => {
assert.equal(options.counter, 2, "function called 2 times");
done();
});
assert.equal(counter, 0, "onAsyncCallbacks called one time");
});
QUnit.test("Item value & dynamic separator, #10424", function (assert) {
var value = new ItemValue("Item");
assert.equal(value.value, "Item", "simple text value");
Expand Down