Skip to content

Commit

Permalink
feeling good about option 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Mar 31, 2024
1 parent 1d6e8b1 commit c448088
Show file tree
Hide file tree
Showing 407 changed files with 17,845 additions and 128 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"react-router-dom",
"react-router"
],
"javascript.preferences.importModuleSpecifierEnding": "js",
"workbench.editorAssociations": {
"*.db": "sqlite-viewer.view"
}
Expand Down
18 changes: 15 additions & 3 deletions exercises/01.exercises/01.problem.ssr/db/ship-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const shipData = JSON.parse(
String(await fs.readFile(new URL('./ships.json', import.meta.url))),
)

const MIN_DELAY = 200
const MAX_DELAY = 500

export async function searchShips({
search,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
const ships = shipData
Expand All @@ -18,7 +21,10 @@ export async function searchShips({
}
}

export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function getShip({
shipId,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
if (!shipId) {
throw new Error('No shipId provided')
Expand All @@ -34,14 +40,20 @@ export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function updateShipName({
shipId,
shipName,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
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}"`)
}
if (shipName.toLowerCase().includes('error')) {
throw new Error('Error updating ship name')
}
if (shipName === ship.name) {
throw new Error('New name is the same as the old name')
}
ship.name = shipName
return ship
}
2 changes: 1 addition & 1 deletion exercises/01.exercises/01.problem.ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"get-port": "^7.1.0",
"react": "0.0.0-experimental-2b036d3f1-20240327",
"react-dom": "0.0.0-experimental-2b036d3f1-20240327",
"react-error-boundary": "^4.0.13",
"react-error-boundary": "npm:@kentcdodds/temp-react-error-boundary@4.0.13",
"react-server-dom-esm": "npm:@kentcdodds/[email protected]"
},
"devDependencies": {
Expand Down
18 changes: 15 additions & 3 deletions exercises/01.exercises/01.solution.ssr/db/ship-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const shipData = JSON.parse(
String(await fs.readFile(new URL('./ships.json', import.meta.url))),
)

const MIN_DELAY = 200
const MAX_DELAY = 500

export async function searchShips({
search,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
const ships = shipData
Expand All @@ -18,7 +21,10 @@ export async function searchShips({
}
}

export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function getShip({
shipId,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
if (!shipId) {
throw new Error('No shipId provided')
Expand All @@ -34,14 +40,20 @@ export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function updateShipName({
shipId,
shipName,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
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}"`)
}
if (shipName.toLowerCase().includes('error')) {
throw new Error('Error updating ship name')
}
if (shipName === ship.name) {
throw new Error('New name is the same as the old name')
}
ship.name = shipName
return ship
}
2 changes: 1 addition & 1 deletion exercises/01.exercises/01.solution.ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"get-port": "^7.1.0",
"react": "0.0.0-experimental-2b036d3f1-20240327",
"react-dom": "0.0.0-experimental-2b036d3f1-20240327",
"react-error-boundary": "^4.0.13",
"react-error-boundary": "npm:@kentcdodds/temp-react-error-boundary@4.0.13",
"react-server-dom-esm": "npm:@kentcdodds/[email protected]"
},
"devDependencies": {
Expand Down
18 changes: 15 additions & 3 deletions exercises/01.exercises/02.problem.server-context/db/ship-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const shipData = JSON.parse(
String(await fs.readFile(new URL('./ships.json', import.meta.url))),
)

const MIN_DELAY = 200
const MAX_DELAY = 500

export async function searchShips({
search,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
const ships = shipData
Expand All @@ -18,7 +21,10 @@ export async function searchShips({
}
}

export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function getShip({
shipId,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
if (!shipId) {
throw new Error('No shipId provided')
Expand All @@ -34,14 +40,20 @@ export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function updateShipName({
shipId,
shipName,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
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}"`)
}
if (shipName.toLowerCase().includes('error')) {
throw new Error('Error updating ship name')
}
if (shipName === ship.name) {
throw new Error('New name is the same as the old name')
}
ship.name = shipName
return ship
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"get-port": "^7.1.0",
"react": "0.0.0-experimental-2b036d3f1-20240327",
"react-dom": "0.0.0-experimental-2b036d3f1-20240327",
"react-error-boundary": "^4.0.13",
"react-error-boundary": "npm:@kentcdodds/temp-react-error-boundary@4.0.13",
"react-server-dom-esm": "npm:@kentcdodds/[email protected]"
},
"devDependencies": {
Expand Down
18 changes: 15 additions & 3 deletions exercises/01.exercises/02.solution.server-context/db/ship-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const shipData = JSON.parse(
String(await fs.readFile(new URL('./ships.json', import.meta.url))),
)

const MIN_DELAY = 200
const MAX_DELAY = 500

export async function searchShips({
search,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
const ships = shipData
Expand All @@ -18,7 +21,10 @@ export async function searchShips({
}
}

export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function getShip({
shipId,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
if (!shipId) {
throw new Error('No shipId provided')
Expand All @@ -34,14 +40,20 @@ export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function updateShipName({
shipId,
shipName,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
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}"`)
}
if (shipName.toLowerCase().includes('error')) {
throw new Error('Error updating ship name')
}
if (shipName === ship.name) {
throw new Error('New name is the same as the old name')
}
ship.name = shipName
return ship
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"get-port": "^7.1.0",
"react": "0.0.0-experimental-2b036d3f1-20240327",
"react-dom": "0.0.0-experimental-2b036d3f1-20240327",
"react-error-boundary": "^4.0.13",
"react-error-boundary": "npm:@kentcdodds/temp-react-error-boundary@4.0.13",
"react-server-dom-esm": "npm:@kentcdodds/[email protected]"
},
"devDependencies": {
Expand Down
18 changes: 15 additions & 3 deletions exercises/01.exercises/03.problem.url/db/ship-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const shipData = JSON.parse(
String(await fs.readFile(new URL('./ships.json', import.meta.url))),
)

const MIN_DELAY = 200
const MAX_DELAY = 500

export async function searchShips({
search,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
const ships = shipData
Expand All @@ -18,7 +21,10 @@ export async function searchShips({
}
}

export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function getShip({
shipId,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
if (!shipId) {
throw new Error('No shipId provided')
Expand All @@ -34,14 +40,20 @@ export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function updateShipName({
shipId,
shipName,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
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}"`)
}
if (shipName.toLowerCase().includes('error')) {
throw new Error('Error updating ship name')
}
if (shipName === ship.name) {
throw new Error('New name is the same as the old name')
}
ship.name = shipName
return ship
}
2 changes: 1 addition & 1 deletion exercises/01.exercises/03.problem.url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"get-port": "^7.1.0",
"react": "0.0.0-experimental-2b036d3f1-20240327",
"react-dom": "0.0.0-experimental-2b036d3f1-20240327",
"react-error-boundary": "^4.0.13",
"react-error-boundary": "npm:@kentcdodds/temp-react-error-boundary@4.0.13",
"react-server-dom-esm": "npm:@kentcdodds/[email protected]"
},
"devDependencies": {
Expand Down
18 changes: 15 additions & 3 deletions exercises/01.exercises/03.solution.url/db/ship-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const shipData = JSON.parse(
String(await fs.readFile(new URL('./ships.json', import.meta.url))),
)

const MIN_DELAY = 200
const MAX_DELAY = 500

export async function searchShips({
search,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
const ships = shipData
Expand All @@ -18,7 +21,10 @@ export async function searchShips({
}
}

export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function getShip({
shipId,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
if (!shipId) {
throw new Error('No shipId provided')
Expand All @@ -34,14 +40,20 @@ export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function updateShipName({
shipId,
shipName,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
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}"`)
}
if (shipName.toLowerCase().includes('error')) {
throw new Error('Error updating ship name')
}
if (shipName === ship.name) {
throw new Error('New name is the same as the old name')
}
ship.name = shipName
return ship
}
2 changes: 1 addition & 1 deletion exercises/01.exercises/03.solution.url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"get-port": "^7.1.0",
"react": "0.0.0-experimental-2b036d3f1-20240327",
"react-dom": "0.0.0-experimental-2b036d3f1-20240327",
"react-error-boundary": "^4.0.13",
"react-error-boundary": "npm:@kentcdodds/temp-react-error-boundary@4.0.13",
"react-server-dom-esm": "npm:@kentcdodds/[email protected]"
},
"devDependencies": {
Expand Down
18 changes: 15 additions & 3 deletions exercises/01.exercises/04.problem.async-components/db/ship-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const shipData = JSON.parse(
String(await fs.readFile(new URL('./ships.json', import.meta.url))),
)

const MIN_DELAY = 200
const MAX_DELAY = 500

export async function searchShips({
search,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
const ships = shipData
Expand All @@ -18,7 +21,10 @@ export async function searchShips({
}
}

export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function getShip({
shipId,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
const endTime = Date.now() + delay
if (!shipId) {
throw new Error('No shipId provided')
Expand All @@ -34,14 +40,20 @@ export async function getShip({ shipId, delay = Math.random() * 200 + 300 }) {
export async function updateShipName({
shipId,
shipName,
delay = Math.random() * 200 + 300,
delay = Math.random() * (MAX_DELAY - MIN_DELAY) + MIN_DELAY,
}) {
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}"`)
}
if (shipName.toLowerCase().includes('error')) {
throw new Error('Error updating ship name')
}
if (shipName === ship.name) {
throw new Error('New name is the same as the old name')
}
ship.name = shipName
return ship
}
Loading

0 comments on commit c448088

Please sign in to comment.