@graffy/cache 0.15.25-alpha.3 → 0.15.25-alpha.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/package.json +4 -9
- package/index.cjs +0 -96
package/package.json
CHANGED
|
@@ -2,13 +2,8 @@
|
|
|
2
2
|
"name": "@graffy/cache",
|
|
3
3
|
"description": "Graffy module providing in-memory caching.",
|
|
4
4
|
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
-
"version": "0.15.25-alpha.
|
|
6
|
-
"main": "./index.
|
|
7
|
-
"exports": {
|
|
8
|
-
"import": "./index.mjs",
|
|
9
|
-
"require": "./index.cjs"
|
|
10
|
-
},
|
|
11
|
-
"module": "./index.mjs",
|
|
5
|
+
"version": "0.15.25-alpha.5",
|
|
6
|
+
"main": "./index.mjs",
|
|
12
7
|
"types": "./types/index.d.ts",
|
|
13
8
|
"repository": {
|
|
14
9
|
"type": "git",
|
|
@@ -16,7 +11,7 @@
|
|
|
16
11
|
},
|
|
17
12
|
"license": "Apache-2.0",
|
|
18
13
|
"dependencies": {
|
|
19
|
-
"@graffy/common": "0.15.25-alpha.
|
|
20
|
-
"@graffy/stream": "0.15.25-alpha.
|
|
14
|
+
"@graffy/common": "0.15.25-alpha.5",
|
|
15
|
+
"@graffy/stream": "0.15.25-alpha.5"
|
|
21
16
|
}
|
|
22
17
|
}
|
package/index.cjs
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const common = require("@graffy/common");
|
|
3
|
-
const stream = require("@graffy/stream");
|
|
4
|
-
const DEFAULT_MAX_AGE = 6e4;
|
|
5
|
-
const NUM_LAYERS = 3;
|
|
6
|
-
function index({ maxAge = DEFAULT_MAX_AGE } = {}) {
|
|
7
|
-
return (store) => {
|
|
8
|
-
const layers = [];
|
|
9
|
-
let optimisticState = [];
|
|
10
|
-
const optimisticChanges = /* @__PURE__ */ new Set();
|
|
11
|
-
const watcher = common.makeWatcher();
|
|
12
|
-
function addToCache(result = []) {
|
|
13
|
-
const now = Date.now();
|
|
14
|
-
let state;
|
|
15
|
-
if (!layers.length || now - layers[0][0] > maxAge / NUM_LAYERS) {
|
|
16
|
-
state = [];
|
|
17
|
-
layers.unshift([now, state]);
|
|
18
|
-
layers.splice(NUM_LAYERS);
|
|
19
|
-
} else {
|
|
20
|
-
state = layers[0][1];
|
|
21
|
-
}
|
|
22
|
-
common.merge(state, result);
|
|
23
|
-
}
|
|
24
|
-
store.on("read", [], async (query, options, next) => {
|
|
25
|
-
if (options.skipCache)
|
|
26
|
-
return next(query);
|
|
27
|
-
const earliest = Date.now() - maxAge;
|
|
28
|
-
const result = [];
|
|
29
|
-
let miss = false;
|
|
30
|
-
if (optimisticState.length) {
|
|
31
|
-
const { known, unknown } = common.slice(optimisticState, query);
|
|
32
|
-
if (known)
|
|
33
|
-
common.merge(result, known);
|
|
34
|
-
if (!unknown)
|
|
35
|
-
return result;
|
|
36
|
-
query = unknown;
|
|
37
|
-
}
|
|
38
|
-
for (let i = 0; i < layers.length; i++) {
|
|
39
|
-
const [fetchTime, state] = layers[i];
|
|
40
|
-
if (fetchTime < earliest)
|
|
41
|
-
break;
|
|
42
|
-
const { known, unknown } = common.slice(state, query);
|
|
43
|
-
if (known)
|
|
44
|
-
common.merge(result, known);
|
|
45
|
-
query = unknown;
|
|
46
|
-
if (!query)
|
|
47
|
-
break;
|
|
48
|
-
miss = true;
|
|
49
|
-
}
|
|
50
|
-
if (query) {
|
|
51
|
-
miss = true;
|
|
52
|
-
const nextValue = await next(query);
|
|
53
|
-
common.merge(result, nextValue);
|
|
54
|
-
}
|
|
55
|
-
if (miss)
|
|
56
|
-
addToCache(result);
|
|
57
|
-
return result;
|
|
58
|
-
});
|
|
59
|
-
store.on("watch", [], (query, options, next) => {
|
|
60
|
-
const nextStream = next(query);
|
|
61
|
-
if (options.skipCache)
|
|
62
|
-
return nextStream;
|
|
63
|
-
const optiStream = watcher.watch([]);
|
|
64
|
-
const nextStreamCopy = stream.mapStream(nextStream, (change) => {
|
|
65
|
-
addToCache(change);
|
|
66
|
-
return change;
|
|
67
|
-
});
|
|
68
|
-
return common.mergeStreams(optiStream, nextStreamCopy);
|
|
69
|
-
});
|
|
70
|
-
store.on("write", [], async (change, options, next) => {
|
|
71
|
-
if (!options.optimism)
|
|
72
|
-
return next(change);
|
|
73
|
-
optimisticChanges.add(change);
|
|
74
|
-
common.merge(optimisticState, change);
|
|
75
|
-
watcher.write(change);
|
|
76
|
-
try {
|
|
77
|
-
const appliedChange = await next(change);
|
|
78
|
-
addToCache(appliedChange);
|
|
79
|
-
watcher.write(appliedChange);
|
|
80
|
-
return appliedChange;
|
|
81
|
-
} catch (e) {
|
|
82
|
-
const query = common.getKnown(change);
|
|
83
|
-
const undo = await store.read(query);
|
|
84
|
-
watcher.write(undo);
|
|
85
|
-
throw e;
|
|
86
|
-
} finally {
|
|
87
|
-
optimisticChanges.delete(change);
|
|
88
|
-
optimisticState = [];
|
|
89
|
-
for (const oChange of optimisticChanges) {
|
|
90
|
-
common.merge(optimisticState, oChange);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
module.exports = index;
|