1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.assertSimpleType = assertSimpleType;
|
---|
7 | exports.makeStrongCache = makeStrongCache;
|
---|
8 | exports.makeStrongCacheSync = makeStrongCacheSync;
|
---|
9 | exports.makeWeakCache = makeWeakCache;
|
---|
10 | exports.makeWeakCacheSync = makeWeakCacheSync;
|
---|
11 | function _gensync() {
|
---|
12 | const data = require("gensync");
|
---|
13 | _gensync = function () {
|
---|
14 | return data;
|
---|
15 | };
|
---|
16 | return data;
|
---|
17 | }
|
---|
18 | var _async = require("../gensync-utils/async.js");
|
---|
19 | var _util = require("./util.js");
|
---|
20 | const synchronize = gen => {
|
---|
21 | return _gensync()(gen).sync;
|
---|
22 | };
|
---|
23 | function* genTrue() {
|
---|
24 | return true;
|
---|
25 | }
|
---|
26 | function makeWeakCache(handler) {
|
---|
27 | return makeCachedFunction(WeakMap, handler);
|
---|
28 | }
|
---|
29 | function makeWeakCacheSync(handler) {
|
---|
30 | return synchronize(makeWeakCache(handler));
|
---|
31 | }
|
---|
32 | function makeStrongCache(handler) {
|
---|
33 | return makeCachedFunction(Map, handler);
|
---|
34 | }
|
---|
35 | function makeStrongCacheSync(handler) {
|
---|
36 | return synchronize(makeStrongCache(handler));
|
---|
37 | }
|
---|
38 | function makeCachedFunction(CallCache, handler) {
|
---|
39 | const callCacheSync = new CallCache();
|
---|
40 | const callCacheAsync = new CallCache();
|
---|
41 | const futureCache = new CallCache();
|
---|
42 | return function* cachedFunction(arg, data) {
|
---|
43 | const asyncContext = yield* (0, _async.isAsync)();
|
---|
44 | const callCache = asyncContext ? callCacheAsync : callCacheSync;
|
---|
45 | const cached = yield* getCachedValueOrWait(asyncContext, callCache, futureCache, arg, data);
|
---|
46 | if (cached.valid) return cached.value;
|
---|
47 | const cache = new CacheConfigurator(data);
|
---|
48 | const handlerResult = handler(arg, cache);
|
---|
49 | let finishLock;
|
---|
50 | let value;
|
---|
51 | if ((0, _util.isIterableIterator)(handlerResult)) {
|
---|
52 | value = yield* (0, _async.onFirstPause)(handlerResult, () => {
|
---|
53 | finishLock = setupAsyncLocks(cache, futureCache, arg);
|
---|
54 | });
|
---|
55 | } else {
|
---|
56 | value = handlerResult;
|
---|
57 | }
|
---|
58 | updateFunctionCache(callCache, cache, arg, value);
|
---|
59 | if (finishLock) {
|
---|
60 | futureCache.delete(arg);
|
---|
61 | finishLock.release(value);
|
---|
62 | }
|
---|
63 | return value;
|
---|
64 | };
|
---|
65 | }
|
---|
66 | function* getCachedValue(cache, arg, data) {
|
---|
67 | const cachedValue = cache.get(arg);
|
---|
68 | if (cachedValue) {
|
---|
69 | for (const {
|
---|
70 | value,
|
---|
71 | valid
|
---|
72 | } of cachedValue) {
|
---|
73 | if (yield* valid(data)) return {
|
---|
74 | valid: true,
|
---|
75 | value
|
---|
76 | };
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return {
|
---|
80 | valid: false,
|
---|
81 | value: null
|
---|
82 | };
|
---|
83 | }
|
---|
84 | function* getCachedValueOrWait(asyncContext, callCache, futureCache, arg, data) {
|
---|
85 | const cached = yield* getCachedValue(callCache, arg, data);
|
---|
86 | if (cached.valid) {
|
---|
87 | return cached;
|
---|
88 | }
|
---|
89 | if (asyncContext) {
|
---|
90 | const cached = yield* getCachedValue(futureCache, arg, data);
|
---|
91 | if (cached.valid) {
|
---|
92 | const value = yield* (0, _async.waitFor)(cached.value.promise);
|
---|
93 | return {
|
---|
94 | valid: true,
|
---|
95 | value
|
---|
96 | };
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return {
|
---|
100 | valid: false,
|
---|
101 | value: null
|
---|
102 | };
|
---|
103 | }
|
---|
104 | function setupAsyncLocks(config, futureCache, arg) {
|
---|
105 | const finishLock = new Lock();
|
---|
106 | updateFunctionCache(futureCache, config, arg, finishLock);
|
---|
107 | return finishLock;
|
---|
108 | }
|
---|
109 | function updateFunctionCache(cache, config, arg, value) {
|
---|
110 | if (!config.configured()) config.forever();
|
---|
111 | let cachedValue = cache.get(arg);
|
---|
112 | config.deactivate();
|
---|
113 | switch (config.mode()) {
|
---|
114 | case "forever":
|
---|
115 | cachedValue = [{
|
---|
116 | value,
|
---|
117 | valid: genTrue
|
---|
118 | }];
|
---|
119 | cache.set(arg, cachedValue);
|
---|
120 | break;
|
---|
121 | case "invalidate":
|
---|
122 | cachedValue = [{
|
---|
123 | value,
|
---|
124 | valid: config.validator()
|
---|
125 | }];
|
---|
126 | cache.set(arg, cachedValue);
|
---|
127 | break;
|
---|
128 | case "valid":
|
---|
129 | if (cachedValue) {
|
---|
130 | cachedValue.push({
|
---|
131 | value,
|
---|
132 | valid: config.validator()
|
---|
133 | });
|
---|
134 | } else {
|
---|
135 | cachedValue = [{
|
---|
136 | value,
|
---|
137 | valid: config.validator()
|
---|
138 | }];
|
---|
139 | cache.set(arg, cachedValue);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | class CacheConfigurator {
|
---|
144 | constructor(data) {
|
---|
145 | this._active = true;
|
---|
146 | this._never = false;
|
---|
147 | this._forever = false;
|
---|
148 | this._invalidate = false;
|
---|
149 | this._configured = false;
|
---|
150 | this._pairs = [];
|
---|
151 | this._data = void 0;
|
---|
152 | this._data = data;
|
---|
153 | }
|
---|
154 | simple() {
|
---|
155 | return makeSimpleConfigurator(this);
|
---|
156 | }
|
---|
157 | mode() {
|
---|
158 | if (this._never) return "never";
|
---|
159 | if (this._forever) return "forever";
|
---|
160 | if (this._invalidate) return "invalidate";
|
---|
161 | return "valid";
|
---|
162 | }
|
---|
163 | forever() {
|
---|
164 | if (!this._active) {
|
---|
165 | throw new Error("Cannot change caching after evaluation has completed.");
|
---|
166 | }
|
---|
167 | if (this._never) {
|
---|
168 | throw new Error("Caching has already been configured with .never()");
|
---|
169 | }
|
---|
170 | this._forever = true;
|
---|
171 | this._configured = true;
|
---|
172 | }
|
---|
173 | never() {
|
---|
174 | if (!this._active) {
|
---|
175 | throw new Error("Cannot change caching after evaluation has completed.");
|
---|
176 | }
|
---|
177 | if (this._forever) {
|
---|
178 | throw new Error("Caching has already been configured with .forever()");
|
---|
179 | }
|
---|
180 | this._never = true;
|
---|
181 | this._configured = true;
|
---|
182 | }
|
---|
183 | using(handler) {
|
---|
184 | if (!this._active) {
|
---|
185 | throw new Error("Cannot change caching after evaluation has completed.");
|
---|
186 | }
|
---|
187 | if (this._never || this._forever) {
|
---|
188 | throw new Error("Caching has already been configured with .never or .forever()");
|
---|
189 | }
|
---|
190 | this._configured = true;
|
---|
191 | const key = handler(this._data);
|
---|
192 | const fn = (0, _async.maybeAsync)(handler, `You appear to be using an async cache handler, but Babel has been called synchronously`);
|
---|
193 | if ((0, _async.isThenable)(key)) {
|
---|
194 | return key.then(key => {
|
---|
195 | this._pairs.push([key, fn]);
|
---|
196 | return key;
|
---|
197 | });
|
---|
198 | }
|
---|
199 | this._pairs.push([key, fn]);
|
---|
200 | return key;
|
---|
201 | }
|
---|
202 | invalidate(handler) {
|
---|
203 | this._invalidate = true;
|
---|
204 | return this.using(handler);
|
---|
205 | }
|
---|
206 | validator() {
|
---|
207 | const pairs = this._pairs;
|
---|
208 | return function* (data) {
|
---|
209 | for (const [key, fn] of pairs) {
|
---|
210 | if (key !== (yield* fn(data))) return false;
|
---|
211 | }
|
---|
212 | return true;
|
---|
213 | };
|
---|
214 | }
|
---|
215 | deactivate() {
|
---|
216 | this._active = false;
|
---|
217 | }
|
---|
218 | configured() {
|
---|
219 | return this._configured;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | function makeSimpleConfigurator(cache) {
|
---|
223 | function cacheFn(val) {
|
---|
224 | if (typeof val === "boolean") {
|
---|
225 | if (val) cache.forever();else cache.never();
|
---|
226 | return;
|
---|
227 | }
|
---|
228 | return cache.using(() => assertSimpleType(val()));
|
---|
229 | }
|
---|
230 | cacheFn.forever = () => cache.forever();
|
---|
231 | cacheFn.never = () => cache.never();
|
---|
232 | cacheFn.using = cb => cache.using(() => assertSimpleType(cb()));
|
---|
233 | cacheFn.invalidate = cb => cache.invalidate(() => assertSimpleType(cb()));
|
---|
234 | return cacheFn;
|
---|
235 | }
|
---|
236 | function assertSimpleType(value) {
|
---|
237 | if ((0, _async.isThenable)(value)) {
|
---|
238 | throw new Error(`You appear to be using an async cache handler, ` + `which your current version of Babel does not support. ` + `We may add support for this in the future, ` + `but if you're on the most recent version of @babel/core and still ` + `seeing this error, then you'll need to synchronously handle your caching logic.`);
|
---|
239 | }
|
---|
240 | if (value != null && typeof value !== "string" && typeof value !== "boolean" && typeof value !== "number") {
|
---|
241 | throw new Error("Cache keys must be either string, boolean, number, null, or undefined.");
|
---|
242 | }
|
---|
243 | return value;
|
---|
244 | }
|
---|
245 | class Lock {
|
---|
246 | constructor() {
|
---|
247 | this.released = false;
|
---|
248 | this.promise = void 0;
|
---|
249 | this._resolve = void 0;
|
---|
250 | this.promise = new Promise(resolve => {
|
---|
251 | this._resolve = resolve;
|
---|
252 | });
|
---|
253 | }
|
---|
254 | release(value) {
|
---|
255 | this.released = true;
|
---|
256 | this._resolve(value);
|
---|
257 | }
|
---|
258 | }
|
---|
259 | 0 && 0;
|
---|
260 |
|
---|
261 | //# sourceMappingURL=caching.js.map
|
---|