@@ -6,8 +6,8 @@ const logRequests = !!process.env.DEBUG_API
66const api = createAPI ( {
77 version : '/v0' ,
88 config : {
9- databaseURL : 'https://hacker-news.firebaseio.com'
10- }
9+ databaseURL : 'https://hacker-news.firebaseio.com' ,
10+ } ,
1111} )
1212
1313// warm the front page cache every 15 min
@@ -16,53 +16,57 @@ if (api.onServer) {
1616 warmCache ( )
1717}
1818
19- function warmCache ( ) {
19+ function warmCache ( ) {
2020 fetchItems ( ( api . cachedIds . top || [ ] ) . slice ( 0 , 30 ) )
2121 setTimeout ( warmCache , 1000 * 60 * 15 )
2222}
2323
24- function fetch ( child ) {
24+ function fetch ( child ) {
2525 logRequests && console . log ( `fetching ${ child } ...` )
2626 const cache = api . cachedItems
2727 if ( cache && cache . has ( child ) ) {
2828 logRequests && console . log ( `cache hit for ${ child } .` )
2929 return Promise . resolve ( cache . get ( child ) )
3030 } else {
3131 return new Promise ( ( resolve , reject ) => {
32- api . child ( child ) . once ( 'value' , snapshot => {
33- const val = snapshot . val ( )
34- // mark the timestamp when this item is cached
35- if ( val ) val . __lastUpdated = Date . now ( )
36- cache && cache . set ( child , val )
37- logRequests && console . log ( `fetched ${ child } .` )
38- resolve ( val )
39- } , reject )
32+ api . child ( child ) . once (
33+ 'value' ,
34+ ( snapshot ) => {
35+ const val = snapshot . val ( )
36+ // mark the timestamp when this item is cached
37+ if ( val ) val . __lastUpdated = Date . now ( )
38+ cache && cache . set ( child , val )
39+ logRequests && console . log ( `fetched ${ child } .` )
40+ resolve ( val )
41+ } ,
42+ reject
43+ )
4044 } )
4145 }
4246}
4347
44- export function fetchIdsByType ( type ) {
48+ export function fetchIdsByType ( type ) {
4549 return api . cachedIds && api . cachedIds [ type ]
4650 ? Promise . resolve ( api . cachedIds [ type ] )
4751 : fetch ( `${ type } stories` )
4852}
4953
50- export function fetchItem ( id ) {
54+ export function fetchItem ( id ) {
5155 return fetch ( `item/${ id } ` )
5256}
5357
54- export function fetchItems ( ids ) {
55- return Promise . all ( ids . map ( id => fetchItem ( id ) ) )
58+ export function fetchItems ( ids ) {
59+ return Promise . all ( ids . map ( ( id ) => fetchItem ( id ) ) )
5660}
5761
58- export function fetchUser ( id ) {
62+ export function fetchUser ( id ) {
5963 return fetch ( `user/${ id } ` )
6064}
6165
62- export function watchList ( type , cb ) {
66+ export function watchList ( type , cb ) {
6367 let first = true
6468 const ref = api . child ( `${ type } stories` )
65- const handler = snapshot => {
69+ const handler = ( snapshot ) => {
6670 if ( first ) {
6771 first = false
6872 } else {
0 commit comments