Files
puter/extensions/example-kv.js
KernelDeimos 70c7aa41ac Reapply "Logs Update" (#1730)
This reverts commit 8f7a68ace6.
2025-10-10 15:01:07 -04:00

31 lines
933 B
JavaScript

const { kv } = extension.import('data');
const { sleep } = extension.import('utilities');
// "kv" is load ready to use before the 'init' event is fired.
extension.on('init', async () => {
kv.set('example-kv-key', 'example-kv-value');
console.log('kv key has', await kv.get('example-kv-key'));
await kv.expire({
key: 'example-kv-key',
ttl: 1000 * 60, // 1 minute
});
// This AIIFE demonstrates how "kv.expire" works.
// We cannot simply "await" this - otherwise we block init!
(async () => {
// wait for 30 seconds...
await sleep(30 * 1000);
console.log('kv key still has value', await kv.get('example-kv-key'));
// wait for 30 more seconds
await sleep(30 * 1000);
// and just a little bit longer
// await sleep(100);
console.log('kv key should no longer have the value', await kv.get('example-kv-key'));
})();
});