@woosh/meep-engine 2.83.0 → 2.84.0
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
CHANGED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { AbstractAsyncMap } from "./AbstractAsyncMap.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Two-tier key-value storage. Intended to accelerate slow asynchronous
|
|
5
|
-
* storage with a fast but limited (in space and reliability) cache in front.
|
|
6
|
-
*
|
|
7
|
-
* First tier (volatile) is considered to be fast an unreliable,
|
|
8
|
-
* whereas second tier (persisted) is considered to be reliable, but slow.
|
|
9
|
-
*/
|
|
10
|
-
export class CachedAsyncMap extends AbstractAsyncMap {
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @param {AbstractAsyncMap} volatile data here may be lost at any point, this is our "cache"
|
|
14
|
-
* @param {AbstractAsyncMap} persisted data here is stored reliably
|
|
15
|
-
*/
|
|
16
|
-
constructor(volatile, persisted) {
|
|
17
|
-
super();
|
|
18
|
-
|
|
19
|
-
this.__volatile = volatile;
|
|
20
|
-
this.__persisted = persisted;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async get(key) {
|
|
24
|
-
const volatile_value = await this.__volatile.get(key);
|
|
25
|
-
|
|
26
|
-
if (volatile_value !== undefined && volatile_value !== null) {
|
|
27
|
-
return volatile_value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// TODO re-insert into volatile if persisted has a hit
|
|
31
|
-
|
|
32
|
-
return await this.__persisted.get(key);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async set(key, value) {
|
|
36
|
-
const lock = this.__volatile.set(key, value);
|
|
37
|
-
|
|
38
|
-
// don't wait for completion on persisted
|
|
39
|
-
this.__persisted.set(key, value);
|
|
40
|
-
|
|
41
|
-
await lock;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { CachedAsyncMap } from "./CachedAsyncMap.js";
|
|
2
|
-
import { AsyncMapWrapper } from "./AsyncMapWrapper.js";
|
|
3
|
-
|
|
4
|
-
test("'get' retrieves data if it is available in volatile level", async () => {
|
|
5
|
-
|
|
6
|
-
const first = new Map();
|
|
7
|
-
|
|
8
|
-
const sut = new CachedAsyncMap(new AsyncMapWrapper(first), new AsyncMapWrapper());
|
|
9
|
-
|
|
10
|
-
first.set(1, 3);
|
|
11
|
-
|
|
12
|
-
expect(await sut.get(1)).toBe(3);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("'get' retrieves data if it is available in persisted level", async () => {
|
|
16
|
-
|
|
17
|
-
const second = new Map();
|
|
18
|
-
|
|
19
|
-
const sut = new CachedAsyncMap(new AsyncMapWrapper(), new AsyncMapWrapper(second));
|
|
20
|
-
|
|
21
|
-
second.set(1, 3);
|
|
22
|
-
|
|
23
|
-
expect(await sut.get(1)).toBe(3);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("'set' propagates data into volatile level", async () => {
|
|
27
|
-
|
|
28
|
-
const first = new Map();
|
|
29
|
-
|
|
30
|
-
const sut = new CachedAsyncMap(new AsyncMapWrapper(first), new AsyncMapWrapper());
|
|
31
|
-
|
|
32
|
-
await sut.set(1, 3);
|
|
33
|
-
|
|
34
|
-
expect(first.get(1)).toBe(3);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
test("'set' propagates data into persisted level", async () => {
|
|
39
|
-
|
|
40
|
-
const second = new Map();
|
|
41
|
-
|
|
42
|
-
const sut = new CachedAsyncMap(new AsyncMapWrapper(), new AsyncMapWrapper(second));
|
|
43
|
-
|
|
44
|
-
await sut.set(1, 3);
|
|
45
|
-
|
|
46
|
-
expect(second.get(1)).toBe(3);
|
|
47
|
-
});
|