Skip to content

Commit 6ffd80b

Browse files
committed
test: add commandkit tests
1 parent 120457d commit 6ffd80b

File tree

16 files changed

+819
-232
lines changed

16 files changed

+819
-232
lines changed

apps/test-bot/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ const commandkit = new CommandKit({
1919

2020
commandkit.setPrefixResolver(() => ['!', '?']);
2121

22-
await client.login(process.env.TOKEN);
22+
await commandkit.start();

apps/test-bot/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"compilerOptions": {
33
"target": "ESNext",
4-
"module": "NodeNext",
5-
"moduleResolution": "nodenext",
4+
"module": "Preserve",
5+
"moduleResolution": "Bundler",
66
"lib": ["ESNext", "DOM"],
77
"jsx": "react",
88
"jsxFactory": "CommandKit.createElement",

demo.mjs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { AsyncLocalStorage } from 'node:async_hooks';
2+
3+
const asyncLocalStorage = new AsyncLocalStorage();
4+
const cache = new Map();
5+
const cacheFnMap = new Map();
6+
7+
// revalidate the cache
8+
function revalidate(key, ...args) {
9+
cache.delete(key);
10+
11+
const cacheFn = cacheFnMap.get(key);
12+
if (!cacheFn) return;
13+
14+
return asyncLocalStorage.run(
15+
{
16+
tag: key,
17+
life: cache.get(key)?.life || 0,
18+
},
19+
() => {
20+
const result = cacheFn(...args);
21+
22+
cache.set(key, {
23+
value: result,
24+
time: Date.now(),
25+
life: asyncLocalStorage.getStore().life || 0,
26+
});
27+
28+
return result;
29+
},
30+
);
31+
}
32+
33+
// tags the current cached function with a name
34+
function cacheTag(name) {
35+
const store = asyncLocalStorage.getStore();
36+
if (store) {
37+
store.tag = name;
38+
}
39+
}
40+
41+
// sets the cache life of the current cached function
42+
function cacheLife(time) {
43+
const store = asyncLocalStorage.getStore();
44+
if (store) {
45+
store.life = time;
46+
}
47+
}
48+
49+
// makes a cached function
50+
function makeCached(fn) {
51+
return (...args) => {
52+
return asyncLocalStorage.run({}, () => {
53+
const result = fn(...args);
54+
const store = asyncLocalStorage.getStore();
55+
56+
if (!store.tag) return result;
57+
58+
const cacheKey = store.tag;
59+
const cached = cache.get(cacheKey);
60+
61+
cacheFnMap.set(cacheKey, fn);
62+
63+
if (cached && Date.now() - cached.time < cached.life) {
64+
return cached.value;
65+
}
66+
67+
cache.set(cacheKey, {
68+
value: result,
69+
time: Date.now(),
70+
life: store.life || 0,
71+
});
72+
73+
return result;
74+
});
75+
};
76+
}
77+
78+
// the cached function
79+
const getData = makeCached((name) => {
80+
cacheTag(`tag:${name}`);
81+
cacheLife(1000);
82+
83+
return Math.random();
84+
});
85+
86+
// get the data
87+
console.log('FOO', getData('foo'), getData('foo')); // should be same
88+
89+
console.log('BAR', getData('bar'), getData('bar')); // should be same but different from above
90+
91+
console.log('FOOBAR', getData('foo'), getData('bar')); // should be different
92+
93+
revalidate('tag:foo');
94+
95+
console.log('FOO', getData('foo'), getData('foo')); // should be different from above

0 commit comments

Comments
 (0)