UNPKG

@zendesk/laika

Version:

Test, mock, intercept and modify Apollo Client's operations — in both browser and unit tests!

47 lines 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createLazyLoadableLink = void 0; const core_1 = require("@apollo/client/core"); /** * Creates a lazy-loadable Apollo Link. * Useful when you want to use a given Apollo Link in a production environment conditionally, * i.e. only load it when necessary. * @param linkPromise A Promise to an Apollo Link to wrap. */ const createLazyLoadableLink = (linkPromise) => new core_1.ApolloLink((operation, forward) => new core_1.Observable((observer) => { let innerSubscription; let disposed = false; void linkPromise.then((link) => { var _a; if (disposed) return; const actualLinkObservable = link === null || link === void 0 ? void 0 : link.request(operation, forward); if (!actualLinkObservable) { (_a = observer.error) === null || _a === void 0 ? void 0 : _a.call(observer, new Error(`LazyLoadableLink: Incorrect linkPromise provided or it's request function returned null`)); return; } innerSubscription = actualLinkObservable.subscribe({ next: (result) => { var _a; (_a = observer.next) === null || _a === void 0 ? void 0 : _a.call(observer, result); }, error: (error) => { var _a; (_a = observer.error) === null || _a === void 0 ? void 0 : _a.call(observer, error); }, complete: () => { var _a; (_a = observer.complete) === null || _a === void 0 ? void 0 : _a.call(observer); }, }); }, (error) => { var _a; (_a = observer.error) === null || _a === void 0 ? void 0 : _a.call(observer, error); }); return () => { disposed = true; innerSubscription === null || innerSubscription === void 0 ? void 0 : innerSubscription.unsubscribe(); }; })); exports.createLazyLoadableLink = createLazyLoadableLink; //# sourceMappingURL=createLazyLoadableLink.js.map