-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from ieedan/add-stopwatch
feat: Add stopwatch + `typedoc` documentation
- Loading branch information
Showing
12 changed files
with
793 additions
and
181 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ts-blocks": minor | ||
--- | ||
|
||
Add stopwatch utility. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ts-blocks": patch | ||
--- | ||
|
||
Now generates documentation with `typedoc` to be hosted on gh-pages. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: GitHub Pages | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: pnpm/action-setup@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20" | ||
|
||
# we have to do this to load the typedoc plugins | ||
- name: Install Dependencies | ||
run: pnpm install | ||
|
||
- name: Generate Documentation | ||
run: pnpm docs:generate | ||
|
||
- name: Deploy | ||
uses: peaceiris/actions-gh-pages@v4 | ||
if: github.ref == 'refs/heads/main' | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
publish_dir: ./docs |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules | ||
|
||
dist | ||
# out | ||
dist | ||
docs |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { expect, test } from 'vitest'; | ||
import { stopwatch } from './stopwatch'; | ||
|
||
// we add this here so that we have a 0 dependency test | ||
const sleep = async (durationMs: number): Promise<void> => | ||
new Promise((res) => setTimeout(res, durationMs)); | ||
|
||
test('Expect correct elapsed time', async () => { | ||
const w = stopwatch(); | ||
|
||
w.start(); | ||
|
||
await sleep(50); | ||
|
||
expect(w.elapsed()).toBeGreaterThanOrEqual(25); | ||
}); | ||
|
||
test('Expect error while accessing before start', async () => { | ||
const w = stopwatch(); | ||
|
||
expect(w.elapsed).toThrow(); | ||
}); | ||
|
||
test('Expect reset to reset', async () => { | ||
const w = stopwatch(); | ||
|
||
w.start(); | ||
|
||
w.stop(); | ||
|
||
w.elapsed(); | ||
|
||
w.reset(); | ||
|
||
expect(w.elapsed).toThrow(); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/** Allows you to time operations. */ | ||
export type Stopwatch = { | ||
/** Start the stopwatch. | ||
* | ||
* @returns | ||
* | ||
* # Usage | ||
* | ||
* ```ts | ||
* const w = stopwatch(); | ||
* | ||
* w.start(); // start counting | ||
* ``` | ||
*/ | ||
start: () => void; | ||
/** Stop the stopwatch. | ||
* | ||
* @returns | ||
* | ||
* # Usage | ||
* | ||
* ```ts | ||
* const w = stopwatch(); | ||
* | ||
* w.start(); | ||
* | ||
* await sleep(1000); | ||
* | ||
* w.stop(); // stop counting | ||
* | ||
* await sleep(1000); | ||
* | ||
* console.log(w.elapsed()); // 1000 | ||
* ``` | ||
*/ | ||
stop: () => void; | ||
/** Reset the stopwatch. | ||
* | ||
* @returns | ||
* | ||
* # Usage | ||
* | ||
* ```ts | ||
* const w = stopwatch(); | ||
* | ||
* w.start(); | ||
* | ||
* w.stop(); | ||
* | ||
* w.reset(); | ||
* | ||
* w.elapsed(); // Error: "Call `.start()` first!" | ||
* ``` | ||
*/ | ||
reset: () => void; | ||
/** Tries to get the elapsed ms. Throws if the Stopwatch has not been started. | ||
* | ||
* @returns | ||
* | ||
* # Usage | ||
* | ||
* ```ts | ||
* const w = watch(); | ||
* | ||
* w.start(); | ||
* | ||
* await sleep(1000); | ||
* | ||
* // you don't have to call stop before accessing `.elapsed()` | ||
* console.log(w.elapsed()); // 1000 | ||
* ``` | ||
*/ | ||
elapsed: () => number; | ||
}; | ||
|
||
/** Creates a new stopwatch instance. | ||
* | ||
* @returns | ||
* | ||
* # Usage | ||
* ```ts | ||
* const w = stopwatch(); | ||
* | ||
* w.start(); | ||
* | ||
* await sleep(1000); | ||
* | ||
* console.log(w.elapsed()); // 1000 | ||
* ``` | ||
*/ | ||
const stopwatch = (): Stopwatch => { | ||
let startedAt: number | undefined = undefined; | ||
let endedAt: number | undefined = undefined; | ||
|
||
return { | ||
start: () => { | ||
startedAt = Date.now(); | ||
}, | ||
stop: () => { | ||
endedAt = Date.now(); | ||
}, | ||
elapsed: () => { | ||
// if this hasn't been defined its always an error in the users code | ||
if (!startedAt) { | ||
throw new Error('Call `.start()` first!'); | ||
} | ||
|
||
let tempEndedAt = endedAt; | ||
|
||
// if the user hasn't called stop just give them the current time | ||
if (!tempEndedAt) { | ||
tempEndedAt = Date.now(); | ||
} | ||
|
||
return tempEndedAt - startedAt; | ||
}, | ||
reset: () => { | ||
endedAt = undefined; | ||
startedAt = undefined; | ||
}, | ||
}; | ||
}; | ||
|
||
export { stopwatch }; |
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
Oops, something went wrong.