@git-diff-view/react 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.development.js +1594 -124
- package/dist/cjs/index.development.js.map +1 -1
- package/dist/cjs/index.production.js +1594 -124
- package/dist/cjs/index.production.js.map +1 -1
- package/dist/esm/index.mjs +1595 -125
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/components/DiffContent.d.ts.map +1 -1
- package/dist/types/components/DiffSplitViewNormal.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -25,6 +25,1447 @@ function _interopNamespaceDefault(e) {
|
|
|
25
25
|
|
|
26
26
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* @module LRUCache
|
|
30
|
+
*/
|
|
31
|
+
const perf = typeof performance === 'object' &&
|
|
32
|
+
performance &&
|
|
33
|
+
typeof performance.now === 'function'
|
|
34
|
+
? performance
|
|
35
|
+
: Date;
|
|
36
|
+
const warned = new Set();
|
|
37
|
+
/* c8 ignore start */
|
|
38
|
+
const PROCESS = (typeof process === 'object' && !!process ? process : {});
|
|
39
|
+
/* c8 ignore start */
|
|
40
|
+
const emitWarning = (msg, type, code, fn) => {
|
|
41
|
+
typeof PROCESS.emitWarning === 'function'
|
|
42
|
+
? PROCESS.emitWarning(msg, type, code, fn)
|
|
43
|
+
: console.error(`[${code}] ${type}: ${msg}`);
|
|
44
|
+
};
|
|
45
|
+
let AC = globalThis.AbortController;
|
|
46
|
+
let AS = globalThis.AbortSignal;
|
|
47
|
+
/* c8 ignore start */
|
|
48
|
+
if (typeof AC === 'undefined') {
|
|
49
|
+
//@ts-ignore
|
|
50
|
+
AS = class AbortSignal {
|
|
51
|
+
onabort;
|
|
52
|
+
_onabort = [];
|
|
53
|
+
reason;
|
|
54
|
+
aborted = false;
|
|
55
|
+
addEventListener(_, fn) {
|
|
56
|
+
this._onabort.push(fn);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
//@ts-ignore
|
|
60
|
+
AC = class AbortController {
|
|
61
|
+
constructor() {
|
|
62
|
+
warnACPolyfill();
|
|
63
|
+
}
|
|
64
|
+
signal = new AS();
|
|
65
|
+
abort(reason) {
|
|
66
|
+
if (this.signal.aborted)
|
|
67
|
+
return;
|
|
68
|
+
//@ts-ignore
|
|
69
|
+
this.signal.reason = reason;
|
|
70
|
+
//@ts-ignore
|
|
71
|
+
this.signal.aborted = true;
|
|
72
|
+
//@ts-ignore
|
|
73
|
+
for (const fn of this.signal._onabort) {
|
|
74
|
+
fn(reason);
|
|
75
|
+
}
|
|
76
|
+
this.signal.onabort?.(reason);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
let printACPolyfillWarning = PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1';
|
|
80
|
+
const warnACPolyfill = () => {
|
|
81
|
+
if (!printACPolyfillWarning)
|
|
82
|
+
return;
|
|
83
|
+
printACPolyfillWarning = false;
|
|
84
|
+
emitWarning('AbortController is not defined. If using lru-cache in ' +
|
|
85
|
+
'node 14, load an AbortController polyfill from the ' +
|
|
86
|
+
'`node-abort-controller` package. A minimal polyfill is ' +
|
|
87
|
+
'provided for use by LRUCache.fetch(), but it should not be ' +
|
|
88
|
+
'relied upon in other contexts (eg, passing it to other APIs that ' +
|
|
89
|
+
'use AbortController/AbortSignal might have undesirable effects). ' +
|
|
90
|
+
'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.', 'NO_ABORT_CONTROLLER', 'ENOTSUP', warnACPolyfill);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/* c8 ignore stop */
|
|
94
|
+
const shouldWarn = (code) => !warned.has(code);
|
|
95
|
+
const isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
|
|
96
|
+
/* c8 ignore start */
|
|
97
|
+
// This is a little bit ridiculous, tbh.
|
|
98
|
+
// The maximum array length is 2^32-1 or thereabouts on most JS impls.
|
|
99
|
+
// And well before that point, you're caching the entire world, I mean,
|
|
100
|
+
// that's ~32GB of just integers for the next/prev links, plus whatever
|
|
101
|
+
// else to hold that many keys and values. Just filling the memory with
|
|
102
|
+
// zeroes at init time is brutal when you get that big.
|
|
103
|
+
// But why not be complete?
|
|
104
|
+
// Maybe in the future, these limits will have expanded.
|
|
105
|
+
const getUintArray = (max) => !isPosInt(max)
|
|
106
|
+
? null
|
|
107
|
+
: max <= Math.pow(2, 8)
|
|
108
|
+
? Uint8Array
|
|
109
|
+
: max <= Math.pow(2, 16)
|
|
110
|
+
? Uint16Array
|
|
111
|
+
: max <= Math.pow(2, 32)
|
|
112
|
+
? Uint32Array
|
|
113
|
+
: max <= Number.MAX_SAFE_INTEGER
|
|
114
|
+
? ZeroArray
|
|
115
|
+
: null;
|
|
116
|
+
/* c8 ignore stop */
|
|
117
|
+
class ZeroArray extends Array {
|
|
118
|
+
constructor(size) {
|
|
119
|
+
super(size);
|
|
120
|
+
this.fill(0);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
class Stack {
|
|
124
|
+
heap;
|
|
125
|
+
length;
|
|
126
|
+
// private constructor
|
|
127
|
+
static #constructing = false;
|
|
128
|
+
static create(max) {
|
|
129
|
+
const HeapCls = getUintArray(max);
|
|
130
|
+
if (!HeapCls)
|
|
131
|
+
return [];
|
|
132
|
+
Stack.#constructing = true;
|
|
133
|
+
const s = new Stack(max, HeapCls);
|
|
134
|
+
Stack.#constructing = false;
|
|
135
|
+
return s;
|
|
136
|
+
}
|
|
137
|
+
constructor(max, HeapCls) {
|
|
138
|
+
/* c8 ignore start */
|
|
139
|
+
if (!Stack.#constructing) {
|
|
140
|
+
throw new TypeError('instantiate Stack using Stack.create(n)');
|
|
141
|
+
}
|
|
142
|
+
/* c8 ignore stop */
|
|
143
|
+
this.heap = new HeapCls(max);
|
|
144
|
+
this.length = 0;
|
|
145
|
+
}
|
|
146
|
+
push(n) {
|
|
147
|
+
this.heap[this.length++] = n;
|
|
148
|
+
}
|
|
149
|
+
pop() {
|
|
150
|
+
return this.heap[--this.length];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Default export, the thing you're using this module to get.
|
|
155
|
+
*
|
|
156
|
+
* All properties from the options object (with the exception of
|
|
157
|
+
* {@link OptionsBase.max} and {@link OptionsBase.maxSize}) are added as
|
|
158
|
+
* normal public members. (`max` and `maxBase` are read-only getters.)
|
|
159
|
+
* Changing any of these will alter the defaults for subsequent method calls,
|
|
160
|
+
* but is otherwise safe.
|
|
161
|
+
*/
|
|
162
|
+
class LRUCache {
|
|
163
|
+
// properties coming in from the options of these, only max and maxSize
|
|
164
|
+
// really *need* to be protected. The rest can be modified, as they just
|
|
165
|
+
// set defaults for various methods.
|
|
166
|
+
#max;
|
|
167
|
+
#maxSize;
|
|
168
|
+
#dispose;
|
|
169
|
+
#disposeAfter;
|
|
170
|
+
#fetchMethod;
|
|
171
|
+
/**
|
|
172
|
+
* {@link LRUCache.OptionsBase.ttl}
|
|
173
|
+
*/
|
|
174
|
+
ttl;
|
|
175
|
+
/**
|
|
176
|
+
* {@link LRUCache.OptionsBase.ttlResolution}
|
|
177
|
+
*/
|
|
178
|
+
ttlResolution;
|
|
179
|
+
/**
|
|
180
|
+
* {@link LRUCache.OptionsBase.ttlAutopurge}
|
|
181
|
+
*/
|
|
182
|
+
ttlAutopurge;
|
|
183
|
+
/**
|
|
184
|
+
* {@link LRUCache.OptionsBase.updateAgeOnGet}
|
|
185
|
+
*/
|
|
186
|
+
updateAgeOnGet;
|
|
187
|
+
/**
|
|
188
|
+
* {@link LRUCache.OptionsBase.updateAgeOnHas}
|
|
189
|
+
*/
|
|
190
|
+
updateAgeOnHas;
|
|
191
|
+
/**
|
|
192
|
+
* {@link LRUCache.OptionsBase.allowStale}
|
|
193
|
+
*/
|
|
194
|
+
allowStale;
|
|
195
|
+
/**
|
|
196
|
+
* {@link LRUCache.OptionsBase.noDisposeOnSet}
|
|
197
|
+
*/
|
|
198
|
+
noDisposeOnSet;
|
|
199
|
+
/**
|
|
200
|
+
* {@link LRUCache.OptionsBase.noUpdateTTL}
|
|
201
|
+
*/
|
|
202
|
+
noUpdateTTL;
|
|
203
|
+
/**
|
|
204
|
+
* {@link LRUCache.OptionsBase.maxEntrySize}
|
|
205
|
+
*/
|
|
206
|
+
maxEntrySize;
|
|
207
|
+
/**
|
|
208
|
+
* {@link LRUCache.OptionsBase.sizeCalculation}
|
|
209
|
+
*/
|
|
210
|
+
sizeCalculation;
|
|
211
|
+
/**
|
|
212
|
+
* {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
|
|
213
|
+
*/
|
|
214
|
+
noDeleteOnFetchRejection;
|
|
215
|
+
/**
|
|
216
|
+
* {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
|
|
217
|
+
*/
|
|
218
|
+
noDeleteOnStaleGet;
|
|
219
|
+
/**
|
|
220
|
+
* {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
|
|
221
|
+
*/
|
|
222
|
+
allowStaleOnFetchAbort;
|
|
223
|
+
/**
|
|
224
|
+
* {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
|
|
225
|
+
*/
|
|
226
|
+
allowStaleOnFetchRejection;
|
|
227
|
+
/**
|
|
228
|
+
* {@link LRUCache.OptionsBase.ignoreFetchAbort}
|
|
229
|
+
*/
|
|
230
|
+
ignoreFetchAbort;
|
|
231
|
+
// computed properties
|
|
232
|
+
#size;
|
|
233
|
+
#calculatedSize;
|
|
234
|
+
#keyMap;
|
|
235
|
+
#keyList;
|
|
236
|
+
#valList;
|
|
237
|
+
#next;
|
|
238
|
+
#prev;
|
|
239
|
+
#head;
|
|
240
|
+
#tail;
|
|
241
|
+
#free;
|
|
242
|
+
#disposed;
|
|
243
|
+
#sizes;
|
|
244
|
+
#starts;
|
|
245
|
+
#ttls;
|
|
246
|
+
#hasDispose;
|
|
247
|
+
#hasFetchMethod;
|
|
248
|
+
#hasDisposeAfter;
|
|
249
|
+
/**
|
|
250
|
+
* Do not call this method unless you need to inspect the
|
|
251
|
+
* inner workings of the cache. If anything returned by this
|
|
252
|
+
* object is modified in any way, strange breakage may occur.
|
|
253
|
+
*
|
|
254
|
+
* These fields are private for a reason!
|
|
255
|
+
*
|
|
256
|
+
* @internal
|
|
257
|
+
*/
|
|
258
|
+
static unsafeExposeInternals(c) {
|
|
259
|
+
return {
|
|
260
|
+
// properties
|
|
261
|
+
starts: c.#starts,
|
|
262
|
+
ttls: c.#ttls,
|
|
263
|
+
sizes: c.#sizes,
|
|
264
|
+
keyMap: c.#keyMap,
|
|
265
|
+
keyList: c.#keyList,
|
|
266
|
+
valList: c.#valList,
|
|
267
|
+
next: c.#next,
|
|
268
|
+
prev: c.#prev,
|
|
269
|
+
get head() {
|
|
270
|
+
return c.#head;
|
|
271
|
+
},
|
|
272
|
+
get tail() {
|
|
273
|
+
return c.#tail;
|
|
274
|
+
},
|
|
275
|
+
free: c.#free,
|
|
276
|
+
// methods
|
|
277
|
+
isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
|
|
278
|
+
backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
|
|
279
|
+
moveToTail: (index) => c.#moveToTail(index),
|
|
280
|
+
indexes: (options) => c.#indexes(options),
|
|
281
|
+
rindexes: (options) => c.#rindexes(options),
|
|
282
|
+
isStale: (index) => c.#isStale(index),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
// Protected read-only members
|
|
286
|
+
/**
|
|
287
|
+
* {@link LRUCache.OptionsBase.max} (read-only)
|
|
288
|
+
*/
|
|
289
|
+
get max() {
|
|
290
|
+
return this.#max;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* {@link LRUCache.OptionsBase.maxSize} (read-only)
|
|
294
|
+
*/
|
|
295
|
+
get maxSize() {
|
|
296
|
+
return this.#maxSize;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* The total computed size of items in the cache (read-only)
|
|
300
|
+
*/
|
|
301
|
+
get calculatedSize() {
|
|
302
|
+
return this.#calculatedSize;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* The number of items stored in the cache (read-only)
|
|
306
|
+
*/
|
|
307
|
+
get size() {
|
|
308
|
+
return this.#size;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* {@link LRUCache.OptionsBase.fetchMethod} (read-only)
|
|
312
|
+
*/
|
|
313
|
+
get fetchMethod() {
|
|
314
|
+
return this.#fetchMethod;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* {@link LRUCache.OptionsBase.dispose} (read-only)
|
|
318
|
+
*/
|
|
319
|
+
get dispose() {
|
|
320
|
+
return this.#dispose;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* {@link LRUCache.OptionsBase.disposeAfter} (read-only)
|
|
324
|
+
*/
|
|
325
|
+
get disposeAfter() {
|
|
326
|
+
return this.#disposeAfter;
|
|
327
|
+
}
|
|
328
|
+
constructor(options) {
|
|
329
|
+
const { max = 0, ttl, ttlResolution = 1, ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0, maxEntrySize = 0, sizeCalculation, fetchMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, } = options;
|
|
330
|
+
if (max !== 0 && !isPosInt(max)) {
|
|
331
|
+
throw new TypeError('max option must be a nonnegative integer');
|
|
332
|
+
}
|
|
333
|
+
const UintArray = max ? getUintArray(max) : Array;
|
|
334
|
+
if (!UintArray) {
|
|
335
|
+
throw new Error('invalid max value: ' + max);
|
|
336
|
+
}
|
|
337
|
+
this.#max = max;
|
|
338
|
+
this.#maxSize = maxSize;
|
|
339
|
+
this.maxEntrySize = maxEntrySize || this.#maxSize;
|
|
340
|
+
this.sizeCalculation = sizeCalculation;
|
|
341
|
+
if (this.sizeCalculation) {
|
|
342
|
+
if (!this.#maxSize && !this.maxEntrySize) {
|
|
343
|
+
throw new TypeError('cannot set sizeCalculation without setting maxSize or maxEntrySize');
|
|
344
|
+
}
|
|
345
|
+
if (typeof this.sizeCalculation !== 'function') {
|
|
346
|
+
throw new TypeError('sizeCalculation set to non-function');
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (fetchMethod !== undefined &&
|
|
350
|
+
typeof fetchMethod !== 'function') {
|
|
351
|
+
throw new TypeError('fetchMethod must be a function if specified');
|
|
352
|
+
}
|
|
353
|
+
this.#fetchMethod = fetchMethod;
|
|
354
|
+
this.#hasFetchMethod = !!fetchMethod;
|
|
355
|
+
this.#keyMap = new Map();
|
|
356
|
+
this.#keyList = new Array(max).fill(undefined);
|
|
357
|
+
this.#valList = new Array(max).fill(undefined);
|
|
358
|
+
this.#next = new UintArray(max);
|
|
359
|
+
this.#prev = new UintArray(max);
|
|
360
|
+
this.#head = 0;
|
|
361
|
+
this.#tail = 0;
|
|
362
|
+
this.#free = Stack.create(max);
|
|
363
|
+
this.#size = 0;
|
|
364
|
+
this.#calculatedSize = 0;
|
|
365
|
+
if (typeof dispose === 'function') {
|
|
366
|
+
this.#dispose = dispose;
|
|
367
|
+
}
|
|
368
|
+
if (typeof disposeAfter === 'function') {
|
|
369
|
+
this.#disposeAfter = disposeAfter;
|
|
370
|
+
this.#disposed = [];
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
this.#disposeAfter = undefined;
|
|
374
|
+
this.#disposed = undefined;
|
|
375
|
+
}
|
|
376
|
+
this.#hasDispose = !!this.#dispose;
|
|
377
|
+
this.#hasDisposeAfter = !!this.#disposeAfter;
|
|
378
|
+
this.noDisposeOnSet = !!noDisposeOnSet;
|
|
379
|
+
this.noUpdateTTL = !!noUpdateTTL;
|
|
380
|
+
this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
|
|
381
|
+
this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
|
|
382
|
+
this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
|
|
383
|
+
this.ignoreFetchAbort = !!ignoreFetchAbort;
|
|
384
|
+
// NB: maxEntrySize is set to maxSize if it's set
|
|
385
|
+
if (this.maxEntrySize !== 0) {
|
|
386
|
+
if (this.#maxSize !== 0) {
|
|
387
|
+
if (!isPosInt(this.#maxSize)) {
|
|
388
|
+
throw new TypeError('maxSize must be a positive integer if specified');
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (!isPosInt(this.maxEntrySize)) {
|
|
392
|
+
throw new TypeError('maxEntrySize must be a positive integer if specified');
|
|
393
|
+
}
|
|
394
|
+
this.#initializeSizeTracking();
|
|
395
|
+
}
|
|
396
|
+
this.allowStale = !!allowStale;
|
|
397
|
+
this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
|
|
398
|
+
this.updateAgeOnGet = !!updateAgeOnGet;
|
|
399
|
+
this.updateAgeOnHas = !!updateAgeOnHas;
|
|
400
|
+
this.ttlResolution =
|
|
401
|
+
isPosInt(ttlResolution) || ttlResolution === 0
|
|
402
|
+
? ttlResolution
|
|
403
|
+
: 1;
|
|
404
|
+
this.ttlAutopurge = !!ttlAutopurge;
|
|
405
|
+
this.ttl = ttl || 0;
|
|
406
|
+
if (this.ttl) {
|
|
407
|
+
if (!isPosInt(this.ttl)) {
|
|
408
|
+
throw new TypeError('ttl must be a positive integer if specified');
|
|
409
|
+
}
|
|
410
|
+
this.#initializeTTLTracking();
|
|
411
|
+
}
|
|
412
|
+
// do not allow completely unbounded caches
|
|
413
|
+
if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
|
|
414
|
+
throw new TypeError('At least one of max, maxSize, or ttl is required');
|
|
415
|
+
}
|
|
416
|
+
if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
|
|
417
|
+
const code = 'LRU_CACHE_UNBOUNDED';
|
|
418
|
+
if (shouldWarn(code)) {
|
|
419
|
+
warned.add(code);
|
|
420
|
+
const msg = 'TTL caching without ttlAutopurge, max, or maxSize can ' +
|
|
421
|
+
'result in unbounded memory consumption.';
|
|
422
|
+
emitWarning(msg, 'UnboundedCacheWarning', code, LRUCache);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Return the remaining TTL time for a given entry key
|
|
428
|
+
*/
|
|
429
|
+
getRemainingTTL(key) {
|
|
430
|
+
return this.#keyMap.has(key) ? Infinity : 0;
|
|
431
|
+
}
|
|
432
|
+
#initializeTTLTracking() {
|
|
433
|
+
const ttls = new ZeroArray(this.#max);
|
|
434
|
+
const starts = new ZeroArray(this.#max);
|
|
435
|
+
this.#ttls = ttls;
|
|
436
|
+
this.#starts = starts;
|
|
437
|
+
this.#setItemTTL = (index, ttl, start = perf.now()) => {
|
|
438
|
+
starts[index] = ttl !== 0 ? start : 0;
|
|
439
|
+
ttls[index] = ttl;
|
|
440
|
+
if (ttl !== 0 && this.ttlAutopurge) {
|
|
441
|
+
const t = setTimeout(() => {
|
|
442
|
+
if (this.#isStale(index)) {
|
|
443
|
+
this.delete(this.#keyList[index]);
|
|
444
|
+
}
|
|
445
|
+
}, ttl + 1);
|
|
446
|
+
// unref() not supported on all platforms
|
|
447
|
+
/* c8 ignore start */
|
|
448
|
+
if (t.unref) {
|
|
449
|
+
t.unref();
|
|
450
|
+
}
|
|
451
|
+
/* c8 ignore stop */
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
this.#updateItemAge = index => {
|
|
455
|
+
starts[index] = ttls[index] !== 0 ? perf.now() : 0;
|
|
456
|
+
};
|
|
457
|
+
this.#statusTTL = (status, index) => {
|
|
458
|
+
if (ttls[index]) {
|
|
459
|
+
const ttl = ttls[index];
|
|
460
|
+
const start = starts[index];
|
|
461
|
+
/* c8 ignore next */
|
|
462
|
+
if (!ttl || !start)
|
|
463
|
+
return;
|
|
464
|
+
status.ttl = ttl;
|
|
465
|
+
status.start = start;
|
|
466
|
+
status.now = cachedNow || getNow();
|
|
467
|
+
const age = status.now - start;
|
|
468
|
+
status.remainingTTL = ttl - age;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
// debounce calls to perf.now() to 1s so we're not hitting
|
|
472
|
+
// that costly call repeatedly.
|
|
473
|
+
let cachedNow = 0;
|
|
474
|
+
const getNow = () => {
|
|
475
|
+
const n = perf.now();
|
|
476
|
+
if (this.ttlResolution > 0) {
|
|
477
|
+
cachedNow = n;
|
|
478
|
+
const t = setTimeout(() => (cachedNow = 0), this.ttlResolution);
|
|
479
|
+
// not available on all platforms
|
|
480
|
+
/* c8 ignore start */
|
|
481
|
+
if (t.unref) {
|
|
482
|
+
t.unref();
|
|
483
|
+
}
|
|
484
|
+
/* c8 ignore stop */
|
|
485
|
+
}
|
|
486
|
+
return n;
|
|
487
|
+
};
|
|
488
|
+
this.getRemainingTTL = key => {
|
|
489
|
+
const index = this.#keyMap.get(key);
|
|
490
|
+
if (index === undefined) {
|
|
491
|
+
return 0;
|
|
492
|
+
}
|
|
493
|
+
const ttl = ttls[index];
|
|
494
|
+
const start = starts[index];
|
|
495
|
+
if (!ttl || !start) {
|
|
496
|
+
return Infinity;
|
|
497
|
+
}
|
|
498
|
+
const age = (cachedNow || getNow()) - start;
|
|
499
|
+
return ttl - age;
|
|
500
|
+
};
|
|
501
|
+
this.#isStale = index => {
|
|
502
|
+
const s = starts[index];
|
|
503
|
+
const t = ttls[index];
|
|
504
|
+
return !!t && !!s && (cachedNow || getNow()) - s > t;
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
// conditionally set private methods related to TTL
|
|
508
|
+
#updateItemAge = () => { };
|
|
509
|
+
#statusTTL = () => { };
|
|
510
|
+
#setItemTTL = () => { };
|
|
511
|
+
/* c8 ignore stop */
|
|
512
|
+
#isStale = () => false;
|
|
513
|
+
#initializeSizeTracking() {
|
|
514
|
+
const sizes = new ZeroArray(this.#max);
|
|
515
|
+
this.#calculatedSize = 0;
|
|
516
|
+
this.#sizes = sizes;
|
|
517
|
+
this.#removeItemSize = index => {
|
|
518
|
+
this.#calculatedSize -= sizes[index];
|
|
519
|
+
sizes[index] = 0;
|
|
520
|
+
};
|
|
521
|
+
this.#requireSize = (k, v, size, sizeCalculation) => {
|
|
522
|
+
// provisionally accept background fetches.
|
|
523
|
+
// actual value size will be checked when they return.
|
|
524
|
+
if (this.#isBackgroundFetch(v)) {
|
|
525
|
+
return 0;
|
|
526
|
+
}
|
|
527
|
+
if (!isPosInt(size)) {
|
|
528
|
+
if (sizeCalculation) {
|
|
529
|
+
if (typeof sizeCalculation !== 'function') {
|
|
530
|
+
throw new TypeError('sizeCalculation must be a function');
|
|
531
|
+
}
|
|
532
|
+
size = sizeCalculation(v, k);
|
|
533
|
+
if (!isPosInt(size)) {
|
|
534
|
+
throw new TypeError('sizeCalculation return invalid (expect positive integer)');
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
throw new TypeError('invalid size value (must be positive integer). ' +
|
|
539
|
+
'When maxSize or maxEntrySize is used, sizeCalculation ' +
|
|
540
|
+
'or size must be set.');
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return size;
|
|
544
|
+
};
|
|
545
|
+
this.#addItemSize = (index, size, status) => {
|
|
546
|
+
sizes[index] = size;
|
|
547
|
+
if (this.#maxSize) {
|
|
548
|
+
const maxSize = this.#maxSize - sizes[index];
|
|
549
|
+
while (this.#calculatedSize > maxSize) {
|
|
550
|
+
this.#evict(true);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
this.#calculatedSize += sizes[index];
|
|
554
|
+
if (status) {
|
|
555
|
+
status.entrySize = size;
|
|
556
|
+
status.totalCalculatedSize = this.#calculatedSize;
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
#removeItemSize = _i => { };
|
|
561
|
+
#addItemSize = (_i, _s, _st) => { };
|
|
562
|
+
#requireSize = (_k, _v, size, sizeCalculation) => {
|
|
563
|
+
if (size || sizeCalculation) {
|
|
564
|
+
throw new TypeError('cannot set size without setting maxSize or maxEntrySize on cache');
|
|
565
|
+
}
|
|
566
|
+
return 0;
|
|
567
|
+
};
|
|
568
|
+
*#indexes({ allowStale = this.allowStale } = {}) {
|
|
569
|
+
if (this.#size) {
|
|
570
|
+
for (let i = this.#tail; true;) {
|
|
571
|
+
if (!this.#isValidIndex(i)) {
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
if (allowStale || !this.#isStale(i)) {
|
|
575
|
+
yield i;
|
|
576
|
+
}
|
|
577
|
+
if (i === this.#head) {
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
i = this.#prev[i];
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
*#rindexes({ allowStale = this.allowStale } = {}) {
|
|
587
|
+
if (this.#size) {
|
|
588
|
+
for (let i = this.#head; true;) {
|
|
589
|
+
if (!this.#isValidIndex(i)) {
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
if (allowStale || !this.#isStale(i)) {
|
|
593
|
+
yield i;
|
|
594
|
+
}
|
|
595
|
+
if (i === this.#tail) {
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
i = this.#next[i];
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
#isValidIndex(index) {
|
|
605
|
+
return (index !== undefined &&
|
|
606
|
+
this.#keyMap.get(this.#keyList[index]) === index);
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Return a generator yielding `[key, value]` pairs,
|
|
610
|
+
* in order from most recently used to least recently used.
|
|
611
|
+
*/
|
|
612
|
+
*entries() {
|
|
613
|
+
for (const i of this.#indexes()) {
|
|
614
|
+
if (this.#valList[i] !== undefined &&
|
|
615
|
+
this.#keyList[i] !== undefined &&
|
|
616
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
617
|
+
yield [this.#keyList[i], this.#valList[i]];
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Inverse order version of {@link LRUCache.entries}
|
|
623
|
+
*
|
|
624
|
+
* Return a generator yielding `[key, value]` pairs,
|
|
625
|
+
* in order from least recently used to most recently used.
|
|
626
|
+
*/
|
|
627
|
+
*rentries() {
|
|
628
|
+
for (const i of this.#rindexes()) {
|
|
629
|
+
if (this.#valList[i] !== undefined &&
|
|
630
|
+
this.#keyList[i] !== undefined &&
|
|
631
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
632
|
+
yield [this.#keyList[i], this.#valList[i]];
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Return a generator yielding the keys in the cache,
|
|
638
|
+
* in order from most recently used to least recently used.
|
|
639
|
+
*/
|
|
640
|
+
*keys() {
|
|
641
|
+
for (const i of this.#indexes()) {
|
|
642
|
+
const k = this.#keyList[i];
|
|
643
|
+
if (k !== undefined &&
|
|
644
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
645
|
+
yield k;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Inverse order version of {@link LRUCache.keys}
|
|
651
|
+
*
|
|
652
|
+
* Return a generator yielding the keys in the cache,
|
|
653
|
+
* in order from least recently used to most recently used.
|
|
654
|
+
*/
|
|
655
|
+
*rkeys() {
|
|
656
|
+
for (const i of this.#rindexes()) {
|
|
657
|
+
const k = this.#keyList[i];
|
|
658
|
+
if (k !== undefined &&
|
|
659
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
660
|
+
yield k;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Return a generator yielding the values in the cache,
|
|
666
|
+
* in order from most recently used to least recently used.
|
|
667
|
+
*/
|
|
668
|
+
*values() {
|
|
669
|
+
for (const i of this.#indexes()) {
|
|
670
|
+
const v = this.#valList[i];
|
|
671
|
+
if (v !== undefined &&
|
|
672
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
673
|
+
yield this.#valList[i];
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Inverse order version of {@link LRUCache.values}
|
|
679
|
+
*
|
|
680
|
+
* Return a generator yielding the values in the cache,
|
|
681
|
+
* in order from least recently used to most recently used.
|
|
682
|
+
*/
|
|
683
|
+
*rvalues() {
|
|
684
|
+
for (const i of this.#rindexes()) {
|
|
685
|
+
const v = this.#valList[i];
|
|
686
|
+
if (v !== undefined &&
|
|
687
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
688
|
+
yield this.#valList[i];
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Iterating over the cache itself yields the same results as
|
|
694
|
+
* {@link LRUCache.entries}
|
|
695
|
+
*/
|
|
696
|
+
[Symbol.iterator]() {
|
|
697
|
+
return this.entries();
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* A String value that is used in the creation of the default string description of an object.
|
|
701
|
+
* Called by the built-in method Object.prototype.toString.
|
|
702
|
+
*/
|
|
703
|
+
[Symbol.toStringTag] = 'LRUCache';
|
|
704
|
+
/**
|
|
705
|
+
* Find a value for which the supplied fn method returns a truthy value,
|
|
706
|
+
* similar to Array.find(). fn is called as fn(value, key, cache).
|
|
707
|
+
*/
|
|
708
|
+
find(fn, getOptions = {}) {
|
|
709
|
+
for (const i of this.#indexes()) {
|
|
710
|
+
const v = this.#valList[i];
|
|
711
|
+
const value = this.#isBackgroundFetch(v)
|
|
712
|
+
? v.__staleWhileFetching
|
|
713
|
+
: v;
|
|
714
|
+
if (value === undefined)
|
|
715
|
+
continue;
|
|
716
|
+
if (fn(value, this.#keyList[i], this)) {
|
|
717
|
+
return this.get(this.#keyList[i], getOptions);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Call the supplied function on each item in the cache, in order from
|
|
723
|
+
* most recently used to least recently used. fn is called as
|
|
724
|
+
* fn(value, key, cache). Does not update age or recenty of use.
|
|
725
|
+
* Does not iterate over stale values.
|
|
726
|
+
*/
|
|
727
|
+
forEach(fn, thisp = this) {
|
|
728
|
+
for (const i of this.#indexes()) {
|
|
729
|
+
const v = this.#valList[i];
|
|
730
|
+
const value = this.#isBackgroundFetch(v)
|
|
731
|
+
? v.__staleWhileFetching
|
|
732
|
+
: v;
|
|
733
|
+
if (value === undefined)
|
|
734
|
+
continue;
|
|
735
|
+
fn.call(thisp, value, this.#keyList[i], this);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* The same as {@link LRUCache.forEach} but items are iterated over in
|
|
740
|
+
* reverse order. (ie, less recently used items are iterated over first.)
|
|
741
|
+
*/
|
|
742
|
+
rforEach(fn, thisp = this) {
|
|
743
|
+
for (const i of this.#rindexes()) {
|
|
744
|
+
const v = this.#valList[i];
|
|
745
|
+
const value = this.#isBackgroundFetch(v)
|
|
746
|
+
? v.__staleWhileFetching
|
|
747
|
+
: v;
|
|
748
|
+
if (value === undefined)
|
|
749
|
+
continue;
|
|
750
|
+
fn.call(thisp, value, this.#keyList[i], this);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Delete any stale entries. Returns true if anything was removed,
|
|
755
|
+
* false otherwise.
|
|
756
|
+
*/
|
|
757
|
+
purgeStale() {
|
|
758
|
+
let deleted = false;
|
|
759
|
+
for (const i of this.#rindexes({ allowStale: true })) {
|
|
760
|
+
if (this.#isStale(i)) {
|
|
761
|
+
this.delete(this.#keyList[i]);
|
|
762
|
+
deleted = true;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
return deleted;
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Get the extended info about a given entry, to get its value, size, and
|
|
769
|
+
* TTL info simultaneously. Like {@link LRUCache#dump}, but just for a
|
|
770
|
+
* single key. Always returns stale values, if their info is found in the
|
|
771
|
+
* cache, so be sure to check for expired TTLs if relevant.
|
|
772
|
+
*/
|
|
773
|
+
info(key) {
|
|
774
|
+
const i = this.#keyMap.get(key);
|
|
775
|
+
if (i === undefined)
|
|
776
|
+
return undefined;
|
|
777
|
+
const v = this.#valList[i];
|
|
778
|
+
const value = this.#isBackgroundFetch(v)
|
|
779
|
+
? v.__staleWhileFetching
|
|
780
|
+
: v;
|
|
781
|
+
if (value === undefined)
|
|
782
|
+
return undefined;
|
|
783
|
+
const entry = { value };
|
|
784
|
+
if (this.#ttls && this.#starts) {
|
|
785
|
+
const ttl = this.#ttls[i];
|
|
786
|
+
const start = this.#starts[i];
|
|
787
|
+
if (ttl && start) {
|
|
788
|
+
const remain = ttl - (perf.now() - start);
|
|
789
|
+
entry.ttl = remain;
|
|
790
|
+
entry.start = Date.now();
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
if (this.#sizes) {
|
|
794
|
+
entry.size = this.#sizes[i];
|
|
795
|
+
}
|
|
796
|
+
return entry;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Return an array of [key, {@link LRUCache.Entry}] tuples which can be
|
|
800
|
+
* passed to cache.load()
|
|
801
|
+
*/
|
|
802
|
+
dump() {
|
|
803
|
+
const arr = [];
|
|
804
|
+
for (const i of this.#indexes({ allowStale: true })) {
|
|
805
|
+
const key = this.#keyList[i];
|
|
806
|
+
const v = this.#valList[i];
|
|
807
|
+
const value = this.#isBackgroundFetch(v)
|
|
808
|
+
? v.__staleWhileFetching
|
|
809
|
+
: v;
|
|
810
|
+
if (value === undefined || key === undefined)
|
|
811
|
+
continue;
|
|
812
|
+
const entry = { value };
|
|
813
|
+
if (this.#ttls && this.#starts) {
|
|
814
|
+
entry.ttl = this.#ttls[i];
|
|
815
|
+
// always dump the start relative to a portable timestamp
|
|
816
|
+
// it's ok for this to be a bit slow, it's a rare operation.
|
|
817
|
+
const age = perf.now() - this.#starts[i];
|
|
818
|
+
entry.start = Math.floor(Date.now() - age);
|
|
819
|
+
}
|
|
820
|
+
if (this.#sizes) {
|
|
821
|
+
entry.size = this.#sizes[i];
|
|
822
|
+
}
|
|
823
|
+
arr.unshift([key, entry]);
|
|
824
|
+
}
|
|
825
|
+
return arr;
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Reset the cache and load in the items in entries in the order listed.
|
|
829
|
+
* Note that the shape of the resulting cache may be different if the
|
|
830
|
+
* same options are not used in both caches.
|
|
831
|
+
*/
|
|
832
|
+
load(arr) {
|
|
833
|
+
this.clear();
|
|
834
|
+
for (const [key, entry] of arr) {
|
|
835
|
+
if (entry.start) {
|
|
836
|
+
// entry.start is a portable timestamp, but we may be using
|
|
837
|
+
// node's performance.now(), so calculate the offset, so that
|
|
838
|
+
// we get the intended remaining TTL, no matter how long it's
|
|
839
|
+
// been on ice.
|
|
840
|
+
//
|
|
841
|
+
// it's ok for this to be a bit slow, it's a rare operation.
|
|
842
|
+
const age = Date.now() - entry.start;
|
|
843
|
+
entry.start = perf.now() - age;
|
|
844
|
+
}
|
|
845
|
+
this.set(key, entry.value, entry);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Add a value to the cache.
|
|
850
|
+
*
|
|
851
|
+
* Note: if `undefined` is specified as a value, this is an alias for
|
|
852
|
+
* {@link LRUCache#delete}
|
|
853
|
+
*/
|
|
854
|
+
set(k, v, setOptions = {}) {
|
|
855
|
+
if (v === undefined) {
|
|
856
|
+
this.delete(k);
|
|
857
|
+
return this;
|
|
858
|
+
}
|
|
859
|
+
const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status, } = setOptions;
|
|
860
|
+
let { noUpdateTTL = this.noUpdateTTL } = setOptions;
|
|
861
|
+
const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation);
|
|
862
|
+
// if the item doesn't fit, don't do anything
|
|
863
|
+
// NB: maxEntrySize set to maxSize by default
|
|
864
|
+
if (this.maxEntrySize && size > this.maxEntrySize) {
|
|
865
|
+
if (status) {
|
|
866
|
+
status.set = 'miss';
|
|
867
|
+
status.maxEntrySizeExceeded = true;
|
|
868
|
+
}
|
|
869
|
+
// have to delete, in case something is there already.
|
|
870
|
+
this.delete(k);
|
|
871
|
+
return this;
|
|
872
|
+
}
|
|
873
|
+
let index = this.#size === 0 ? undefined : this.#keyMap.get(k);
|
|
874
|
+
if (index === undefined) {
|
|
875
|
+
// addition
|
|
876
|
+
index = (this.#size === 0
|
|
877
|
+
? this.#tail
|
|
878
|
+
: this.#free.length !== 0
|
|
879
|
+
? this.#free.pop()
|
|
880
|
+
: this.#size === this.#max
|
|
881
|
+
? this.#evict(false)
|
|
882
|
+
: this.#size);
|
|
883
|
+
this.#keyList[index] = k;
|
|
884
|
+
this.#valList[index] = v;
|
|
885
|
+
this.#keyMap.set(k, index);
|
|
886
|
+
this.#next[this.#tail] = index;
|
|
887
|
+
this.#prev[index] = this.#tail;
|
|
888
|
+
this.#tail = index;
|
|
889
|
+
this.#size++;
|
|
890
|
+
this.#addItemSize(index, size, status);
|
|
891
|
+
if (status)
|
|
892
|
+
status.set = 'add';
|
|
893
|
+
noUpdateTTL = false;
|
|
894
|
+
}
|
|
895
|
+
else {
|
|
896
|
+
// update
|
|
897
|
+
this.#moveToTail(index);
|
|
898
|
+
const oldVal = this.#valList[index];
|
|
899
|
+
if (v !== oldVal) {
|
|
900
|
+
if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
|
|
901
|
+
oldVal.__abortController.abort(new Error('replaced'));
|
|
902
|
+
const { __staleWhileFetching: s } = oldVal;
|
|
903
|
+
if (s !== undefined && !noDisposeOnSet) {
|
|
904
|
+
if (this.#hasDispose) {
|
|
905
|
+
this.#dispose?.(s, k, 'set');
|
|
906
|
+
}
|
|
907
|
+
if (this.#hasDisposeAfter) {
|
|
908
|
+
this.#disposed?.push([s, k, 'set']);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
else if (!noDisposeOnSet) {
|
|
913
|
+
if (this.#hasDispose) {
|
|
914
|
+
this.#dispose?.(oldVal, k, 'set');
|
|
915
|
+
}
|
|
916
|
+
if (this.#hasDisposeAfter) {
|
|
917
|
+
this.#disposed?.push([oldVal, k, 'set']);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
this.#removeItemSize(index);
|
|
921
|
+
this.#addItemSize(index, size, status);
|
|
922
|
+
this.#valList[index] = v;
|
|
923
|
+
if (status) {
|
|
924
|
+
status.set = 'replace';
|
|
925
|
+
const oldValue = oldVal && this.#isBackgroundFetch(oldVal)
|
|
926
|
+
? oldVal.__staleWhileFetching
|
|
927
|
+
: oldVal;
|
|
928
|
+
if (oldValue !== undefined)
|
|
929
|
+
status.oldValue = oldValue;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
else if (status) {
|
|
933
|
+
status.set = 'update';
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
if (ttl !== 0 && !this.#ttls) {
|
|
937
|
+
this.#initializeTTLTracking();
|
|
938
|
+
}
|
|
939
|
+
if (this.#ttls) {
|
|
940
|
+
if (!noUpdateTTL) {
|
|
941
|
+
this.#setItemTTL(index, ttl, start);
|
|
942
|
+
}
|
|
943
|
+
if (status)
|
|
944
|
+
this.#statusTTL(status, index);
|
|
945
|
+
}
|
|
946
|
+
if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
|
|
947
|
+
const dt = this.#disposed;
|
|
948
|
+
let task;
|
|
949
|
+
while ((task = dt?.shift())) {
|
|
950
|
+
this.#disposeAfter?.(...task);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
return this;
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Evict the least recently used item, returning its value or
|
|
957
|
+
* `undefined` if cache is empty.
|
|
958
|
+
*/
|
|
959
|
+
pop() {
|
|
960
|
+
try {
|
|
961
|
+
while (this.#size) {
|
|
962
|
+
const val = this.#valList[this.#head];
|
|
963
|
+
this.#evict(true);
|
|
964
|
+
if (this.#isBackgroundFetch(val)) {
|
|
965
|
+
if (val.__staleWhileFetching) {
|
|
966
|
+
return val.__staleWhileFetching;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
else if (val !== undefined) {
|
|
970
|
+
return val;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
finally {
|
|
975
|
+
if (this.#hasDisposeAfter && this.#disposed) {
|
|
976
|
+
const dt = this.#disposed;
|
|
977
|
+
let task;
|
|
978
|
+
while ((task = dt?.shift())) {
|
|
979
|
+
this.#disposeAfter?.(...task);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
#evict(free) {
|
|
985
|
+
const head = this.#head;
|
|
986
|
+
const k = this.#keyList[head];
|
|
987
|
+
const v = this.#valList[head];
|
|
988
|
+
if (this.#hasFetchMethod && this.#isBackgroundFetch(v)) {
|
|
989
|
+
v.__abortController.abort(new Error('evicted'));
|
|
990
|
+
}
|
|
991
|
+
else if (this.#hasDispose || this.#hasDisposeAfter) {
|
|
992
|
+
if (this.#hasDispose) {
|
|
993
|
+
this.#dispose?.(v, k, 'evict');
|
|
994
|
+
}
|
|
995
|
+
if (this.#hasDisposeAfter) {
|
|
996
|
+
this.#disposed?.push([v, k, 'evict']);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
this.#removeItemSize(head);
|
|
1000
|
+
// if we aren't about to use the index, then null these out
|
|
1001
|
+
if (free) {
|
|
1002
|
+
this.#keyList[head] = undefined;
|
|
1003
|
+
this.#valList[head] = undefined;
|
|
1004
|
+
this.#free.push(head);
|
|
1005
|
+
}
|
|
1006
|
+
if (this.#size === 1) {
|
|
1007
|
+
this.#head = this.#tail = 0;
|
|
1008
|
+
this.#free.length = 0;
|
|
1009
|
+
}
|
|
1010
|
+
else {
|
|
1011
|
+
this.#head = this.#next[head];
|
|
1012
|
+
}
|
|
1013
|
+
this.#keyMap.delete(k);
|
|
1014
|
+
this.#size--;
|
|
1015
|
+
return head;
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* Check if a key is in the cache, without updating the recency of use.
|
|
1019
|
+
* Will return false if the item is stale, even though it is technically
|
|
1020
|
+
* in the cache.
|
|
1021
|
+
*
|
|
1022
|
+
* Will not update item age unless
|
|
1023
|
+
* {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
|
|
1024
|
+
*/
|
|
1025
|
+
has(k, hasOptions = {}) {
|
|
1026
|
+
const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
|
|
1027
|
+
const index = this.#keyMap.get(k);
|
|
1028
|
+
if (index !== undefined) {
|
|
1029
|
+
const v = this.#valList[index];
|
|
1030
|
+
if (this.#isBackgroundFetch(v) &&
|
|
1031
|
+
v.__staleWhileFetching === undefined) {
|
|
1032
|
+
return false;
|
|
1033
|
+
}
|
|
1034
|
+
if (!this.#isStale(index)) {
|
|
1035
|
+
if (updateAgeOnHas) {
|
|
1036
|
+
this.#updateItemAge(index);
|
|
1037
|
+
}
|
|
1038
|
+
if (status) {
|
|
1039
|
+
status.has = 'hit';
|
|
1040
|
+
this.#statusTTL(status, index);
|
|
1041
|
+
}
|
|
1042
|
+
return true;
|
|
1043
|
+
}
|
|
1044
|
+
else if (status) {
|
|
1045
|
+
status.has = 'stale';
|
|
1046
|
+
this.#statusTTL(status, index);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
else if (status) {
|
|
1050
|
+
status.has = 'miss';
|
|
1051
|
+
}
|
|
1052
|
+
return false;
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* Like {@link LRUCache#get} but doesn't update recency or delete stale
|
|
1056
|
+
* items.
|
|
1057
|
+
*
|
|
1058
|
+
* Returns `undefined` if the item is stale, unless
|
|
1059
|
+
* {@link LRUCache.OptionsBase.allowStale} is set.
|
|
1060
|
+
*/
|
|
1061
|
+
peek(k, peekOptions = {}) {
|
|
1062
|
+
const { allowStale = this.allowStale } = peekOptions;
|
|
1063
|
+
const index = this.#keyMap.get(k);
|
|
1064
|
+
if (index === undefined ||
|
|
1065
|
+
(!allowStale && this.#isStale(index))) {
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
const v = this.#valList[index];
|
|
1069
|
+
// either stale and allowed, or forcing a refresh of non-stale value
|
|
1070
|
+
return this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
1071
|
+
}
|
|
1072
|
+
#backgroundFetch(k, index, options, context) {
|
|
1073
|
+
const v = index === undefined ? undefined : this.#valList[index];
|
|
1074
|
+
if (this.#isBackgroundFetch(v)) {
|
|
1075
|
+
return v;
|
|
1076
|
+
}
|
|
1077
|
+
const ac = new AC();
|
|
1078
|
+
const { signal } = options;
|
|
1079
|
+
// when/if our AC signals, then stop listening to theirs.
|
|
1080
|
+
signal?.addEventListener('abort', () => ac.abort(signal.reason), {
|
|
1081
|
+
signal: ac.signal,
|
|
1082
|
+
});
|
|
1083
|
+
const fetchOpts = {
|
|
1084
|
+
signal: ac.signal,
|
|
1085
|
+
options,
|
|
1086
|
+
context,
|
|
1087
|
+
};
|
|
1088
|
+
const cb = (v, updateCache = false) => {
|
|
1089
|
+
const { aborted } = ac.signal;
|
|
1090
|
+
const ignoreAbort = options.ignoreFetchAbort && v !== undefined;
|
|
1091
|
+
if (options.status) {
|
|
1092
|
+
if (aborted && !updateCache) {
|
|
1093
|
+
options.status.fetchAborted = true;
|
|
1094
|
+
options.status.fetchError = ac.signal.reason;
|
|
1095
|
+
if (ignoreAbort)
|
|
1096
|
+
options.status.fetchAbortIgnored = true;
|
|
1097
|
+
}
|
|
1098
|
+
else {
|
|
1099
|
+
options.status.fetchResolved = true;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
if (aborted && !ignoreAbort && !updateCache) {
|
|
1103
|
+
return fetchFail(ac.signal.reason);
|
|
1104
|
+
}
|
|
1105
|
+
// either we didn't abort, and are still here, or we did, and ignored
|
|
1106
|
+
const bf = p;
|
|
1107
|
+
if (this.#valList[index] === p) {
|
|
1108
|
+
if (v === undefined) {
|
|
1109
|
+
if (bf.__staleWhileFetching) {
|
|
1110
|
+
this.#valList[index] = bf.__staleWhileFetching;
|
|
1111
|
+
}
|
|
1112
|
+
else {
|
|
1113
|
+
this.delete(k);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
else {
|
|
1117
|
+
if (options.status)
|
|
1118
|
+
options.status.fetchUpdated = true;
|
|
1119
|
+
this.set(k, v, fetchOpts.options);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
return v;
|
|
1123
|
+
};
|
|
1124
|
+
const eb = (er) => {
|
|
1125
|
+
if (options.status) {
|
|
1126
|
+
options.status.fetchRejected = true;
|
|
1127
|
+
options.status.fetchError = er;
|
|
1128
|
+
}
|
|
1129
|
+
return fetchFail(er);
|
|
1130
|
+
};
|
|
1131
|
+
const fetchFail = (er) => {
|
|
1132
|
+
const { aborted } = ac.signal;
|
|
1133
|
+
const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
|
|
1134
|
+
const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
|
|
1135
|
+
const noDelete = allowStale || options.noDeleteOnFetchRejection;
|
|
1136
|
+
const bf = p;
|
|
1137
|
+
if (this.#valList[index] === p) {
|
|
1138
|
+
// if we allow stale on fetch rejections, then we need to ensure that
|
|
1139
|
+
// the stale value is not removed from the cache when the fetch fails.
|
|
1140
|
+
const del = !noDelete || bf.__staleWhileFetching === undefined;
|
|
1141
|
+
if (del) {
|
|
1142
|
+
this.delete(k);
|
|
1143
|
+
}
|
|
1144
|
+
else if (!allowStaleAborted) {
|
|
1145
|
+
// still replace the *promise* with the stale value,
|
|
1146
|
+
// since we are done with the promise at this point.
|
|
1147
|
+
// leave it untouched if we're still waiting for an
|
|
1148
|
+
// aborted background fetch that hasn't yet returned.
|
|
1149
|
+
this.#valList[index] = bf.__staleWhileFetching;
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
if (allowStale) {
|
|
1153
|
+
if (options.status && bf.__staleWhileFetching !== undefined) {
|
|
1154
|
+
options.status.returnedStale = true;
|
|
1155
|
+
}
|
|
1156
|
+
return bf.__staleWhileFetching;
|
|
1157
|
+
}
|
|
1158
|
+
else if (bf.__returned === bf) {
|
|
1159
|
+
throw er;
|
|
1160
|
+
}
|
|
1161
|
+
};
|
|
1162
|
+
const pcall = (res, rej) => {
|
|
1163
|
+
const fmp = this.#fetchMethod?.(k, v, fetchOpts);
|
|
1164
|
+
if (fmp && fmp instanceof Promise) {
|
|
1165
|
+
fmp.then(v => res(v === undefined ? undefined : v), rej);
|
|
1166
|
+
}
|
|
1167
|
+
// ignored, we go until we finish, regardless.
|
|
1168
|
+
// defer check until we are actually aborting,
|
|
1169
|
+
// so fetchMethod can override.
|
|
1170
|
+
ac.signal.addEventListener('abort', () => {
|
|
1171
|
+
if (!options.ignoreFetchAbort ||
|
|
1172
|
+
options.allowStaleOnFetchAbort) {
|
|
1173
|
+
res(undefined);
|
|
1174
|
+
// when it eventually resolves, update the cache.
|
|
1175
|
+
if (options.allowStaleOnFetchAbort) {
|
|
1176
|
+
res = v => cb(v, true);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
};
|
|
1181
|
+
if (options.status)
|
|
1182
|
+
options.status.fetchDispatched = true;
|
|
1183
|
+
const p = new Promise(pcall).then(cb, eb);
|
|
1184
|
+
const bf = Object.assign(p, {
|
|
1185
|
+
__abortController: ac,
|
|
1186
|
+
__staleWhileFetching: v,
|
|
1187
|
+
__returned: undefined,
|
|
1188
|
+
});
|
|
1189
|
+
if (index === undefined) {
|
|
1190
|
+
// internal, don't expose status.
|
|
1191
|
+
this.set(k, bf, { ...fetchOpts.options, status: undefined });
|
|
1192
|
+
index = this.#keyMap.get(k);
|
|
1193
|
+
}
|
|
1194
|
+
else {
|
|
1195
|
+
this.#valList[index] = bf;
|
|
1196
|
+
}
|
|
1197
|
+
return bf;
|
|
1198
|
+
}
|
|
1199
|
+
#isBackgroundFetch(p) {
|
|
1200
|
+
if (!this.#hasFetchMethod)
|
|
1201
|
+
return false;
|
|
1202
|
+
const b = p;
|
|
1203
|
+
return (!!b &&
|
|
1204
|
+
b instanceof Promise &&
|
|
1205
|
+
b.hasOwnProperty('__staleWhileFetching') &&
|
|
1206
|
+
b.__abortController instanceof AC);
|
|
1207
|
+
}
|
|
1208
|
+
async fetch(k, fetchOptions = {}) {
|
|
1209
|
+
const {
|
|
1210
|
+
// get options
|
|
1211
|
+
allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet,
|
|
1212
|
+
// set options
|
|
1213
|
+
ttl = this.ttl, noDisposeOnSet = this.noDisposeOnSet, size = 0, sizeCalculation = this.sizeCalculation, noUpdateTTL = this.noUpdateTTL,
|
|
1214
|
+
// fetch exclusive options
|
|
1215
|
+
noDeleteOnFetchRejection = this.noDeleteOnFetchRejection, allowStaleOnFetchRejection = this.allowStaleOnFetchRejection, ignoreFetchAbort = this.ignoreFetchAbort, allowStaleOnFetchAbort = this.allowStaleOnFetchAbort, context, forceRefresh = false, status, signal, } = fetchOptions;
|
|
1216
|
+
if (!this.#hasFetchMethod) {
|
|
1217
|
+
if (status)
|
|
1218
|
+
status.fetch = 'get';
|
|
1219
|
+
return this.get(k, {
|
|
1220
|
+
allowStale,
|
|
1221
|
+
updateAgeOnGet,
|
|
1222
|
+
noDeleteOnStaleGet,
|
|
1223
|
+
status,
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
const options = {
|
|
1227
|
+
allowStale,
|
|
1228
|
+
updateAgeOnGet,
|
|
1229
|
+
noDeleteOnStaleGet,
|
|
1230
|
+
ttl,
|
|
1231
|
+
noDisposeOnSet,
|
|
1232
|
+
size,
|
|
1233
|
+
sizeCalculation,
|
|
1234
|
+
noUpdateTTL,
|
|
1235
|
+
noDeleteOnFetchRejection,
|
|
1236
|
+
allowStaleOnFetchRejection,
|
|
1237
|
+
allowStaleOnFetchAbort,
|
|
1238
|
+
ignoreFetchAbort,
|
|
1239
|
+
status,
|
|
1240
|
+
signal,
|
|
1241
|
+
};
|
|
1242
|
+
let index = this.#keyMap.get(k);
|
|
1243
|
+
if (index === undefined) {
|
|
1244
|
+
if (status)
|
|
1245
|
+
status.fetch = 'miss';
|
|
1246
|
+
const p = this.#backgroundFetch(k, index, options, context);
|
|
1247
|
+
return (p.__returned = p);
|
|
1248
|
+
}
|
|
1249
|
+
else {
|
|
1250
|
+
// in cache, maybe already fetching
|
|
1251
|
+
const v = this.#valList[index];
|
|
1252
|
+
if (this.#isBackgroundFetch(v)) {
|
|
1253
|
+
const stale = allowStale && v.__staleWhileFetching !== undefined;
|
|
1254
|
+
if (status) {
|
|
1255
|
+
status.fetch = 'inflight';
|
|
1256
|
+
if (stale)
|
|
1257
|
+
status.returnedStale = true;
|
|
1258
|
+
}
|
|
1259
|
+
return stale ? v.__staleWhileFetching : (v.__returned = v);
|
|
1260
|
+
}
|
|
1261
|
+
// if we force a refresh, that means do NOT serve the cached value,
|
|
1262
|
+
// unless we are already in the process of refreshing the cache.
|
|
1263
|
+
const isStale = this.#isStale(index);
|
|
1264
|
+
if (!forceRefresh && !isStale) {
|
|
1265
|
+
if (status)
|
|
1266
|
+
status.fetch = 'hit';
|
|
1267
|
+
this.#moveToTail(index);
|
|
1268
|
+
if (updateAgeOnGet) {
|
|
1269
|
+
this.#updateItemAge(index);
|
|
1270
|
+
}
|
|
1271
|
+
if (status)
|
|
1272
|
+
this.#statusTTL(status, index);
|
|
1273
|
+
return v;
|
|
1274
|
+
}
|
|
1275
|
+
// ok, it is stale or a forced refresh, and not already fetching.
|
|
1276
|
+
// refresh the cache.
|
|
1277
|
+
const p = this.#backgroundFetch(k, index, options, context);
|
|
1278
|
+
const hasStale = p.__staleWhileFetching !== undefined;
|
|
1279
|
+
const staleVal = hasStale && allowStale;
|
|
1280
|
+
if (status) {
|
|
1281
|
+
status.fetch = isStale ? 'stale' : 'refresh';
|
|
1282
|
+
if (staleVal && isStale)
|
|
1283
|
+
status.returnedStale = true;
|
|
1284
|
+
}
|
|
1285
|
+
return staleVal ? p.__staleWhileFetching : (p.__returned = p);
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
/**
|
|
1289
|
+
* Return a value from the cache. Will update the recency of the cache
|
|
1290
|
+
* entry found.
|
|
1291
|
+
*
|
|
1292
|
+
* If the key is not found, get() will return `undefined`.
|
|
1293
|
+
*/
|
|
1294
|
+
get(k, getOptions = {}) {
|
|
1295
|
+
const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status, } = getOptions;
|
|
1296
|
+
const index = this.#keyMap.get(k);
|
|
1297
|
+
if (index !== undefined) {
|
|
1298
|
+
const value = this.#valList[index];
|
|
1299
|
+
const fetching = this.#isBackgroundFetch(value);
|
|
1300
|
+
if (status)
|
|
1301
|
+
this.#statusTTL(status, index);
|
|
1302
|
+
if (this.#isStale(index)) {
|
|
1303
|
+
if (status)
|
|
1304
|
+
status.get = 'stale';
|
|
1305
|
+
// delete only if not an in-flight background fetch
|
|
1306
|
+
if (!fetching) {
|
|
1307
|
+
if (!noDeleteOnStaleGet) {
|
|
1308
|
+
this.delete(k);
|
|
1309
|
+
}
|
|
1310
|
+
if (status && allowStale)
|
|
1311
|
+
status.returnedStale = true;
|
|
1312
|
+
return allowStale ? value : undefined;
|
|
1313
|
+
}
|
|
1314
|
+
else {
|
|
1315
|
+
if (status &&
|
|
1316
|
+
allowStale &&
|
|
1317
|
+
value.__staleWhileFetching !== undefined) {
|
|
1318
|
+
status.returnedStale = true;
|
|
1319
|
+
}
|
|
1320
|
+
return allowStale ? value.__staleWhileFetching : undefined;
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
else {
|
|
1324
|
+
if (status)
|
|
1325
|
+
status.get = 'hit';
|
|
1326
|
+
// if we're currently fetching it, we don't actually have it yet
|
|
1327
|
+
// it's not stale, which means this isn't a staleWhileRefetching.
|
|
1328
|
+
// If it's not stale, and fetching, AND has a __staleWhileFetching
|
|
1329
|
+
// value, then that means the user fetched with {forceRefresh:true},
|
|
1330
|
+
// so it's safe to return that value.
|
|
1331
|
+
if (fetching) {
|
|
1332
|
+
return value.__staleWhileFetching;
|
|
1333
|
+
}
|
|
1334
|
+
this.#moveToTail(index);
|
|
1335
|
+
if (updateAgeOnGet) {
|
|
1336
|
+
this.#updateItemAge(index);
|
|
1337
|
+
}
|
|
1338
|
+
return value;
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
else if (status) {
|
|
1342
|
+
status.get = 'miss';
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
#connect(p, n) {
|
|
1346
|
+
this.#prev[n] = p;
|
|
1347
|
+
this.#next[p] = n;
|
|
1348
|
+
}
|
|
1349
|
+
#moveToTail(index) {
|
|
1350
|
+
// if tail already, nothing to do
|
|
1351
|
+
// if head, move head to next[index]
|
|
1352
|
+
// else
|
|
1353
|
+
// move next[prev[index]] to next[index] (head has no prev)
|
|
1354
|
+
// move prev[next[index]] to prev[index]
|
|
1355
|
+
// prev[index] = tail
|
|
1356
|
+
// next[tail] = index
|
|
1357
|
+
// tail = index
|
|
1358
|
+
if (index !== this.#tail) {
|
|
1359
|
+
if (index === this.#head) {
|
|
1360
|
+
this.#head = this.#next[index];
|
|
1361
|
+
}
|
|
1362
|
+
else {
|
|
1363
|
+
this.#connect(this.#prev[index], this.#next[index]);
|
|
1364
|
+
}
|
|
1365
|
+
this.#connect(this.#tail, index);
|
|
1366
|
+
this.#tail = index;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* Deletes a key out of the cache.
|
|
1371
|
+
* Returns true if the key was deleted, false otherwise.
|
|
1372
|
+
*/
|
|
1373
|
+
delete(k) {
|
|
1374
|
+
let deleted = false;
|
|
1375
|
+
if (this.#size !== 0) {
|
|
1376
|
+
const index = this.#keyMap.get(k);
|
|
1377
|
+
if (index !== undefined) {
|
|
1378
|
+
deleted = true;
|
|
1379
|
+
if (this.#size === 1) {
|
|
1380
|
+
this.clear();
|
|
1381
|
+
}
|
|
1382
|
+
else {
|
|
1383
|
+
this.#removeItemSize(index);
|
|
1384
|
+
const v = this.#valList[index];
|
|
1385
|
+
if (this.#isBackgroundFetch(v)) {
|
|
1386
|
+
v.__abortController.abort(new Error('deleted'));
|
|
1387
|
+
}
|
|
1388
|
+
else if (this.#hasDispose || this.#hasDisposeAfter) {
|
|
1389
|
+
if (this.#hasDispose) {
|
|
1390
|
+
this.#dispose?.(v, k, 'delete');
|
|
1391
|
+
}
|
|
1392
|
+
if (this.#hasDisposeAfter) {
|
|
1393
|
+
this.#disposed?.push([v, k, 'delete']);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
this.#keyMap.delete(k);
|
|
1397
|
+
this.#keyList[index] = undefined;
|
|
1398
|
+
this.#valList[index] = undefined;
|
|
1399
|
+
if (index === this.#tail) {
|
|
1400
|
+
this.#tail = this.#prev[index];
|
|
1401
|
+
}
|
|
1402
|
+
else if (index === this.#head) {
|
|
1403
|
+
this.#head = this.#next[index];
|
|
1404
|
+
}
|
|
1405
|
+
else {
|
|
1406
|
+
const pi = this.#prev[index];
|
|
1407
|
+
this.#next[pi] = this.#next[index];
|
|
1408
|
+
const ni = this.#next[index];
|
|
1409
|
+
this.#prev[ni] = this.#prev[index];
|
|
1410
|
+
}
|
|
1411
|
+
this.#size--;
|
|
1412
|
+
this.#free.push(index);
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
if (this.#hasDisposeAfter && this.#disposed?.length) {
|
|
1417
|
+
const dt = this.#disposed;
|
|
1418
|
+
let task;
|
|
1419
|
+
while ((task = dt?.shift())) {
|
|
1420
|
+
this.#disposeAfter?.(...task);
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
return deleted;
|
|
1424
|
+
}
|
|
1425
|
+
/**
|
|
1426
|
+
* Clear the cache entirely, throwing away all values.
|
|
1427
|
+
*/
|
|
1428
|
+
clear() {
|
|
1429
|
+
for (const index of this.#rindexes({ allowStale: true })) {
|
|
1430
|
+
const v = this.#valList[index];
|
|
1431
|
+
if (this.#isBackgroundFetch(v)) {
|
|
1432
|
+
v.__abortController.abort(new Error('deleted'));
|
|
1433
|
+
}
|
|
1434
|
+
else {
|
|
1435
|
+
const k = this.#keyList[index];
|
|
1436
|
+
if (this.#hasDispose) {
|
|
1437
|
+
this.#dispose?.(v, k, 'delete');
|
|
1438
|
+
}
|
|
1439
|
+
if (this.#hasDisposeAfter) {
|
|
1440
|
+
this.#disposed?.push([v, k, 'delete']);
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
this.#keyMap.clear();
|
|
1445
|
+
this.#valList.fill(undefined);
|
|
1446
|
+
this.#keyList.fill(undefined);
|
|
1447
|
+
if (this.#ttls && this.#starts) {
|
|
1448
|
+
this.#ttls.fill(0);
|
|
1449
|
+
this.#starts.fill(0);
|
|
1450
|
+
}
|
|
1451
|
+
if (this.#sizes) {
|
|
1452
|
+
this.#sizes.fill(0);
|
|
1453
|
+
}
|
|
1454
|
+
this.#head = 0;
|
|
1455
|
+
this.#tail = 0;
|
|
1456
|
+
this.#free.length = 0;
|
|
1457
|
+
this.#calculatedSize = 0;
|
|
1458
|
+
this.#size = 0;
|
|
1459
|
+
if (this.#hasDisposeAfter && this.#disposed) {
|
|
1460
|
+
const dt = this.#disposed;
|
|
1461
|
+
let task;
|
|
1462
|
+
while ((task = dt?.shift())) {
|
|
1463
|
+
this.#disposeAfter?.(...task);
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
|
|
28
1469
|
const lowlight = lowlight$1.createLowlight(lowlight$1.all);
|
|
29
1470
|
lowlight.register("vue", function hljsDefineVue(hljs) {
|
|
30
1471
|
return {
|
|
@@ -91,8 +1532,7 @@ Object.defineProperty(highlighter, "setAutoDetectLang", {
|
|
|
91
1532
|
},
|
|
92
1533
|
});
|
|
93
1534
|
|
|
94
|
-
|
|
95
|
-
const map = {};
|
|
1535
|
+
const map = new LRUCache({ max: 30 });
|
|
96
1536
|
class File {
|
|
97
1537
|
raw;
|
|
98
1538
|
lang;
|
|
@@ -109,7 +1549,7 @@ class File {
|
|
|
109
1549
|
this.lang = lang;
|
|
110
1550
|
Object.defineProperty(this, "__v_skip", { value: true });
|
|
111
1551
|
}
|
|
112
|
-
doSyntax({ autoDetectLang, registerHighlighter }) {
|
|
1552
|
+
doSyntax({ autoDetectLang, registerHighlighter, }) {
|
|
113
1553
|
if (!this.raw || this.hasDoSyntax)
|
|
114
1554
|
return;
|
|
115
1555
|
let hasRegisteredLang = true;
|
|
@@ -243,14 +1683,69 @@ class File {
|
|
|
243
1683
|
}
|
|
244
1684
|
}
|
|
245
1685
|
const getFile = (raw, lang) => {
|
|
246
|
-
const key = raw + "--" + "0.0.
|
|
247
|
-
if (map
|
|
248
|
-
return map
|
|
1686
|
+
const key = raw + "--" + "0.0.5" + "--" + lang;
|
|
1687
|
+
if (map.has(key))
|
|
1688
|
+
return map.get(key);
|
|
249
1689
|
const file = new File(raw, lang);
|
|
250
|
-
map
|
|
1690
|
+
map.set(key, file);
|
|
251
1691
|
return file;
|
|
252
1692
|
};
|
|
253
1693
|
|
|
1694
|
+
const maxLength = 1000;
|
|
1695
|
+
/** Get the maximum position in the range. */
|
|
1696
|
+
function rangeMax(range) {
|
|
1697
|
+
return range.location + range.length;
|
|
1698
|
+
}
|
|
1699
|
+
/** Get the length of the common substring between the two strings. */
|
|
1700
|
+
function commonLength(stringA, rangeA, stringB, rangeB, reverse) {
|
|
1701
|
+
const max = Math.min(rangeA.length, rangeB.length);
|
|
1702
|
+
const startA = reverse ? rangeMax(rangeA) - 1 : rangeA.location;
|
|
1703
|
+
const startB = reverse ? rangeMax(rangeB) - 1 : rangeB.location;
|
|
1704
|
+
const stride = reverse ? -1 : 1;
|
|
1705
|
+
let length = 0;
|
|
1706
|
+
while (Math.abs(length) < max) {
|
|
1707
|
+
if (stringA[startA + length] !== stringB[startB + length]) {
|
|
1708
|
+
break;
|
|
1709
|
+
}
|
|
1710
|
+
length += stride;
|
|
1711
|
+
}
|
|
1712
|
+
return Math.abs(length);
|
|
1713
|
+
}
|
|
1714
|
+
function isInValidString(s) {
|
|
1715
|
+
return s.trim().length === 0 || s.length >= maxLength;
|
|
1716
|
+
}
|
|
1717
|
+
/** Get the changed ranges in the strings, relative to each other. */
|
|
1718
|
+
function relativeChanges(stringA, stringB) {
|
|
1719
|
+
let bRange = { location: 0, length: stringB.length };
|
|
1720
|
+
let aRange = { location: 0, length: stringA.length };
|
|
1721
|
+
const _stringA = stringA.trimEnd();
|
|
1722
|
+
const _stringB = stringB.trimEnd();
|
|
1723
|
+
if (_stringA === _stringB) {
|
|
1724
|
+
return {
|
|
1725
|
+
stringARange: { location: _stringA.length, length: stringA.length - _stringA.length },
|
|
1726
|
+
stringBRange: { location: _stringB.length, length: stringB.length - _stringB.length },
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
if (isInValidString(stringA) || isInValidString(stringB)) {
|
|
1730
|
+
aRange.length = 0;
|
|
1731
|
+
bRange.length = 0;
|
|
1732
|
+
return { stringARange: aRange, stringBRange: bRange };
|
|
1733
|
+
}
|
|
1734
|
+
const prefixLength = commonLength(stringB, bRange, stringA, aRange, false);
|
|
1735
|
+
bRange = {
|
|
1736
|
+
location: bRange.location + prefixLength,
|
|
1737
|
+
length: bRange.length - prefixLength,
|
|
1738
|
+
};
|
|
1739
|
+
aRange = {
|
|
1740
|
+
location: aRange.location + prefixLength,
|
|
1741
|
+
length: aRange.length - prefixLength,
|
|
1742
|
+
};
|
|
1743
|
+
const suffixLength = commonLength(stringB, bRange, stringA, aRange, true);
|
|
1744
|
+
bRange.length -= suffixLength;
|
|
1745
|
+
aRange.length -= suffixLength;
|
|
1746
|
+
return { stringARange: aRange, stringBRange: bRange };
|
|
1747
|
+
}
|
|
1748
|
+
|
|
254
1749
|
/** indicate what a line in the diff represents */
|
|
255
1750
|
var DiffLineType;
|
|
256
1751
|
(function (DiffLineType) {
|
|
@@ -457,61 +1952,25 @@ const numIterator = (num, cb) => {
|
|
|
457
1952
|
}
|
|
458
1953
|
return re;
|
|
459
1954
|
};
|
|
460
|
-
|
|
461
|
-
const
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
1955
|
+
const getLang = (fileName) => {
|
|
1956
|
+
const dotIndex = fileName.lastIndexOf(".");
|
|
1957
|
+
const extension = fileName.slice(dotIndex + 1);
|
|
1958
|
+
return extension;
|
|
1959
|
+
};
|
|
1960
|
+
const getDiffRange = (additions, deletions) => {
|
|
1961
|
+
if (additions.length === deletions.length) {
|
|
1962
|
+
const len = additions.length;
|
|
1963
|
+
for (let i = 0; i < len; i++) {
|
|
1964
|
+
const addition = additions[i];
|
|
1965
|
+
const deletion = deletions[i];
|
|
1966
|
+
const { stringARange, stringBRange } = relativeChanges(addition.text, deletion.text);
|
|
1967
|
+
addition.needRematch = true;
|
|
1968
|
+
addition.range = stringARange;
|
|
1969
|
+
deletion.needRematch = true;
|
|
1970
|
+
deletion.range = stringBRange;
|
|
476
1971
|
}
|
|
477
|
-
length += stride;
|
|
478
|
-
}
|
|
479
|
-
return Math.abs(length);
|
|
480
|
-
}
|
|
481
|
-
function isInValidString(s) {
|
|
482
|
-
return s.trim().length === 0 || s.length >= maxLength;
|
|
483
|
-
}
|
|
484
|
-
/** Get the changed ranges in the strings, relative to each other. */
|
|
485
|
-
function relativeChanges(stringA, stringB) {
|
|
486
|
-
let bRange = { location: 0, length: stringB.length };
|
|
487
|
-
let aRange = { location: 0, length: stringA.length };
|
|
488
|
-
const _stringA = stringA.trimEnd();
|
|
489
|
-
const _stringB = stringB.trimEnd();
|
|
490
|
-
if (_stringA === _stringB) {
|
|
491
|
-
return {
|
|
492
|
-
stringARange: { location: _stringA.length, length: stringA.length - _stringA.length },
|
|
493
|
-
stringBRange: { location: _stringB.length, length: stringB.length - _stringB.length },
|
|
494
|
-
};
|
|
495
|
-
}
|
|
496
|
-
if (isInValidString(stringA) || isInValidString(stringB)) {
|
|
497
|
-
aRange.length = 0;
|
|
498
|
-
bRange.length = 0;
|
|
499
|
-
return { stringARange: aRange, stringBRange: bRange };
|
|
500
1972
|
}
|
|
501
|
-
|
|
502
|
-
bRange = {
|
|
503
|
-
location: bRange.location + prefixLength,
|
|
504
|
-
length: bRange.length - prefixLength,
|
|
505
|
-
};
|
|
506
|
-
aRange = {
|
|
507
|
-
location: aRange.location + prefixLength,
|
|
508
|
-
length: aRange.length - prefixLength,
|
|
509
|
-
};
|
|
510
|
-
const suffixLength = commonLength(stringB, bRange, stringA, aRange, true);
|
|
511
|
-
bRange.length -= suffixLength;
|
|
512
|
-
aRange.length -= suffixLength;
|
|
513
|
-
return { stringARange: aRange, stringBRange: bRange };
|
|
514
|
-
}
|
|
1973
|
+
};
|
|
515
1974
|
|
|
516
1975
|
/* eslint-disable max-lines */
|
|
517
1976
|
// NODE: ALL of the Diff parse logic from desktop, SEE https://github.com/desktop/desktop
|
|
@@ -866,11 +2325,6 @@ const parseInstance = new DiffParser();
|
|
|
866
2325
|
/* eslint-disable max-lines */
|
|
867
2326
|
const composeLen = 40;
|
|
868
2327
|
const idSet = new Set();
|
|
869
|
-
const getLang = (fileName) => {
|
|
870
|
-
const dotIndex = fileName.lastIndexOf(".");
|
|
871
|
-
const extension = fileName.slice(dotIndex + 1);
|
|
872
|
-
return extension;
|
|
873
|
-
};
|
|
874
2328
|
class DiffFile {
|
|
875
2329
|
_oldFileName;
|
|
876
2330
|
_newFileName;
|
|
@@ -1055,22 +2509,12 @@ class DiffFile {
|
|
|
1055
2509
|
deletions.push(line);
|
|
1056
2510
|
}
|
|
1057
2511
|
else {
|
|
1058
|
-
|
|
1059
|
-
const len = additions.length;
|
|
1060
|
-
for (let i = 0; i < len; i++) {
|
|
1061
|
-
const addition = additions[i];
|
|
1062
|
-
const deletion = deletions[i];
|
|
1063
|
-
const { stringARange, stringBRange } = relativeChanges(addition.text, deletion.text);
|
|
1064
|
-
addition.needRematch = true;
|
|
1065
|
-
addition.range = stringARange;
|
|
1066
|
-
deletion.needRematch = true;
|
|
1067
|
-
deletion.range = stringBRange;
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
2512
|
+
getDiffRange(additions, deletions);
|
|
1070
2513
|
additions = [];
|
|
1071
2514
|
deletions = [];
|
|
1072
2515
|
}
|
|
1073
2516
|
});
|
|
2517
|
+
getDiffRange(additions, deletions);
|
|
1074
2518
|
});
|
|
1075
2519
|
});
|
|
1076
2520
|
this.#diffLines = this.#diffListResults
|
|
@@ -1641,6 +3085,24 @@ class DiffFile {
|
|
|
1641
3085
|
this.#clonedInstance.forEach((v) => v());
|
|
1642
3086
|
this.#clonedInstance.clear();
|
|
1643
3087
|
};
|
|
3088
|
+
clear = () => {
|
|
3089
|
+
this._destroy();
|
|
3090
|
+
this.#oldFileResult = null;
|
|
3091
|
+
this.#newFileResult = null;
|
|
3092
|
+
this.#diffLines = null;
|
|
3093
|
+
this.#diffListResults = null;
|
|
3094
|
+
this.#newFileDiffLines = null;
|
|
3095
|
+
this.#oldFileDiffLines = null;
|
|
3096
|
+
this.#newFileLines = null;
|
|
3097
|
+
this.#oldFileLines = null;
|
|
3098
|
+
this.#newFileSyntaxLines = null;
|
|
3099
|
+
this.#oldFileSyntaxLines = null;
|
|
3100
|
+
this.#splitHunksLines = null;
|
|
3101
|
+
this.#splitLeftLines = null;
|
|
3102
|
+
this.#splitRightLines = null;
|
|
3103
|
+
this.#unifiedHunksLines = null;
|
|
3104
|
+
this.#unifiedLines = null;
|
|
3105
|
+
};
|
|
1644
3106
|
}
|
|
1645
3107
|
|
|
1646
3108
|
var DiffFileLineType;
|
|
@@ -1733,6 +3195,47 @@ const useUnmount = (cb, deps) => {
|
|
|
1733
3195
|
React.useEffect(() => ref.current, deps);
|
|
1734
3196
|
};
|
|
1735
3197
|
|
|
3198
|
+
const isClient = typeof window !== "undefined";
|
|
3199
|
+
const useSafeLayout = isClient ? React.useLayoutEffect : React.useEffect;
|
|
3200
|
+
|
|
3201
|
+
let canvasCtx = null;
|
|
3202
|
+
class TextMeasure {
|
|
3203
|
+
#key = "";
|
|
3204
|
+
#map = {};
|
|
3205
|
+
#getInstance() {
|
|
3206
|
+
canvasCtx = canvasCtx || document.createElement("canvas").getContext("2d");
|
|
3207
|
+
return canvasCtx;
|
|
3208
|
+
}
|
|
3209
|
+
measure(text, font) {
|
|
3210
|
+
const currentKey = `${font?.fontFamily}-${font?.fontStyle}-${font?.fontSize}-${text}`;
|
|
3211
|
+
if (this.#map[currentKey]) {
|
|
3212
|
+
return this.#map[currentKey];
|
|
3213
|
+
}
|
|
3214
|
+
const instance = this.#getInstance();
|
|
3215
|
+
if (font) {
|
|
3216
|
+
const currentFontKey = `${font.fontFamily}-${font.fontStyle}-${font.fontSize}`;
|
|
3217
|
+
if (this.#key !== currentFontKey) {
|
|
3218
|
+
this.#key = currentFontKey;
|
|
3219
|
+
instance.font = `${font.fontStyle || ""} ${font.fontSize || ""} ${font.fontFamily || ""}`;
|
|
3220
|
+
}
|
|
3221
|
+
}
|
|
3222
|
+
else {
|
|
3223
|
+
instance.font = "";
|
|
3224
|
+
}
|
|
3225
|
+
const textWidth = instance.measureText(text).width;
|
|
3226
|
+
return textWidth;
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
const measureInstance = new TextMeasure();
|
|
3230
|
+
const useTextWidth = ({ text, font, }) => {
|
|
3231
|
+
const [width, setWidth] = React.useState(0);
|
|
3232
|
+
useSafeLayout(() => {
|
|
3233
|
+
const width = measureInstance.measure(text, font);
|
|
3234
|
+
setWidth(width);
|
|
3235
|
+
}, [text, font]);
|
|
3236
|
+
return width;
|
|
3237
|
+
};
|
|
3238
|
+
|
|
1736
3239
|
const useDomWidth = ({ selector, enable }) => {
|
|
1737
3240
|
const [width, setWidth] = React.useState(0);
|
|
1738
3241
|
const { useDiffContext } = useDiffViewContext();
|
|
@@ -1783,9 +3286,6 @@ const DiffViewContext = React.createContext(null);
|
|
|
1783
3286
|
DiffViewContext.displayName = "DiffViewContext";
|
|
1784
3287
|
const useDiffViewContext = () => React.useContext(DiffViewContext);
|
|
1785
3288
|
|
|
1786
|
-
const isClient = typeof window !== "undefined";
|
|
1787
|
-
const useSafeLayout = isClient ? React.useLayoutEffect : React.useEffect;
|
|
1788
|
-
|
|
1789
3289
|
const useSyncHeight = ({ selector, side, enable, }) => {
|
|
1790
3290
|
const { useDiffContext } = useDiffViewContext();
|
|
1791
3291
|
const id = useDiffContext(React.useCallback((s) => s.id, []));
|
|
@@ -2072,12 +3572,13 @@ const DiffSyntax = ({ rawLine, diffLine, operator, syntaxLine, }) => {
|
|
|
2072
3572
|
const DiffContent = ({ diffLine, rawLine, syntaxLine, enableWrap, enableHighlight, }) => {
|
|
2073
3573
|
const isAdded = diffLine?.type === DiffLineType.Add;
|
|
2074
3574
|
const isDelete = diffLine?.type === DiffLineType.Delete;
|
|
3575
|
+
const isMaxLineLengthToIgnoreSyntax = syntaxLine?.nodeList?.length > 150;
|
|
2075
3576
|
return (React__namespace.createElement("div", { className: "diff-line-content-item pl-[2.0em]", "data-val": rawLine, style: {
|
|
2076
3577
|
whiteSpace: enableWrap ? "pre-wrap" : "pre",
|
|
2077
3578
|
wordBreak: enableWrap ? "break-all" : "initial",
|
|
2078
3579
|
} },
|
|
2079
3580
|
React__namespace.createElement("span", { "data-operator": isAdded ? "+" : isDelete ? "-" : undefined, className: "diff-line-content-operator inline-block w-[1.5em] ml-[-1.5em] indent-[0.2em] select-none" }, isAdded ? "+" : isDelete ? "-" : " "),
|
|
2080
|
-
enableHighlight && syntaxLine ? (React__namespace.createElement(DiffSyntax, { operator: isAdded ? "add" : isDelete ? "del" : undefined, rawLine: rawLine, diffLine: diffLine, syntaxLine: syntaxLine })) : (React__namespace.createElement(DiffString, { operator: isAdded ? "add" : isDelete ? "del" : undefined, rawLine: rawLine, diffLine: diffLine }))));
|
|
3581
|
+
enableHighlight && syntaxLine && !isMaxLineLengthToIgnoreSyntax ? (React__namespace.createElement(DiffSyntax, { operator: isAdded ? "add" : isDelete ? "del" : undefined, rawLine: rawLine, diffLine: diffLine, syntaxLine: syntaxLine })) : (React__namespace.createElement(DiffString, { operator: isAdded ? "add" : isDelete ? "del" : undefined, rawLine: rawLine, diffLine: diffLine }))));
|
|
2081
3582
|
};
|
|
2082
3583
|
|
|
2083
3584
|
const DiffWidgetContext = React.createContext(null);
|
|
@@ -2203,12 +3704,12 @@ const onMouseDown$1 = (e) => {
|
|
|
2203
3704
|
return;
|
|
2204
3705
|
}
|
|
2205
3706
|
};
|
|
2206
|
-
const DiffSplitViewTable = ({ side, diffFile }) => {
|
|
3707
|
+
const DiffSplitViewTable = ({ side, diffFile, width }) => {
|
|
2207
3708
|
const className = side === exports.SplitSide.new ? "new-diff-table" : "old-diff-table";
|
|
2208
3709
|
const lines = getSplitContentLines(diffFile);
|
|
2209
3710
|
return (React__namespace.createElement("table", { className: className + " border-collapse w-full", "data-mode": exports.SplitSide[side] },
|
|
2210
3711
|
React__namespace.createElement("colgroup", null,
|
|
2211
|
-
React__namespace.createElement("col", { className: `diff-table-${exports.SplitSide[side]}-num-col
|
|
3712
|
+
React__namespace.createElement("col", { className: `diff-table-${exports.SplitSide[side]}-num-col`, style: { minWidth: Math.round(width) + 25 } }),
|
|
2212
3713
|
React__namespace.createElement("col", { className: `diff-table-${exports.SplitSide[side]}-content-col` })),
|
|
2213
3714
|
React__namespace.createElement("thead", { className: "hidden" },
|
|
2214
3715
|
React__namespace.createElement("tr", null,
|
|
@@ -2229,6 +3730,9 @@ const DiffSplitViewTable = ({ side, diffFile }) => {
|
|
|
2229
3730
|
const DiffSplitViewNormal = React.memo(({ diffFile }) => {
|
|
2230
3731
|
const ref1 = React.useRef(null);
|
|
2231
3732
|
const ref2 = React.useRef(null);
|
|
3733
|
+
const splitLineLength = diffFile.splitLineLength;
|
|
3734
|
+
const { useDiffContext } = useDiffViewContext();
|
|
3735
|
+
const fontSize = useDiffContext(React.useCallback((s) => s.fontSize, []));
|
|
2232
3736
|
shim.useSyncExternalStore(diffFile.subscribe, diffFile.getUpdateCount);
|
|
2233
3737
|
React.useEffect(() => {
|
|
2234
3738
|
const left = ref1.current;
|
|
@@ -2237,61 +3741,27 @@ const DiffSplitViewNormal = React.memo(({ diffFile }) => {
|
|
|
2237
3741
|
return;
|
|
2238
3742
|
return syncScroll(left, right);
|
|
2239
3743
|
}, []);
|
|
3744
|
+
const width = useTextWidth({
|
|
3745
|
+
text: splitLineLength.toString(),
|
|
3746
|
+
font: { fontSize: fontSize + "px", fontFamily: "Menlo, Consolas, monospace" },
|
|
3747
|
+
});
|
|
2240
3748
|
return (React__namespace.createElement("div", { className: "split-diff-view split-diff-view-wrap w-full flex basis-[50%]" },
|
|
2241
3749
|
React__namespace.createElement("div", { className: "old-diff-table-wrapper overflow-auto w-full scrollbar-hide scrollbar-disable", ref: ref1, style: {
|
|
2242
3750
|
overscrollBehaviorX: "none",
|
|
2243
3751
|
fontFamily: "Menlo, Consolas, monospace",
|
|
2244
3752
|
fontSize: "var(--diff-font-size--)",
|
|
2245
3753
|
} },
|
|
2246
|
-
React__namespace.createElement(DiffSplitViewTable, { side: exports.SplitSide.old, diffFile: diffFile })),
|
|
3754
|
+
React__namespace.createElement(DiffSplitViewTable, { side: exports.SplitSide.old, diffFile: diffFile, width: width })),
|
|
2247
3755
|
React__namespace.createElement("div", { className: "diff-split-line w-[1.5px] bg-[#ccc]" }),
|
|
2248
3756
|
React__namespace.createElement("div", { className: "new-diff-table-wrapper overflow-auto w-full scrollbar-hide scrollbar-disable", ref: ref2, style: {
|
|
2249
3757
|
overscrollBehaviorX: "none",
|
|
2250
3758
|
fontFamily: "Menlo, Consolas, monospace",
|
|
2251
3759
|
fontSize: "var(--diff-font-size--)",
|
|
2252
3760
|
} },
|
|
2253
|
-
React__namespace.createElement(DiffSplitViewTable, { side: exports.SplitSide.new, diffFile: diffFile }))));
|
|
3761
|
+
React__namespace.createElement(DiffSplitViewTable, { side: exports.SplitSide.new, diffFile: diffFile, width: width }))));
|
|
2254
3762
|
});
|
|
2255
3763
|
DiffSplitViewNormal.displayName = "DiffSplitViewNormal";
|
|
2256
3764
|
|
|
2257
|
-
let canvasCtx = null;
|
|
2258
|
-
class TextMeasure {
|
|
2259
|
-
#key = "";
|
|
2260
|
-
#map = {};
|
|
2261
|
-
#getInstance() {
|
|
2262
|
-
canvasCtx = canvasCtx || document.createElement("canvas").getContext("2d");
|
|
2263
|
-
return canvasCtx;
|
|
2264
|
-
}
|
|
2265
|
-
measure(text, font) {
|
|
2266
|
-
const currentKey = `${font?.fontFamily}-${font?.fontStyle}-${font?.fontSize}-${text}`;
|
|
2267
|
-
if (this.#map[currentKey]) {
|
|
2268
|
-
return this.#map[currentKey];
|
|
2269
|
-
}
|
|
2270
|
-
const instance = this.#getInstance();
|
|
2271
|
-
if (font) {
|
|
2272
|
-
const currentFontKey = `${font.fontFamily}-${font.fontStyle}-${font.fontSize}`;
|
|
2273
|
-
if (this.#key !== currentFontKey) {
|
|
2274
|
-
this.#key = currentFontKey;
|
|
2275
|
-
instance.font = `${font.fontStyle || ""} ${font.fontSize || ""} ${font.fontFamily || ""}`;
|
|
2276
|
-
}
|
|
2277
|
-
}
|
|
2278
|
-
else {
|
|
2279
|
-
instance.font = "";
|
|
2280
|
-
}
|
|
2281
|
-
const textWidth = instance.measureText(text).width;
|
|
2282
|
-
return textWidth;
|
|
2283
|
-
}
|
|
2284
|
-
}
|
|
2285
|
-
const measureInstance = new TextMeasure();
|
|
2286
|
-
const useTextWidth = ({ text, font, }) => {
|
|
2287
|
-
const [width, setWidth] = React.useState(0);
|
|
2288
|
-
useSafeLayout(() => {
|
|
2289
|
-
const width = measureInstance.measure(text, font);
|
|
2290
|
-
setWidth(width);
|
|
2291
|
-
}, [text, font]);
|
|
2292
|
-
return width;
|
|
2293
|
-
};
|
|
2294
|
-
|
|
2295
3765
|
const _DiffSplitExtendLine = ({ index, diffFile, lineNumber, }) => {
|
|
2296
3766
|
const { useDiffContext } = useDiffViewContext();
|
|
2297
3767
|
// 需要显示的时候才进行方法订阅,可以大幅度提高性能
|
|
@@ -2988,7 +4458,7 @@ const DiffView = (props) => {
|
|
|
2988
4458
|
return null;
|
|
2989
4459
|
return (React__namespace.createElement(InternalDiffView, { key: diffFile.getId(), ...restProps, diffFile: diffFile, diffViewFontSize: restProps.diffViewFontSize || 14 }));
|
|
2990
4460
|
};
|
|
2991
|
-
const version = "0.0.
|
|
4461
|
+
const version = "0.0.5";
|
|
2992
4462
|
|
|
2993
4463
|
exports.DiffView = DiffView;
|
|
2994
4464
|
exports.DiffViewContext = DiffViewContext;
|