module.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { forEachValue } from '../util'
  2. export default class Module {
  3. constructor (rawModule, runtime) {
  4. this.runtime = runtime
  5. this._children = Object.create(null)
  6. this._rawModule = rawModule
  7. const rawState = rawModule.state
  8. this.state = (typeof rawState === 'function' ? rawState() : rawState) || {}
  9. }
  10. get namespaced () {
  11. return !!this._rawModule.namespaced
  12. }
  13. addChild (key, module) {
  14. this._children[key] = module
  15. }
  16. removeChild (key) {
  17. delete this._children[key]
  18. }
  19. getChild (key) {
  20. return this._children[key]
  21. }
  22. update (rawModule) {
  23. this._rawModule.namespaced = rawModule.namespaced
  24. if (rawModule.actions) {
  25. this._rawModule.actions = rawModule.actions
  26. }
  27. if (rawModule.mutations) {
  28. this._rawModule.mutations = rawModule.mutations
  29. }
  30. if (rawModule.getters) {
  31. this._rawModule.getters = rawModule.getters
  32. }
  33. }
  34. forEachChild (fn) {
  35. forEachValue(this._children, fn)
  36. }
  37. forEachGetter (fn) {
  38. if (this._rawModule.getters) {
  39. forEachValue(this._rawModule.getters, fn)
  40. }
  41. }
  42. forEachAction (fn) {
  43. if (this._rawModule.actions) {
  44. forEachValue(this._rawModule.actions, fn)
  45. }
  46. }
  47. forEachMutation (fn) {
  48. if (this._rawModule.mutations) {
  49. forEachValue(this._rawModule.mutations, fn)
  50. }
  51. }
  52. }