Last active
December 9, 2020 16:12
-
-
Save gabesullice/9ece0cb24b683f30562f3d1437a5534d to your computer and use it in GitHub Desktop.
This file contains 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
let count = undefined; | |
let startTime = undefined; | |
export class ErrorNotStarted extends Error { | |
constructor() { | |
super("The counter has not been started."); | |
} | |
} | |
export class ErrorAlreadyStarted extends Error { | |
constructor() { | |
super("The counter has already been started."); | |
} | |
} | |
export function next() { | |
ensureStarted(); | |
return count++; | |
} | |
export function start() { | |
ensureStarted(false); | |
startTime = new Date(); | |
return count = 0; | |
} | |
export function timeSinceStart() { | |
ensureStarted(); | |
return startTime; | |
} | |
function ensureStarted(wantStarted = true) { | |
if (startTime === undefined && wantStarted) { | |
throw new ErrorNotStarted(); | |
} | |
if (startTime !== undefined && !wantStarted) { | |
throw new ErrorAlreadyStarted(); | |
} | |
} |
This file contains 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 { start, next, timeSinceStart } from 'counter.js'; | |
// import { count, startTime } from 'counter.js'; // This is a language-level error. | |
function handleStart() { | |
start(); | |
} | |
function handleClick() { | |
next(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment