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
180 lines (162 loc) · 4.96 KB
/
index.ts
File metadata and controls
180 lines (162 loc) · 4.96 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {
ENVIRONMENT,
EventType,
IConfiguration,
IContext,
MODE_SSR,
Overmind,
} from 'overmind'
import { Component, ComponentOptions } from 'vue'
type AnyComponent = ComponentOptions | Component
const OVERMIND = Symbol('OVERMIND')
const IS_PRODUCTION = ENVIRONMENT === 'production'
let nextComponentId = 0
function createMixin(overmind, propsCallback, trackPropsCallback = false) {
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).proxyStateTreeInstance.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,
}
this[OVERMIND].tree.track(this[OVERMIND].onUpdate)
if (propsCallback) {
Object.assign(
this,
propsCallback({
state: this[OVERMIND].tree.state,
actions: overmind.actions,
effects: overmind.effects,
})
)
}
}
},
beforeUpdate(this: any) {
if (overmind.mode.mode === MODE_SSR) return
this[OVERMIND].tree.track(this[OVERMIND].onUpdate)
if (propsCallback && trackPropsCallback) {
Object.assign(
this,
propsCallback({
state: this[OVERMIND].tree.state,
actions: overmind.actions,
effects: overmind.effects,
})
)
}
},
...(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.proxyStateTreeInstance.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 function createConnect<Context extends IContext<any>>(
overmind: Overmind<any>
) {
return <T>(
cb: ((context: Context) => T) | AnyComponent,
component?: AnyComponent
) => {
let options: any = component || cb
let propsCallback = component ? cb : null
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, true)
),
overmind,
} as any
}
}