tsbase
Version:
Base class libraries for TypeScript
42 lines • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncObservable = void 0;
const Guid_1 = require("../../System/Guid");
const AsyncCommand_1 = require("../CommandQuery/AsyncCommand");
const BaseObservable_1 = require("./BaseObservable");
class AsyncObservable extends BaseObservable_1.BaseObservable {
async Subscribe(func, useCurrentIssue = true) {
const subscriptionId = Guid_1.Guid.NewGuid();
this.subscribers.set(subscriptionId, func);
if (useCurrentIssue && this.CurrentIssue) {
await func(this.CurrentIssue);
}
return subscriptionId;
}
async Order(func, useCurrentIssue = true) {
const subscriptionId = Guid_1.Guid.NewGuid();
const orderFunction = async (content) => {
await func(content);
this.Cancel(subscriptionId);
};
this.subscribers.set(subscriptionId, orderFunction);
if (useCurrentIssue && this.CurrentIssue) {
await orderFunction(this.CurrentIssue);
}
}
async Publish(content) {
if (this.active) {
this.CurrentIssue = content;
for (const element of this.subscribers) {
const key = element[0];
const func = element[1];
const result = await new AsyncCommand_1.AsyncCommand(async () => func(content)).Execute();
if (!result.IsSuccess) {
this.Cancel(key);
}
}
}
}
}
exports.AsyncObservable = AsyncObservable;
//# sourceMappingURL=AsyncObservable.js.map