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