Skip to content

Instantly share code, notes, and snippets.

@ilkkakuivanen
Created March 21, 2025 20:42
Show Gist options
  • Save ilkkakuivanen/982e9d4b855399f278a9ce3daa2c1510 to your computer and use it in GitHub Desktop.
Save ilkkakuivanen/982e9d4b855399f278a9ce3daa2c1510 to your computer and use it in GitHub Desktop.
Singleton module
let count: number | null = null;
function init(): void {
count = 0;
}
function increment(): void {
if (count) {
count++;
}
}
function decrement(): void {
if (count) {
count--;
}
}
function getCount(): number {
if (!count) {
throw new Error("Count has not been defined!");
}
return count;
}
export { init, increment, decrement, getCount };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment