Я работаю над Vue проектом. Я действительно запутался в том, как выглядит правильный код расщепления кода и спагетти в javascript.
Некоторые из моей команды сказали, что мой код сбивает с толку и говорят мне, чтобы узнать больше о расщеплении кода, чтобы предотвратить код спагетти , Я сделал это этим, взят из магазина Vuex, который я рефакторинг:
import VueCookies from 'vue-cookies'
export default {
state: {
destinations: [],
excluded: [],
isBroadcasting: false,
sent: 0,
fails: 0,
},
mutations: {
/**
* Notify the user that the broadcasting event is started.
*
* @author Donny Pratama <donnypratama1024@gmail.com>
* @param {Object} state Vuex state
* @returns void
*/
START_BROADCASTING(state) {
state.isBroadcasting = true
},
/**
* Set broadcast destinations.
*
* @author Donny Pratama <donnypratama1024@gmail.com>
* @param {Object} state Vuex state
* @param {Array} destinations Collection of destinations
* @returns void
*/
SET_BROADCAST_DESTINATIONS(state, destinations) {
state.destinations = destinations
},
/**
* Set the excluded destinations.
*
* @param {Object} state Vuex state
* @param {Array} excluded Collection of the excluded destinations
*/
SET_BROADCAST_EXCLUSION(state, excluded) {
state.excluded = excluded
},
/**
* Count how many message has been sent.
*
* @author Donny Pratama <donnypratama1024@gmail.com>
* @param {Object} state Vuex state
* @returns void
*/
ON_BROADCAST_SUCCESS(state) {
state.sent++
},
/**
* Count how many messages has been failed to send.
*
* @author Donny Pratama <donnypratama1024@gmail.com>
* @param {Object} state Vuex state
* @returns void
*/
ON_BROADCAST_FAILED(state) {
state.fails++
},
/**
* Notify the user that the broadcast process has been done.
*
* @author Donny Pratama <donnypratama1024@gmail.com>
* @param {Object} state Vuex state
* @returns void
*/
END_BROADCASTING(state) {
state.isBroadcasting = false
}
},
actions: {
/**
* Start broadcasting.
*
* @param {Object} context Vuex context
* @param {String} message Message to be broadcasted
*/
startBroadcast(context, message) {
let destinations = context.dispatch('mapWithout', context.state.excluded)
context.commit('START_BROADCASTING')
context.dispatch('send', { message, destinations })
},
/**
* Make a post data.
*
* @author Donny Pratama <donnypratama1024@gmai.com>
* @param {Object} destination Contact destination
* @returns {Object} { from, to, message, type }
*/
make(payload) {
return {
from: VueCookies.get('active_project').projectPhoneNumber,
message: payload.message,
type: 'text',
to: payload.destination.id._serialized,
}
},
/**
* Map destination without the excluded.
*
* @author Donny Pratama <donnypratama1024@gmail.com>
* @param {Object} context Vuex context
* @param {Array} excluded Array of destination to be excluded
* @returns {Array} destinations
*/
mapWithout(context, excluded) {
return _.without(context.state.destinations, excluded)
},
/**
* Notify when the broadcast process is done via [state.isBroadcasting].
* @param {Object} payload Current index and destinations array
*/
notifyWhenDone(payload) {
if (payload.index === payload.destinations.length) {
context.commit('END_BROADCASTING')
}
},
/**
* Send message to destinations.
*
* @author Donny Pratama <donnypratama1024@gmail.com>
* @param {Object} payload Message and destinations
* @returns void
*/
send(payload) {
payload.destinations.forEach((destination, index) => {
let data = context.dispatch('make', {
message: payload.message,
destination: destination
})
axios.post(`sendMessage`, data)
.then((_) => context.commit('ON_BROADCAST_SUCCESS'))
.catch((_) => context.commit('ON_BROADCAST_FAILED'))
context.dispatch('notifyWhenDone', {
index: index,
destinations: payload.destinations
})
})
},
}
}
Мне нужно ваше мнение, я делаю это правильно?