1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const manifest_file_1 = require("@anycli/manifest-file");
|
4 | class PluginCache extends manifest_file_1.default {
|
5 | constructor(file, cacheKey, name) {
|
6 | super(['@anycli/load', name].join(':'), file);
|
7 | this.file = file;
|
8 | this.cacheKey = cacheKey;
|
9 | this.name = name;
|
10 | this.skipIfLocked = true;
|
11 | this.type = 'cache';
|
12 | this.debug('file: %s cacheKey: %s', this.file, this.cacheKey);
|
13 | }
|
14 | async fetch(key, fn) {
|
15 | let [output, cacheKey] = await this.get(key, 'cache_key');
|
16 | if (cacheKey && cacheKey !== this.cacheKey) {
|
17 | await this.reset();
|
18 | output = undefined;
|
19 | }
|
20 | if (output)
|
21 | return output;
|
22 | this.debug('fetching', key);
|
23 | let input = await fn();
|
24 | try {
|
25 | await this.set(['cache_key', this.cacheKey], [key, input]);
|
26 | return input;
|
27 | }
|
28 | catch (err) {
|
29 | this.debug(err);
|
30 | return input;
|
31 | }
|
32 | }
|
33 | }
|
34 | exports.default = PluginCache;
|