SlimIO / TimeMap

ES6 Map-Like implementation with keys that have a defined timelife
MIT License
3 stars 1 forks source link

has must refresh the internal timestamp if the key exist #49

Closed fraxken closed 3 years ago

fraxken commented 3 years ago

The current .has(key) method has a major flaw because the method return true for a key but it doesn't mean the key will still exist later in time.

In the following example we show the problem (in this example we imagine that "key" already exist in the Map).

const test = new TimeMap(1_000);

async function doTheWork() {
    console.log(test.has("key")); // true
    await new Promise((resolve) => setImmediate(resolve));
    console.log(test.get("key")); // ??
}
doTheWork().catch(console.error);

This is very problematic because in a purely asynchronous code there can be several ticks between a .has and a .get!

Like the get method we should implement a refreshTimestamp option on .has().

It will guarantee that the key exists for 1_000 ms.