Last active
August 20, 2024 11:38
-
-
Save e-oz/4e1e5209a7058bef0ef5f730ace76653 to your computer and use it in GitHub Desktop.
singletonOnly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { isDevMode } from '@angular/core'; | |
const singletons = new Set<symbol>(); | |
export function singletonOnly(unique: symbol) { | |
if (!isDevMode() || typeof window === 'undefined') { | |
return; | |
} | |
if (singletons.has(unique)) { | |
const msg = unique.toString() + ' MUST BE A SINGLETON! It was instantiated more than once.'; | |
if (typeof alert !== 'undefined') { | |
alert(msg); | |
} else { | |
console?.trace(msg); | |
} | |
} else { | |
singletons.add(unique); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example:
File: account-store.ts