Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sample
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
周韬
node-sample
Commits
b651d998
Commit
b651d998
authored
Apr 13, 2017
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
split api into separate folder
parent
6ffdc5d3
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
37 additions
and
38 deletions
+37
-38
create-api-client.js
src/api/create-api-client.js
+3
-8
create-api-server.js
src/api/create-api-server.js
+23
-25
index.js
src/api/index.js
+9
-3
ItemList.vue
src/components/ItemList.vue
+1
-1
actions.js
src/store/actions.js
+1
-1
No files found.
src/
store
/create-api-client.js
→
src/
api
/create-api-client.js
View file @
b651d998
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
src/
store
/create-api-server.js
→
src/
api
/create-api-server.js
View file @
b651d998
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__
)
{
api
=
process
.
__API__
}
else
{
Firebase
.
initializeApp
(
config
)
api
=
process
.
__API__
=
Firebase
.
database
().
ref
(
version
)
if
(
process
.
__API__
)
{
api
.
onServer
=
true
api
=
process
.
__API__
}
else
{
Firebase
.
initializeApp
(
config
)
api
=
process
.
__API__
=
Firebase
.
database
().
ref
(
version
)
api
.
onServer
=
true
// fetched item cache
// fetched item cache
api
.
cachedItems
=
LRU
({
api
.
cachedItems
=
LRU
({
max
:
1000
,
max
:
1000
,
maxAge
:
1000
*
60
*
15
// 15 min cache
maxAge
:
1000
*
60
*
15
// 15 min cache
})
})
// cache the latest story ids
// cache the latest story ids
api
.
cachedIds
=
{}
api
.
cachedIds
=
{}
;[
'top'
,
'new'
,
'show'
,
'ask'
,
'job'
].
forEach
(
type
=>
{
;[
'top'
,
'new'
,
'show'
,
'ask'
,
'job'
].
forEach
(
type
=>
{
api
.
child
(
`
${
type
}
stories`
).
on
(
'value'
,
snapshot
=>
{
api
.
child
(
`
${
type
}
stories`
).
on
(
'value'
,
snapshot
=>
{
api
.
cachedIds
[
type
]
=
snapshot
.
val
()
api
.
cachedIds
[
type
]
=
snapshot
.
val
()
})
})
})
})
}
return
api
}
}
export
default
api
src/
store/api
.js
→
src/
api/index
.js
View file @
b651d998
// 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
()
}
}
...
...
src/components/ItemList.vue
View file @
b651d998
...
@@ -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'
,
...
...
src/store/actions.js
View file @
b651d998
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment