forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
151 lines (136 loc) · 4.36 KB
/
index.ts
File metadata and controls
151 lines (136 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { EventType, MODE_SSR } from 'overmind'
const OVERMIND = Symbol('OVERMIND')
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
let nextComponentId = 0
function createMixin(overmind, propsCallback) {
const componentId = nextComponentId++
let componentInstanceId = 0
return {
beforeCreate(this: any) {
if (overmind.mode.mode === MODE_SSR) {
this.overmind = {
state: overmind.state,
actions: overmind.actions,
effects: overmind.effects,
addMutationListener: overmind.addMutationListener,
reaction: overmind.reaction,
}
if (propsCallback) {
Object.assign(
this,
propsCallback({
state: overmind.state,
actions: overmind.actions,
effects: overmind.effects,
})
)
}
} else {
this[OVERMIND] = {
tree: (overmind as any).proxyStateTree.getTrackStateTree(),
componentInstanceId: componentInstanceId++,
onUpdate: (mutations, paths, flushId) => {
this[OVERMIND].currentFlushId = flushId
this[OVERMIND].isUpdating = true
this.$forceUpdate()
},
isUpdating: false,
}
this.overmind = {
state: this[OVERMIND].tree.state,
actions: overmind.actions,
effects: overmind.effects,
addMutationListener: overmind.addMutationListener,
reaction: overmind.reaction,
}
if (propsCallback) {
Object.assign(
this,
propsCallback({
state: this[OVERMIND].tree.state,
actions: overmind.actions,
effects: overmind.effects,
})
)
}
this[OVERMIND].tree.track(this[OVERMIND].onUpdate)
}
},
beforeUpdate(this: any) {
if (overmind.mode.mode === MODE_SSR) return
this[OVERMIND].tree.track(this[OVERMIND].onUpdate)
},
...(IS_PRODUCTION
? {
updated(this: any) {
this[OVERMIND].tree.stopTracking()
},
}
: {
mounted(this: any) {
if (overmind.mode.mode === MODE_SSR) return
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId: this[OVERMIND].componentInstanceId,
name: this.$options.name || '',
paths: Array.from(this[OVERMIND].tree.pathDependencies) as any,
})
},
updated(this: any) {
if (overmind.mode.mode === MODE_SSR) return
this[OVERMIND].tree.stopTracking()
if (this[OVERMIND].isUpdating) {
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId,
componentInstanceId: this[OVERMIND].componentInstanceId,
name: this.$options.name || '',
flushId: this[OVERMIND].currentFlushId,
paths: Array.from(this[OVERMIND].tree.pathDependencies) as any,
})
this[OVERMIND].isUpdating = false
}
},
}),
beforeDestroy(this: any) {
if (overmind.mode.mode === MODE_SSR) return
// @ts-ignore
overmind.proxyStateTree.disposeTree(this[OVERMIND].tree)
if (IS_PRODUCTION) {
return
}
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId,
componentInstanceId: this[OVERMIND].componentInstanceId,
name: this.$options.name || '',
})
},
}
}
export const createPlugin = (overmind) => ({
install(
Vue,
propsCallback = ({ state, actions, effects }) => ({
state,
actions,
effects,
})
) {
Vue.mixin(createMixin(overmind, propsCallback))
},
})
export const createConnect = (overmind) => (...args) => {
let options = args.length === 1 ? args[0] : args[1]
let propsCallback = args.length === 1 ? null : args[0]
if (propsCallback && typeof propsCallback !== 'function') {
throw new Error(
`OVERMIND-VUE: When passing two arguments to "connect", the first has to be a function. You can alternatively only pass a single argument, which is the component`
)
}
return {
...options,
mixins: (options.mixins ? options.mixins : []).concat(
createMixin(overmind, propsCallback)
),
overmind,
} as any
}