Commit b651d998 authored by Evan You's avatar Evan You

split api into separate folder

parent 6ffdc5d3
import Firebase from 'firebase/app' import Firebase from 'firebase/app'
import 'firebase/database' import 'firebase/database'
const config = { export function createAPI ({ config, version }) {
databaseURL: 'https://hacker-news.firebaseio.com' Firebase.initializeApp(config)
return Firebase.database().ref(version)
} }
const version = '/v0'
Firebase.initializeApp(config)
const api = Firebase.database().ref(version)
export default api
\ No newline at end of file
import Firebase from 'firebase' import Firebase from 'firebase'
import LRU from 'lru-cache' import LRU from 'lru-cache'
import { fetchItems } from './api'
let api export function createAPI ({ config, version }) {
const config = { let api
databaseURL: 'https://hacker-news.firebaseio.com' // this piece of code may run multiple times in development mode,
} // so we attach the instantiated API to `process` to avoid duplications
const version = '/v0' if (process.__API__) {
if (process.__API__) {
api = process.__API__ api = process.__API__
} else { } else {
Firebase.initializeApp(config) Firebase.initializeApp(config)
api = process.__API__ = Firebase.database().ref(version) api = process.__API__ = Firebase.database().ref(version)
api.onServer = true api.onServer = true
// fetched item cache // fetched item cache
...@@ -28,6 +26,6 @@ if (process.__API__) { ...@@ -28,6 +26,6 @@ if (process.__API__) {
api.cachedIds[type] = snapshot.val() api.cachedIds[type] = snapshot.val()
}) })
}) })
}
return api
} }
export default api
// this is aliased in webpack config based on server/client build // this is aliased in webpack config based on server/client build
import api from 'create-api' import { createAPI } from 'create-api'
const api = createAPI({
version: '/v0',
config: {
databaseURL: 'https://hacker-news.firebaseio.com'
}
})
// warm the front page cache every 15 min // warm the front page cache every 15 min
// make sure to do this only once across all requests // make sure to do this only once across all requests
if (api.onServer && !api.warmCacheStarted) { if (api.onServer) {
api.warmCacheStarted = true
warmCache() warmCache()
} }
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
<script> <script>
import Spinner from './Spinner.vue' import Spinner from './Spinner.vue'
import Item from './Item.vue' import Item from './Item.vue'
import { watchList } from '../store/api' import { watchList } from '../api'
export default { export default {
name: 'item-list', name: 'item-list',
......
...@@ -2,7 +2,7 @@ import { ...@@ -2,7 +2,7 @@ import {
fetchUser, fetchUser,
fetchItems, fetchItems,
fetchIdsByType fetchIdsByType
} from './api' } from '../api'
export default { export default {
// ensure data for rendering given list type // ensure data for rendering given list type
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment