Skip to content

Commit 3e834f3

Browse files
authored
ci: add missing dir to rsc demo (#1314)
1 parent 50ca76e commit 3e834f3

File tree

7 files changed

+97
-1
lines changed

7 files changed

+97
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,4 @@ Temporary Items
146146
.netlify
147147
demos/default/.next
148148
.parcel-cache
149-
lib
149+
plugin/lib
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default async function fetchData(type, delay = 0) {
2+
const [res] = await Promise.all([
3+
fetch(`https://hacker-news.firebaseio.com/v0/${type}.json`),
4+
new Promise(res => setTimeout(res, (Math.random()) * delay))
5+
])
6+
if (res.status !== 200) {
7+
throw new Error(`Status ${res.status}`)
8+
}
9+
return res.json()
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import fetchData from './fetch-data'
2+
3+
// hydrate comments based on an array of item ids
4+
export default function fetch(ids) {
5+
return Promise.all(
6+
ids.map(async (id) => {
7+
const val = await fetchData(`item/${id}`)
8+
return {
9+
id: val.id,
10+
user: val.by,
11+
text: val.text,
12+
date: new Date(val.time * 1000).getTime() || 0,
13+
comments: await fetch(val.kids || []),
14+
commentsCount: val.descendants || 0,
15+
}
16+
})
17+
)
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import fetchData from './fetch-data'
2+
3+
export default async function (id) {
4+
const val = await fetchData(`item/${id}`)
5+
if (val) {
6+
return transform(val)
7+
} else {
8+
return null
9+
}
10+
}
11+
12+
export function transform(val) {
13+
return {
14+
id: val.id,
15+
url: val.url || '',
16+
user: val.by,
17+
// time is seconds since epoch, not ms
18+
date: new Date(val.time * 1000).getTime() || 0,
19+
// sometimes `kids` is `undefined`
20+
comments: val.kids || [],
21+
commentsCount: val.descendants || 0,
22+
score: val.score,
23+
title: val.title,
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import fetchData from './fetch-data'
2+
3+
export default async function (
4+
type = 'topstories',
5+
{ page = 1, max = 30 } = {}
6+
) {
7+
const start = max * (page - 1)
8+
const end = start + max
9+
const ids = await fetchData(type)
10+
return ids.slice(start, end)
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import ms from 'ms'
2+
3+
const map = {
4+
s: 'seconds',
5+
ms: 'milliseconds',
6+
m: 'minutes',
7+
h: 'hours',
8+
d: 'days',
9+
}
10+
11+
export default (date) =>
12+
date ? ms(new Date() - date).replace(/[a-z]+/, (str) => ' ' + map[str]) : ''
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const cache = {}
2+
3+
export default function useData(key, fetcher) {
4+
if (!cache[key]) {
5+
let data
6+
let error
7+
let promise
8+
cache[key] = () => {
9+
if (error !== undefined || data !== undefined) return { data, error }
10+
if (!promise) {
11+
promise = fetcher()
12+
.then((r) => (data = r))
13+
// Convert all errors to plain string for serialization
14+
.catch((e) => error = e + '')
15+
}
16+
throw promise
17+
}
18+
}
19+
return cache[key]()
20+
}

0 commit comments

Comments
 (0)