tsbase
Version:
Base class libraries for TypeScript
127 lines • 6.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncObservable = void 0;
var tslib_1 = require("tslib");
var Guid_1 = require("../../System/Guid");
var AsyncCommand_1 = require("../CommandQuery/AsyncCommand");
var BaseObservable_1 = require("./BaseObservable");
var AsyncObservable = /** @class */ (function (_super) {
tslib_1.__extends(AsyncObservable, _super);
function AsyncObservable() {
return _super !== null && _super.apply(this, arguments) || this;
}
AsyncObservable.prototype.Subscribe = function (func, useCurrentIssue) {
if (useCurrentIssue === void 0) { useCurrentIssue = true; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var subscriptionId;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
subscriptionId = Guid_1.Guid.NewGuid();
this.subscribers.set(subscriptionId, func);
if (!(useCurrentIssue && this.CurrentIssue)) return [3 /*break*/, 2];
return [4 /*yield*/, func(this.CurrentIssue)];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/, subscriptionId];
}
});
});
};
AsyncObservable.prototype.Order = function (func, useCurrentIssue) {
if (useCurrentIssue === void 0) { useCurrentIssue = true; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var subscriptionId, orderFunction;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
subscriptionId = Guid_1.Guid.NewGuid();
orderFunction = function (content) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, func(content)];
case 1:
_a.sent();
this.Cancel(subscriptionId);
return [2 /*return*/];
}
});
}); };
this.subscribers.set(subscriptionId, orderFunction);
if (!(useCurrentIssue && this.CurrentIssue)) return [3 /*break*/, 2];
return [4 /*yield*/, orderFunction(this.CurrentIssue)];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
});
};
AsyncObservable.prototype.Publish = function (content) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _loop_1, this_1, _a, _b, element, e_1_1;
var e_1, _c;
var _this = this;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!this.active) return [3 /*break*/, 8];
this.CurrentIssue = content;
_loop_1 = function (element) {
var key, func, result;
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
key = element[0];
func = element[1];
return [4 /*yield*/, new AsyncCommand_1.AsyncCommand(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, func(content)];
}); }); }).Execute()];
case 1:
result = _e.sent();
if (!result.IsSuccess) {
this_1.Cancel(key);
}
return [2 /*return*/];
}
});
};
this_1 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = tslib_1.__values(this.subscribers), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
element = _b.value;
return [5 /*yield**/, _loop_1(element)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _d.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
return AsyncObservable;
}(BaseObservable_1.BaseObservable));
exports.AsyncObservable = AsyncObservable;
//# sourceMappingURL=AsyncObservable.js.map