Skip to content

Commit d5abf6b

Browse files
refactor(overmind-vue): move vue3 API to vue3 path
1 parent 2b9a5a3 commit d5abf6b

File tree

1 file changed

+180
-0
lines changed
  • packages/node_modules/overmind-vue/src

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import {
2+
ENVIRONMENT,
3+
EventType,
4+
IConfiguration,
5+
MODE_SSR,
6+
Overmind,
7+
} from 'overmind'
8+
import {
9+
Ref,
10+
inject,
11+
ref,
12+
onBeforeUpdate,
13+
onRenderTracked,
14+
onMounted,
15+
onBeforeUnmount,
16+
} from 'vue'
17+
18+
const IS_PRODUCTION = ENVIRONMENT === 'production'
19+
20+
let nextComponentId = 0
21+
22+
export interface StateHook<Config extends IConfiguration> {
23+
(): Ref<Overmind<Config>['state']>
24+
<CB extends (state: Overmind<Config>['state']) => object>(
25+
cb: CB
26+
): CB extends (state: Overmind<Config>['state']) => infer O
27+
? O extends object
28+
? Ref<O>
29+
: never
30+
: never
31+
}
32+
33+
export function createStateHook<Config extends IConfiguration>(): StateHook<
34+
Config
35+
> {
36+
const componentId = nextComponentId++
37+
let componentInstanceId = 0
38+
return ((cb: any) => {
39+
const overmindInstance = inject<any>('overmind')
40+
41+
if (overmindInstance.mode.mode === MODE_SSR) {
42+
return cb ? cb(overmindInstance.state) : overmindInstance.state
43+
} else {
44+
const overmindRef = ref<any>({})
45+
const flushIds = ref(-1)
46+
const { value } = overmindRef
47+
const state = ref(
48+
cb ? cb(overmindInstance.state) : overmindInstance.state
49+
)
50+
51+
if (!value.tree) {
52+
value.tree = overmindInstance.proxyStateTree.getTrackStateTree()
53+
value.componentInstanceId = componentInstanceId++
54+
value.onUpdate = (_: any, __: any, flushId: number) => {
55+
value.currentFlushId = flushId
56+
value.isUpdating = true
57+
flushIds.value = flushId
58+
state.value = {
59+
...(cb ? cb(overmindInstance.state) : overmindInstance.state),
60+
}
61+
62+
// this.$forceUpdate()
63+
}
64+
value.isUpdating = false
65+
}
66+
67+
onBeforeUpdate(function(this: any, ...args) {
68+
if (overmindInstance.mode.mode === MODE_SSR) return
69+
70+
value.tree.track(value.onUpdate)
71+
})
72+
73+
onRenderTracked(function(this: any, ...args) {
74+
if (IS_PRODUCTION) {
75+
return
76+
}
77+
78+
if (overmindInstance.isUpdating) {
79+
overmindInstance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
80+
componentId,
81+
componentInstanceId: value.componentInstanceId,
82+
name: '', // this.$options.name || '',
83+
flushId: value.currentFlushId,
84+
paths: Array.from(value.tree.pathDependencies) as any,
85+
})
86+
value.isUpdating = false
87+
}
88+
})
89+
90+
onMounted(() => {
91+
if (IS_PRODUCTION || overmindInstance.mode.mode === MODE_SSR) return
92+
value.tree.stopTracking()
93+
overmindInstance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
94+
componentId,
95+
componentInstanceId: value.componentInstanceId,
96+
name: '', // this.$options.name || '',
97+
paths: Array.from(value.tree.pathDependencies) as any,
98+
})
99+
})
100+
101+
onBeforeUnmount(() => {
102+
if (overmindInstance.mode.mode === MODE_SSR) return
103+
104+
overmindInstance.proxyStateTree.disposeTree(value.tree)
105+
if (IS_PRODUCTION) {
106+
return
107+
}
108+
109+
overmindInstance.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
110+
componentId,
111+
componentInstanceId: value.componentInstanceId,
112+
name: '', // this.$options.name || '',
113+
})
114+
})
115+
116+
value.tree.track(value.onUpdate)
117+
118+
return state
119+
}
120+
}) as any
121+
}
122+
123+
export interface ActionsHook<Config extends IConfiguration> {
124+
(): Ref<Overmind<Config>['actions']>
125+
<CB extends (actions: Overmind<Config>['actions']) => object>(
126+
cb: CB
127+
): CB extends (actions: Overmind<Config>['actions']) => infer O
128+
? O extends object
129+
? Ref<O>
130+
: never
131+
: never
132+
}
133+
134+
export function createActionsHook<Config extends IConfiguration>(): ActionsHook<
135+
Config
136+
> {
137+
return ((cb?: any): Overmind<Config>['actions'] => {
138+
const overmindInstance = inject<any>('overmind')
139+
140+
return cb ? cb(overmindInstance.actions) : overmindInstance.actions
141+
}) as any
142+
}
143+
144+
export interface EffectsHook<Config extends IConfiguration> {
145+
(): Ref<Overmind<Config>['effects']>
146+
<CB extends (effects: Overmind<Config>['effects']) => object>(
147+
cb: CB
148+
): CB extends (effects: Overmind<Config>['effects']) => infer O
149+
? O extends object
150+
? Ref<O>
151+
: never
152+
: never
153+
}
154+
155+
export function createEffectsHook<Config extends IConfiguration>(): EffectsHook<
156+
Config
157+
> {
158+
return ((cb?: any): Overmind<Config>['effects'] => {
159+
const overmindInstance = inject<any>('overmind')
160+
161+
return cb ? cb(overmindInstance.effects) : overmindInstance.effects
162+
}) as any
163+
}
164+
165+
export function createReactionHook<Config extends IConfiguration>() {
166+
return (): Overmind<Config>['reaction'] => {
167+
const overmindInstance = inject<any>('overmind')
168+
169+
return overmindInstance.reaction
170+
}
171+
}
172+
173+
export function createHooks<Config extends IConfiguration>() {
174+
return {
175+
state: createStateHook<Config>(),
176+
actions: createActionsHook<Config>(),
177+
effects: createEffectsHook<Config>(),
178+
reaction: createReactionHook<Config>(),
179+
}
180+
}

0 commit comments

Comments
 (0)