All files / src singletonClockFactory.ts

75% Statements 9/12
50% Branches 1/2
25% Functions 1/4
75% Lines 9/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41    1x     1x                                 1x     1x         3x 1x   1x   3x       1x  
import AbsClock from './AbsClock'
// use whichever clock factory is exported from clockFactories
import ClockFactory from './clockFactories'
 
 
export class SingletonClockFactory0  {
    private constructor() {}
 
    private static theClock : AbsClock  // initially undefined!
 
    public static instance () : AbsClock {
        Iif (SingletonClockFactory0.theClock === undefined) {
            SingletonClockFactory0.theClock 
                = (new ClockFactory).instance()
        };
        return SingletonClockFactory0.theClock
    }
}
 
// the code above depends on the peculiarity that in TS, 
// theClock will be initially undefined.
// here's a solution that doesn't depend on that language property
export class SingletonClockFactory  {
    private constructor() {}
 
    private static isInitialized : boolean = false
 
    private static theClock : AbsClock  
 
    public static instance () : AbsClock {
        if (!(SingletonClockFactory.isInitialized)) {            
            SingletonClockFactory.theClock
                = (new ClockFactory).instance()
            SingletonClockFactory.isInitialized = true
        };
        return SingletonClockFactory.theClock
    }
}
 
export default SingletonClockFactory