forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvue3.ts
More file actions
198 lines (171 loc) · 5.17 KB
/
vue3.ts
File metadata and controls
198 lines (171 loc) · 5.17 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
ENVIRONMENT,
EventType,
IConfiguration,
MODE_SSR,
Overmind,
} from 'overmind'
import {
Ref,
inject,
ref,
onBeforeUpdate,
onRenderTracked,
onMounted,
onBeforeUnmount,
defineComponent,
provide,
h,
Component,
} from 'vue'
const IS_PRODUCTION = ENVIRONMENT === 'production'
let nextComponentId = 0
export const withOvermind = (
instance: Overmind<IConfiguration>,
Component: Component
) => {
return defineComponent({
setup() {
provide('overmind', instance)
},
render() {
return h(Component)
},
})
}
export interface StateHook<Config extends IConfiguration> {
(): Ref<Overmind<Config>['state']>
<CB extends (state: Overmind<Config>['state']) => object>(
cb: CB
): CB extends (state: Overmind<Config>['state']) => infer O
? O extends object
? Ref<O>
: never
: never
}
export function createStateHook<Config extends IConfiguration>(): StateHook<
Config
> {
const componentId = nextComponentId++
let componentInstanceId = 0
return ((cb: any) => {
const overmindInstance = inject<any>('overmind')
if (overmindInstance.mode.mode === MODE_SSR) {
return cb ? cb(overmindInstance.state) : overmindInstance.state
} else {
const overmindRef = ref<any>({})
const flushIds = ref(-1)
const { value } = overmindRef
const state = ref(
cb ? cb(overmindInstance.state) : overmindInstance.state
)
if (!value.tree) {
value.tree = overmindInstance.proxyStateTree.getTrackStateTree()
value.componentInstanceId = componentInstanceId++
value.onUpdate = (_: any, __: any, flushId: number) => {
value.currentFlushId = flushId
value.isUpdating = true
flushIds.value = flushId
state.value = {
...(cb ? cb(overmindInstance.state) : overmindInstance.state),
}
// this.$forceUpdate()
}
value.isUpdating = false
}
onBeforeUpdate(function(this: any, ...args) {
if (overmindInstance.mode.mode === MODE_SSR) return
value.tree.track(value.onUpdate)
})
onRenderTracked(function(this: any, ...args) {
if (IS_PRODUCTION) {
return
}
if (overmindInstance.isUpdating) {
overmindInstance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId,
componentInstanceId: value.componentInstanceId,
name: '', // this.$options.name || '',
flushId: value.currentFlushId,
paths: Array.from(value.tree.pathDependencies) as any,
})
value.isUpdating = false
}
})
onMounted(() => {
if (IS_PRODUCTION || overmindInstance.mode.mode === MODE_SSR) return
value.tree.stopTracking()
overmindInstance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId: value.componentInstanceId,
name: '', // this.$options.name || '',
paths: Array.from(value.tree.pathDependencies) as any,
})
})
onBeforeUnmount(() => {
if (overmindInstance.mode.mode === MODE_SSR) return
overmindInstance.proxyStateTree.disposeTree(value.tree)
if (IS_PRODUCTION) {
return
}
overmindInstance.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId,
componentInstanceId: value.componentInstanceId,
name: '', // this.$options.name || '',
})
})
value.tree.track(value.onUpdate)
return state
}
}) as any
}
export interface ActionsHook<Config extends IConfiguration> {
(): Ref<Overmind<Config>['actions']>
<CB extends (actions: Overmind<Config>['actions']) => object>(
cb: CB
): CB extends (actions: Overmind<Config>['actions']) => infer O
? O extends object
? Ref<O>
: never
: never
}
export function createActionsHook<Config extends IConfiguration>(): ActionsHook<
Config
> {
return ((cb?: any): Overmind<Config>['actions'] => {
const overmindInstance = inject<any>('overmind')
return cb ? cb(overmindInstance.actions) : overmindInstance.actions
}) as any
}
export interface EffectsHook<Config extends IConfiguration> {
(): Ref<Overmind<Config>['effects']>
<CB extends (effects: Overmind<Config>['effects']) => object>(
cb: CB
): CB extends (effects: Overmind<Config>['effects']) => infer O
? O extends object
? Ref<O>
: never
: never
}
export function createEffectsHook<Config extends IConfiguration>(): EffectsHook<
Config
> {
return ((cb?: any): Overmind<Config>['effects'] => {
const overmindInstance = inject<any>('overmind')
return cb ? cb(overmindInstance.effects) : overmindInstance.effects
}) as any
}
export function createReactionHook<Config extends IConfiguration>() {
return (): Overmind<Config>['reaction'] => {
const overmindInstance = inject<any>('overmind')
return overmindInstance.reaction
}
}
export function createHooks<Config extends IConfiguration>() {
return {
state: createStateHook<Config>(),
actions: createActionsHook<Config>(),
effects: createEffectsHook<Config>(),
reaction: createReactionHook<Config>(),
}
}