vikunja-frontend/src/store/helper.ts

28 lines
951 B
TypeScript
Raw Normal View History

import {LOADING, LOADING_MODULE} from './mutation-types'
2020-05-09 22:08:18 +02:00
/**
* This helper sets the loading state with a 100ms delay to avoid flickering.
*
* @param {*} context The vuex module context.
* @param {null|String} module The module that is loading. This parameter allows components to listen for specific parts of the application loading.
* @param {null|function} loadFunc If not null, this function will be executed instead of the default setting loading.
*/
export const setLoading = (context, module = null, loadFunc = null) => {
2020-05-09 22:08:18 +02:00
const timeout = setTimeout(() => {
if (loadFunc === null) {
context.commit(LOADING, true, {root: true})
context.commit(LOADING_MODULE, module, {root: true})
} else {
loadFunc(true)
}
2020-05-09 22:08:18 +02:00
}, 100)
return () => {
clearTimeout(timeout)
if (loadFunc === null) {
context.commit(LOADING, false, {root: true})
context.commit(LOADING_MODULE, null, {root: true})
} else {
loadFunc(false)
}
2020-05-09 22:08:18 +02:00
}
}