Skip to content

Commit

Permalink
finish exercise 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Feb 23, 2024
1 parent 05b42d8 commit 30b5a30
Show file tree
Hide file tree
Showing 246 changed files with 805,095 additions and 536 deletions.
6 changes: 5 additions & 1 deletion exercises/01.code-splitting/01.problem.lazy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
"preview": "vite preview"
},
"dependencies": {
"@use-gesture/react": "^10.3.0",
"d3-geo": "^3.1.0",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209"
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"react-spring": "^9.7.3",
"topojson-client": "^3.1.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
Expand Down
6 changes: 5 additions & 1 deletion exercises/01.code-splitting/01.solution.lazy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
"preview": "vite preview"
},
"dependencies": {
"@use-gesture/react": "^10.3.0",
"d3-geo": "^3.1.0",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209"
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"react-spring": "^9.7.3",
"topojson-client": "^3.1.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
Expand Down
6 changes: 5 additions & 1 deletion exercises/01.code-splitting/02.problem.eager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
"preview": "vite preview"
},
"dependencies": {
"@use-gesture/react": "^10.3.0",
"d3-geo": "^3.1.0",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209"
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"react-spring": "^9.7.3",
"topojson-client": "^3.1.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
Expand Down
6 changes: 5 additions & 1 deletion exercises/01.code-splitting/02.solution.eager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
"preview": "vite preview"
},
"dependencies": {
"@use-gesture/react": "^10.3.0",
"d3-geo": "^3.1.0",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209"
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"react-spring": "^9.7.3",
"topojson-client": "^3.1.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
"preview": "vite preview"
},
"dependencies": {
"@use-gesture/react": "^10.3.0",
"d3-geo": "^3.1.0",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209"
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"react-spring": "^9.7.3",
"topojson-client": "^3.1.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
"preview": "vite preview"
},
"dependencies": {
"@use-gesture/react": "^10.3.0",
"d3-geo": "^3.1.0",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209"
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"react-spring": "^9.7.3",
"topojson-client": "^3.1.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
Expand Down
28 changes: 28 additions & 0 deletions exercises/02.calculations/01.problem.use-memo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "exercises__sep__02.calculations__sep__01.problem.use-memo",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"comlink": "^4.4.1",
"downshift": "^8.3.2",
"match-sorter": "^6.3.4",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"spin-delay": "^1.2.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"prettier": "^3.0.3",
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}
28 changes: 28 additions & 0 deletions exercises/02.calculations/01.solution.use-memo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "exercises__sep__02.calculations__sep__01.solution.use-memo",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"comlink": "^4.4.1",
"downshift": "^8.3.2",
"match-sorter": "^6.3.4",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"spin-delay": "^1.2.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"prettier": "^3.0.3",
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "exercises__sep__02.calculations__sep__02.problem",
"name": "exercises__sep__02.calculations__sep__02.problem.build",
"private": true,
"version": "0.0.0",
"type": "module",
Expand All @@ -9,10 +9,12 @@
"preview": "vite preview"
},
"dependencies": {
"comlink": "^4.4.1",
"downshift": "^8.3.2",
"match-sorter": "^6.3.4",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"downshift": "^8.3.2",
"match-sorter": "^6.3.4"
"spin-delay": "^1.2.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
Expand Down
28 changes: 28 additions & 0 deletions exercises/02.calculations/02.solution.build/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "exercises__sep__02.calculations__sep__02.solution.build",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"comlink": "^4.4.1",
"downshift": "^8.3.2",
"match-sorter": "^6.3.4",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"spin-delay": "^1.2.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"prettier": "^3.0.3",
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}
28 changes: 28 additions & 0 deletions exercises/02.calculations/03.problem.worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "exercises__sep__02.calculations__sep__03.problem.worker",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"comlink": "^4.4.1",
"downshift": "^8.3.2",
"match-sorter": "^6.3.4",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"spin-delay": "^1.2.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"prettier": "^3.0.3",
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}
26 changes: 0 additions & 26 deletions exercises/02.calculations/03.problem/package.json

This file was deleted.

28 changes: 28 additions & 0 deletions exercises/02.calculations/03.solution.worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "exercises__sep__02.calculations__sep__03.solution.worker",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"comlink": "^4.4.1",
"downshift": "^8.3.2",
"match-sorter": "^6.3.4",
"react": "18.3.0-canary-03d6f7cf0-20240209",
"react-dom": "18.3.0-canary-03d6f7cf0-20240209",
"spin-delay": "^1.2.0"
},
"devDependencies": {
"@types/node": "^20.9.1",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"prettier": "^3.0.3",
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ type Cities = Awaited<ReturnType<typeof searchItems>>

export function App() {
const forceRerender = useForceRerender()

const [inputValue, setInputValue] = useState('')

const { data: allItems, run } = useAsync<Cities>()
const { data: allItems, run, status } = useAsync<Cities>()
useEffect(() => {
run(searchItems(inputValue))
}, [inputValue, run])
const items = (allItems ?? []).slice(0, 500)
const items = allItems ?? []

const {
selectedItem,
Expand Down Expand Up @@ -50,7 +49,11 @@ export function App() {
&#10005;
</button>
</div>
<ul {...getMenuProps()}>
<ul
{...getMenuProps({
style: { opacity: status === 'pending' ? 0.6 : 1 },
})}
>
{items.map((item, index) => {
const isSelected = selectedItem?.id === item.id
const isHighlighted = highlightedIndex === index
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Comlink from 'comlink'
import { matchSorter } from 'match-sorter'
import cities from './us-cities.json'

const allItems = cities.map((city, index) => ({ ...city, id: String(index) }))
export type City = (typeof allItems)[number]

export function searchItems(input: string) {
return matchSorter(allItems, input, { keys: ['name'] }).slice(0, 500)
}

Comlink.expose({ searchItems })
17 changes: 17 additions & 0 deletions exercises/02.calculations/03.solution.worker/src/cities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Comlink from 'comlink'
import { type searchItems as searchItemsWorker } from './filter-cities.worker'

const worker = new Worker(
new URL('./filter-cities.worker.ts', import.meta.url),
{ type: 'module' },
)

type WorkerAPI = {
searchItems: typeof searchItemsWorker
}

const filterCities = Comlink.wrap<WorkerAPI>(worker)

export async function searchItems(input: string) {
return filterCities.searchItems(input)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type AsyncState<DataType> =
}
| {
status: 'pending'
data?: null
error?: null
data?: null | DataType
error?: null | Error
promise: Promise<DataType>
}
| {
Expand Down Expand Up @@ -42,9 +42,8 @@ function asyncReducer<DataType>(
switch (action.type) {
case 'pending': {
return {
...state,
status: 'pending',
data: null,
error: null,
promise: action.promise,
}
}
Expand Down
26 changes: 0 additions & 26 deletions exercises/02.calculations/03.solution/package.json

This file was deleted.

Loading

0 comments on commit 30b5a30

Please sign in to comment.