UNPKG

@ng-doc/builder

Version:

<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/ng-doc/ng-doc"> <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px"> </a>

61 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PENDING_CACHE = void 0; exports.disableCache = disableCache; exports.handleCacheStrategy = handleCacheStrategy; const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const cache_1 = require("../../cache"); const types_1 = require("../types"); const variables_1 = require("../variables"); let isCacheEnabled = true; /** * Disables cache for all builders. */ function disableCache() { isCacheEnabled = false; } const restoredBuilders = new Set(); exports.PENDING_CACHE = []; /** * Handles cache provided cache strategy. * It will skip or restore the builder based on the strategy on the first run and * update the cache on every next run. * @param id * @param strategy - Cache strategy * @param valid */ function handleCacheStrategy(id, strategy, valid = true) { return (source) => { if (!strategy || !isCacheEnabled) return source; const cache = (0, cache_1.createCache)(undefined, strategy.files?.(), strategy.getData?.() ?? {}); const sourceWithCache = source.pipe((0, operators_1.tap)((state) => { if ((0, types_1.isBuilderDone)(state)) { exports.PENDING_CACHE.push(() => (0, cache_1.updateCache)(strategy.id, (0, cache_1.createCache)(undefined, strategy.files?.(), strategy.getData?.() ?? {}, strategy.action === 'restore' ? strategy.toCache(state.result) : undefined))); } })); // If the builder is already restored, return the source with cache. // Since this function is called every time the builder is triggered (because of `switchMap` in // `runBuild` function), we need to make sure that the builder is restored only once if (restoredBuilders.has(id)) { return sourceWithCache; } const isCacheVal = (0, cache_1.isCacheValid)(strategy.id, cache); const isValid = cache && valid && (0, variables_1.isColdStart)() && isCacheVal; const savedCache = (0, cache_1.loadCache)(strategy.id); savedCache.data && strategy.onCacheLoad?.(savedCache.data); restoredBuilders.add(id); switch (strategy.action) { case 'skip': return isValid ? rxjs_1.EMPTY : sourceWithCache; case 'restore': return isValid ? (0, rxjs_1.of)(new types_1.BuilderDone(strategy.id, strategy.fromCache(savedCache.result ?? ''), true)) : sourceWithCache; default: return source; } }; } //# sourceMappingURL=handle-cache-strategy.js.map