|
import PubSub from '../lib/pubsub.js'; |
|
|
|
|
|
|
|
|
|
export default class Store { |
|
|
|
|
|
|
|
|
|
constructor(params) { |
|
let self = this; |
|
self.actions = {}; |
|
self.mutations = {}; |
|
self.state = {}; |
|
self.status = 'default state'; |
|
self.events = new PubSub(); |
|
if (params.hasOwnProperty('actions')) { |
|
self.actions = params.actions; |
|
} |
|
if (params.hasOwnProperty('mutations')) { |
|
self.mutations = params.mutations; |
|
} |
|
|
|
|
|
self.state = new Proxy((params.state || {}), { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set: function (state, key, value) { |
|
state[key] = value; |
|
console.log(`stateChange: ${key}: ${value}`); |
|
|
|
if(key == 'envsSets'){ |
|
self.events.publish('envsSetChange', self.state); |
|
} |
|
else if(key == 'morphologies'){ |
|
self.events.publish('morphologiesChange', self.state); |
|
} |
|
else if(key == 'agents'){ |
|
self.events.publish('agentsListChange', self.state); |
|
} |
|
else if(key == 'simulationState'){ |
|
self.events.publish('agentsListChange', self.state); |
|
self.events.publish('mainButtonsChange', self.state); |
|
} |
|
else if(key == 'parkourConfig'){ |
|
self.events.publish('parkourConfigChange', self.state); |
|
} |
|
else if(key == 'drawingModeState'){ |
|
self.events.publish('drawingModeChange', self.state); |
|
} |
|
else if(key == 'advancedOptionsState'){ |
|
self.events.publish('advancedOptionsChange', self.state); |
|
} |
|
else if(key == 'language'){ |
|
self.events.publish('globalElementsChange', self.state); |
|
self.events.publish('aboutTabChange', self.state); |
|
} |
|
|
|
if (self.status !== 'mutation') { |
|
console.warn(`You should use a mutation to set ${key}`); |
|
} |
|
return true; |
|
} |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dispatch(actionKey, payload) { |
|
let self = this; |
|
if (typeof self.actions[actionKey] !== 'function') { |
|
console.error(`Action "${actionKey} doesn't exist.`); |
|
return false; |
|
} |
|
console.groupCollapsed(`ACTION: ${actionKey}`); |
|
self.status = 'action'; |
|
self.actions[actionKey](self, payload); |
|
console.groupEnd(); |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commit(mutationKey, payload) { |
|
let self = this; |
|
if (typeof self.mutations[mutationKey] !== 'function') { |
|
console.log(`Mutation "${mutationKey}" doesn't exist`); |
|
return false; |
|
} |
|
self.status = 'mutation'; |
|
let newState = self.mutations[mutationKey](self.state, payload); |
|
self.state = Object.assign(self.state, newState); |
|
self.status = 'resting'; |
|
return true; |
|
} |
|
} |