Passed
Pull Request — master (#555)
by John
03:00
created

src/store/folders.js   A

Complexity

Total Complexity 13
Complexity/F 1.08

Size

Lines of Code 81
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 81
rs 10
wmc 13
mnd 1
bc 1
fnc 12
bpm 0.0833
cpm 1.0833
noi 1
1
/**
2
 * @copyright Copyright (c) 2019 John Molakvoæ <[email protected]>
3
 *
4
 * @author John Molakvoæ <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
import Vue from 'vue'
23
24
const state = {
25
	paths: {},
26
	folders: {}
27
}
28
29
const mutations = {
30
	/**
31
	 * Index folders paths and ids
32
	 *
33
	 * @param {Object} state vuex state
34
	 * @param {Object} data destructuring object
35
	 * @param {number} data.id current folder id
36
	 * @param {Array} data.files list of files
37
	 */
38
	updateFolders(state, { id, files }) {
39
		if (files.length > 0) {
40
			const t0 = performance.now()
0 ignored issues
show
Bug introduced by
The variable performance seems to be never declared. If this is a global, consider adding a /** global: performance */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
			// sort by last modified
42
			const list = files.sort((a, b) => {
43
				return new Date(b.lastmod).getTime() - new Date(a.lastmod).getTime()
44
			})
45
46
			// Set folder list
47
			Vue.set(state.folders, id, list.map(file => file.id))
48
			const t1 = performance.now()
49
			console.debug('perf: updateFolders', `${t1 - t0}ms`)
50
		}
51
	},
52
53
	/**
54
	 * Index folders paths and ids
55
	 *
56
	 * @param {Object} state vuex state
57
	 * @param {Object} data destructuring object
58
	 * @param {string} data.path path of this folder
59
	 * @param {number} data.id id of this folder
60
	 */
61
	addPath(state, { path, id }) {
62
		Vue.set(state.paths, path, id)
63
	}
64
}
65
66
const getters = {
67
	folders: state => state.folders,
68
	folder: state => id => state.folders[id],
69
	folderId: state => path => state.paths[path]
70
}
71
72
const actions = {
73
	/**
74
	 * Update files and folders
75
	 *
76
	 * @param {Object} context vuex context
77
	 * @param {Object} data destructuring object
78
	 * @param {number} data.id current folder id
79
	 * @param {Array} data.files list of files
80
	 * @param {Array} data.folders list of folders
81
	 */
82
	updateFolders(context, { id, files, folders }) {
83
		context.commit('updateFolders', { id, files })
84
85
		// then add each folders path indexes
86
		folders.forEach(folder => context.commit('addPath', { path: folder.filename, id: folder.id }))
87
	},
88
89
	/**
90
	 * Index folders paths and ids
91
	 *
92
	 * @param {Object} context vuex context
93
	 * @param {Object} data destructuring object
94
	 * @param {string} data.path path of this folder
95
	 * @param {number} data.id id of this folder
96
	 */
97
	addPath(context, { path, id }) {
98
		context.commit('addPath', { path, id })
99
	}
100
}
101
102
export default { state, mutations, getters, actions }
103