freddi301
2/27/2018 - 11:03 AM

singleton

singleton

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 })