Skip to content

Instantly share code, notes, and snippets.

@JCloudYu
Created November 27, 2022 19:18
Show Gist options
  • Save JCloudYu/cbee07716aadb6eb9a92e80b267601db to your computer and use it in GitHub Desktop.
Save JCloudYu/cbee07716aadb6eb9a92e80b267601db to your computer and use it in GitHub Desktop.
A tiny volatile memory storage designed for better typing
import "process";
declare global {
interface IVolatileStorage {
(key:'some-key'):{
definition_1: Buffer;
definition_2: string;
}
}
}
declare global {
interface IVolatileStorage {
<ReturnType extends {}=any>(key:string):ReturnType;
}
}
const DYN_DATA:Map<string, any> = new Map();
const ACCESSOR:IVolatileStorage = function<ReturnType extends {}=any>(key:string='default'):ReturnType {
if ( !DYN_DATA.has(key) ) DYN_DATA.set(key, {});
return DYN_DATA.get(key)!
};
export default ACCESSOR;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment