apollo-link-queue
Version:
A link to queue requests when a certain condition is met (eg. device is offline)
62 lines • 2.48 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
var core_1 = require("@apollo/client/link/core");
var utilities_1 = require("@apollo/client/utilities");
var QueueLink = (function (_super) {
__extends(QueueLink, _super);
function QueueLink() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.opQueue = [];
_this.isOpen = true;
return _this;
}
QueueLink.prototype.open = function () {
this.isOpen = true;
this.opQueue.forEach(function (_a) {
var operation = _a.operation, forward = _a.forward, observer = _a.observer;
forward(operation).subscribe(observer);
});
this.opQueue = [];
};
QueueLink.prototype.close = function () {
this.isOpen = false;
};
QueueLink.prototype.request = function (operation, forward) {
var _this = this;
if (this.isOpen) {
return forward(operation);
}
if (operation.getContext().skipQueue) {
return forward(operation);
}
return new utilities_1.Observable(function (observer) {
var operationEntry = { operation: operation, forward: forward, observer: observer };
_this.enqueue(operationEntry);
return function () { return _this.cancelOperation(operationEntry); };
});
};
QueueLink.prototype.cancelOperation = function (entry) {
this.opQueue = this.opQueue.filter(function (e) { return e !== entry; });
};
QueueLink.prototype.enqueue = function (entry) {
this.opQueue.push(entry);
};
return QueueLink;
}(core_1.ApolloLink));
exports["default"] = QueueLink;
//# sourceMappingURL=QueueLink.js.map