@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
57 lines (52 loc) • 2.21 kB
JavaScript
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
/* eslint-disable max-classes-per-file */
import { BaseError } from './errors.js';
export function isMiddlewareRawPage(maybePage) {
const testPage = maybePage;
return testPage?.data != null && Array.isArray(testPage.data) && 'next' in testPage && 'prev' in testPage;
}
/**
* @category exception
*/
export class MiddlewarePageMissed extends BaseError {
constructor(isNext) {
super(`There is no ${isNext ? 'next' : 'previous'} page`);
this.name = 'MiddlewarePageMissed';
}
}
/**
* A wrapper around the middleware's page allowing to get the next/previous pages.
*/
var _middleware = /*#__PURE__*/new WeakMap();
export class MiddlewarePage {
constructor(rawPage, middleware) {
_classPrivateFieldInitSpec(this, _middleware, void 0);
this.data = rawPage.data;
this.nextPath = rawPage.next;
this.prevPath = rawPage.prev;
_classPrivateFieldSet(_middleware, this, middleware);
}
/**
* Get the next page.
* Check the presence of `nextPath` to not fall outside existing pages.
* @throws MiddlewarePageMissed
*/
async next() {
if (this.nextPath == null) throw new MiddlewarePageMissed(true);
return _classPrivateFieldGet(_middleware, this).requestByPath(this.nextPath);
}
/**
* Get the previous page.
* Check the presence of `prevPath` to not fall outside existing pages.
* @throws MiddlewarePageMissed
*/
async prev() {
if (this.prevPath == null) throw new MiddlewarePageMissed(false);
return _classPrivateFieldGet(_middleware, this).requestByPath(this.prevPath);
}
}
//# sourceMappingURL=MiddlewarePage.js.map