@graffy/cache 0.15.8-alpha.5 → 0.15.10-alpha.2

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