Skip to content

Instantly share code, notes, and snippets.

@gabesullice
Last active December 9, 2020 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabesullice/9ece0cb24b683f30562f3d1437a5534d to your computer and use it in GitHub Desktop.
Save gabesullice/9ece0cb24b683f30562f3d1437a5534d to your computer and use it in GitHub Desktop.
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();
}
}
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