Created
February 27, 2018 11:03
-
-
Save freddi301/0cda96506d82a00b01f488321c4632fe to your computer and use it in GitHub Desktop.
singleton
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
| export function singleton<T>(builder: () => T): () => T { | |
| let getter = () => { | |
| const value = builder(); | |
| getter = () => value; | |
| return value; | |
| } | |
| return () => getter() | |
| } | |
| const x = singleton(() => { console.log("creating 5"); return 5 }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment