Skip to content

Instantly share code, notes, and snippets.

@bh3605
Created April 8, 2026 13:34
Show Gist options
  • Select an option

  • Save bh3605/94cfe9b22a4b0d400559ea3e6422f61c to your computer and use it in GitHub Desktop.

Select an option

Save bh3605/94cfe9b22a4b0d400559ea3e6422f61c to your computer and use it in GitHub Desktop.
Singleton service decorator
import { ɵgetInjectableDef } from "@angular/core";
export function Harden(target: any) {
let def = ɵgetInjectableDef(target) as any;
const realFactory = def.factory;
let value = undefined as any | undefined;
def.factory = () => { value = value == undefined ? realFactory() : value; return value; };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment