File tree Expand file tree Collapse file tree 7 files changed +97
-1
lines changed
demos/server-components/lib Expand file tree Collapse file tree 7 files changed +97
-1
lines changed Original file line number Diff line number Diff line change @@ -146,4 +146,4 @@ Temporary Items
146146.netlify
147147demos /default /.next
148148.parcel-cache
149- lib
149+ plugin / lib
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 ] ) : ''
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments