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
328ef229
Commit
328ef229
authored
Jun 04, 2019
by
Sébastien Chopin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: Refactor store for better lisibility
parent
3f5ae21d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
83 deletions
+84
-83
index.js
store/index.js
+84
-83
No files found.
store/index.js
View file @
328ef229
...
@@ -4,98 +4,99 @@ import { validFeeds } from '~/common/api'
...
@@ -4,98 +4,99 @@ import { validFeeds } from '~/common/api'
import
{
lazy
}
from
'~/common/utils'
import
{
lazy
}
from
'~/common/utils'
import
{
CancelToken
}
from
'axios'
import
{
CancelToken
}
from
'axios'
export
default
{
// Learn more on https://nuxtjs.org/guide/vuex-store
// =================================================
// State
// =================================================
state
:
()
=>
{
const
state
=
{
items
:
{
/* [id: number]: Item */
},
users
:
{
/* [id: string]: User */
},
feeds
:
{
/* [page: number] : [ [id: number] ] */
}
}
validFeeds
.
forEach
((
feed
)
=>
{
state
.
feeds
[
feed
]
=
{}
})
return
state
},
// =================================================
// Actions
// =================================================
actions
:
{
FETCH_FEED
({
commit
,
state
},
{
feed
,
page
,
prefetch
})
{
// Don't priorotize already fetched feeds
if
(
state
.
feeds
[
feed
][
page
]
&&
state
.
feeds
[
feed
][
page
].
length
)
{
prefetch
=
true
}
if
(
!
prefetch
)
{
if
(
this
.
feedCancelSource
)
{
this
.
feedCancelSource
.
cancel
(
'priorotize feed: '
+
feed
+
' page: '
+
page
)
}
this
.
feedCancelSource
=
CancelToken
.
source
()
}
return
lazy
(
// =================================================
(
items
)
=>
{
// State
const
ids
=
items
.
map
(
item
=>
item
.
id
)
// =================================================
commit
(
'SET_FEED'
,
{
feed
,
ids
,
page
})
export
const
state
=
()
=>
{
commit
(
'SET_ITEMS'
,
{
items
})
const
s
=
{
},
items
:
{
()
=>
/* [id: number]: Item */
this
.
$axios
.
$get
(
`/
${
feed
}
/
${
page
}
.json`
,
{
cancelToken
:
this
.
feedCancelSource
&&
this
.
feedCancelSource
.
token
}),
(
state
.
feeds
[
feed
][
page
]
||
[]).
map
(
id
=>
state
.
items
[
id
])
)
},
},
users
:
{
FETCH_ITEM
({
commit
,
state
},
{
id
})
{
/* [id: string]: User */
return
lazy
(
item
=>
commit
(
'SET_ITEM'
,
{
item
}),
()
=>
this
.
$axios
.
$get
(
`/item/
${
id
}
.json`
),
Object
.
assign
({
id
,
loading
:
true
,
comments
:
[]
},
state
.
items
[
id
])
)
},
},
feeds
:
{
/* [page: number] : [ [id: number] ] */
}
}
validFeeds
.
forEach
((
feed
)
=>
{
s
.
feeds
[
feed
]
=
{}
})
return
s
}
FETCH_USER
({
state
,
commit
},
{
id
})
{
// =================================================
return
lazy
(
// Mutations
user
=>
commit
(
'SET_USER'
,
{
id
,
user
}),
// =================================================
()
=>
this
.
$axios
.
$get
(
`/user/
${
id
}
.json`
),
export
const
mutations
=
{
Object
.
assign
({
id
,
loading
:
true
},
state
.
users
[
id
])
SET_FEED
:
(
state
,
{
feed
,
ids
,
page
})
=>
{
)
Vue
.
set
(
state
.
feeds
[
feed
],
page
,
ids
)
},
SET_ITEM
:
(
state
,
{
item
})
=>
{
if
(
item
)
{
Vue
.
set
(
state
.
items
,
item
.
id
,
item
)
}
}
},
},
// =================================================
SET_ITEMS
:
(
state
,
{
items
})
=>
{
// Mutations
items
.
forEach
((
item
)
=>
{
// =================================================
mutations
:
{
SET_FEED
:
(
state
,
{
feed
,
ids
,
page
})
=>
{
Vue
.
set
(
state
.
feeds
[
feed
],
page
,
ids
)
},
SET_ITEM
:
(
state
,
{
item
})
=>
{
if
(
item
)
{
if
(
item
)
{
Vue
.
set
(
state
.
items
,
item
.
id
,
item
)
Vue
.
set
(
state
.
items
,
item
.
id
,
item
)
}
}
},
})
SET_ITEMS
:
(
state
,
{
items
})
=>
{
},
items
.
forEach
((
item
)
=>
{
SET_USER
:
(
state
,
{
id
,
user
})
=>
{
if
(
item
)
{
Vue
.
set
(
state
.
users
,
id
,
user
||
false
)
/* false means user not found */
Vue
.
set
(
state
.
items
,
item
.
id
,
item
)
}
}
}
})
// =================================================
},
// Actions
SET_USER
:
(
state
,
{
id
,
user
})
=>
{
// =================================================
Vue
.
set
(
state
.
users
,
id
,
user
||
false
)
/* false means user not found */
export
const
actions
=
{
FETCH_FEED
({
commit
,
state
},
{
feed
,
page
,
prefetch
})
{
// Don't priorotize already fetched feeds
if
(
state
.
feeds
[
feed
][
page
]
&&
state
.
feeds
[
feed
][
page
].
length
)
{
prefetch
=
true
}
}
if
(
!
prefetch
)
{
if
(
this
.
feedCancelSource
)
{
this
.
feedCancelSource
.
cancel
(
'priorotize feed: '
+
feed
+
' page: '
+
page
)
}
this
.
feedCancelSource
=
CancelToken
.
source
()
}
return
lazy
(
(
items
)
=>
{
const
ids
=
items
.
map
(
item
=>
item
.
id
)
commit
(
'SET_FEED'
,
{
feed
,
ids
,
page
})
commit
(
'SET_ITEMS'
,
{
items
})
},
()
=>
this
.
$axios
.
$get
(
`/
${
feed
}
/
${
page
}
.json`
,
{
cancelToken
:
this
.
feedCancelSource
&&
this
.
feedCancelSource
.
token
}),
(
state
.
feeds
[
feed
][
page
]
||
[]).
map
(
id
=>
state
.
items
[
id
])
)
},
FETCH_ITEM
({
commit
,
state
},
{
id
})
{
return
lazy
(
item
=>
commit
(
'SET_ITEM'
,
{
item
}),
()
=>
this
.
$axios
.
$get
(
`/item/
${
id
}
.json`
),
Object
.
assign
({
id
,
loading
:
true
,
comments
:
[]
},
state
.
items
[
id
])
)
},
FETCH_USER
({
state
,
commit
},
{
id
})
{
return
lazy
(
user
=>
commit
(
'SET_USER'
,
{
id
,
user
}),
()
=>
this
.
$axios
.
$get
(
`/user/
${
id
}
.json`
),
Object
.
assign
({
id
,
loading
:
true
},
state
.
users
[
id
])
)
}
}
}
}
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