-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58b71e9
commit 2089d2a
Showing
48 changed files
with
2,378 additions
and
8 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
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
1 change: 1 addition & 0 deletions
1
exercises/06.server-context/01.problem.async-local-storage/.gitignore
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 @@ | ||
node_modules |
3 changes: 3 additions & 0 deletions
3
exercises/06.server-context/01.problem.async-local-storage/.prettierignore
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,3 @@ | ||
node_modules | ||
package-lock.json | ||
built_node_modules |
28 changes: 28 additions & 0 deletions
28
exercises/06.server-context/01.problem.async-local-storage/.prettierrc
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,28 @@ | ||
{ | ||
"arrowParens": "avoid", | ||
"bracketSameLine": false, | ||
"bracketSpacing": true, | ||
"embeddedLanguageFormatting": "auto", | ||
"endOfLine": "lf", | ||
"htmlWhitespaceSensitivity": "css", | ||
"insertPragma": false, | ||
"jsxSingleQuote": false, | ||
"printWidth": 80, | ||
"proseWrap": "always", | ||
"quoteProps": "as-needed", | ||
"requirePragma": false, | ||
"semi": false, | ||
"singleAttributePerLine": false, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "all", | ||
"useTabs": true, | ||
"overrides": [ | ||
{ | ||
"files": ["**/*.json"], | ||
"options": { | ||
"useTabs": false | ||
} | ||
} | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
exercises/06.server-context/01.problem.async-local-storage/README.mdx
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 @@ | ||
# Async Local Storage |
47 changes: 47 additions & 0 deletions
47
exercises/06.server-context/01.problem.async-local-storage/db/ship-api.js
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,47 @@ | ||
import fs from 'node:fs/promises' | ||
|
||
const shipData = JSON.parse( | ||
String(await fs.readFile(new URL('./ships.json', import.meta.url))), | ||
) | ||
|
||
export async function searchShips({ | ||
query, | ||
delay = Math.random() * 200 + 300, | ||
}) { | ||
const endTime = Date.now() + delay | ||
const ships = shipData | ||
.filter(ship => ship.name.toLowerCase().includes(query.toLowerCase())) | ||
.slice(0, 13) | ||
await new Promise(resolve => setTimeout(resolve, endTime - Date.now())) | ||
return { | ||
ships: ships.map(ship => ({ name: ship.name, id: ship.id })), | ||
} | ||
} | ||
|
||
export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) { | ||
const endTime = Date.now() + delay | ||
if (!shipId) { | ||
throw new Error('No shipId provided') | ||
} | ||
const ship = shipData.find(ship => ship.id === shipId) | ||
await new Promise(resolve => setTimeout(resolve, endTime - Date.now())) | ||
if (!ship) { | ||
throw new Error(`No ship with the id "${shipId}"`) | ||
} | ||
return ship | ||
} | ||
|
||
export async function updateShipName({ | ||
shipId, | ||
shipName, | ||
delay = Math.random() * 200 + 300, | ||
}) { | ||
const endTime = Date.now() + delay | ||
const ship = shipData.find(ship => ship.id === shipId) | ||
await new Promise(resolve => setTimeout(resolve, endTime - Date.now())) | ||
if (!ship) { | ||
throw new Error(`No ship with the id "${shipId}"`) | ||
} | ||
ship.name = shipName | ||
return ship | ||
} |
Oops, something went wrong.