@pnp/odata
Version:
pnp - provides shared odata functionality and base classes
103 lines • 3.66 kB
JavaScript
import { getGUID } from "@pnp/common";
var Batch = /** @class */ (function () {
function Batch(_batchId) {
if (_batchId === void 0) { _batchId = getGUID(); }
this._batchId = _batchId;
this._reqs = [];
this._deps = [];
this._rDeps = [];
this._index = -1;
}
Object.defineProperty(Batch.prototype, "batchId", {
get: function () {
return this._batchId;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Batch.prototype, "requests", {
/**
* The requests contained in this batch
*/
get: function () {
// we sort these each time this is accessed
return this._reqs.sort(function (info1, info2) { return info1.index - info2.index; });
},
enumerable: false,
configurable: true
});
/**
* Not meant for use directly
*
* @param batchee The IQueryable for this batch to track in order
*/
Batch.prototype.track = function (batchee) {
batchee.data.batch = this;
// we need to track the order requests are added to the batch to ensure we always
// operate on them in order
if (typeof batchee.data.batchIndex === "undefined" || batchee.data.batchIndex < 0) {
batchee.data.batchIndex = ++this._index;
}
};
/**
* Adds the given request context to the batch for execution
*
* @param context Details of the request to batch
*/
Batch.prototype.add = function (context) {
var info = {
id: context.requestId,
index: context.batchIndex,
method: context.method.toUpperCase(),
options: context.options,
parser: context.parser,
reject: null,
resolve: null,
url: context.url,
};
// we create a new promise that will be resolved within the batch
var p = new Promise(function (resolve, reject) {
info.resolve = resolve;
info.reject = reject;
});
this._reqs.push(info);
return p;
};
/**
* Adds a dependency insuring that some set of actions will occur before a batch is processed.
* MUST be cleared using the returned resolve delegate to allow batches to run
*/
Batch.prototype.addDependency = function () {
var resolver = function () { return void (0); };
this._deps.push(new Promise(function (resolve) {
resolver = resolve;
}));
return resolver;
};
/**
* The batch's execute method will not resolve util any promises added here resolve
*
* @param p The dependent promise
*/
Batch.prototype.addResolveBatchDependency = function (p) {
this._rDeps.push(p);
};
/**
* Execute the current batch and resolve the associated promises
*
* @returns A promise which will be resolved once all of the batch's child promises have resolved
*/
Batch.prototype.execute = function () {
var _this = this;
// we need to check the dependencies twice due to how different engines handle things.
// We can get a second set of promises added during the first set resolving
return Promise.all(this._deps)
.then(function () { return Promise.all(_this._deps); })
.then(function () { return _this.executeImpl(); })
.then(function () { return Promise.all(_this._rDeps); })
.then(function () { return void (0); });
};
return Batch;
}());
export { Batch };
//# sourceMappingURL=batch.js.map