Skip to content

Commit d6fa643

Browse files
feat(overmind-vue): add plugin factory
1 parent f8a1fdd commit d6fa643

File tree

1 file changed

+78
-58
lines changed
  • packages/node_modules/overmind-vue/src

1 file changed

+78
-58
lines changed

packages/node_modules/overmind-vue/src/index.ts

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,91 @@ import { EventType } from 'overmind'
22

33
let nextComponentId = 0
44

5-
export const createConnect = (overmind) => (...args) => {
5+
function createMixin(overmind, propsCallback) {
66
const componentId = nextComponentId++
77
let componentInstanceId = 0
8+
9+
return {
10+
beforeMount(this: any) {
11+
this.__tree = (overmind as any).proxyStateTree.getTrackStateTree()
12+
this.__componentInstanceId = componentInstanceId++
13+
this.__onUpdate = (mutations, paths, flushId) => {
14+
this.__currentFlushId = flushId
15+
this.$forceUpdate()
16+
}
17+
this.overmind = {
18+
state: this.__tree.state,
19+
actions: overmind.actions,
20+
effects: overmind.effects,
21+
addMutationListener: overmind.addMutationListener,
22+
}
23+
if (propsCallback) {
24+
Object.assign(
25+
this,
26+
propsCallback({
27+
state: this.__tree.state,
28+
actions: overmind.actions,
29+
effects: overmind.effects,
30+
})
31+
)
32+
}
33+
this.__tree.track(this.__onUpdate)
34+
},
35+
beforeUpdate(this: any) {
36+
this.__tree.track(this.__onUpdate)
37+
},
38+
mounted(this: any) {
39+
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
40+
componentId,
41+
componentInstanceId: this.__componentInstanceId,
42+
name: this.$options.name || '',
43+
paths: Array.from(this.__tree.pathDependencies) as any,
44+
})
45+
},
46+
updated(this: any) {
47+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
48+
componentId,
49+
componentInstanceId: this.__componentInstanceId,
50+
name: this.$options.name || '',
51+
flushId: this.__currentFlushId,
52+
paths: Array.from(this.__tree.pathDependencies) as any,
53+
})
54+
},
55+
beforeDestroy(this: any) {
56+
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
57+
componentId,
58+
componentInstanceId: this.__componentInstanceId,
59+
name: this.$options.name || '',
60+
})
61+
62+
// @ts-ignore
63+
overmind.proxyStateTree.disposeTree(this.__tree)
64+
},
65+
}
66+
}
67+
68+
export const createPlugin = (overmind) => ({
69+
install(
70+
Vue,
71+
propsCallback = ({ state, actions, effects }) => ({
72+
state,
73+
actions,
74+
effects,
75+
})
76+
) {
77+
Vue.mixin(createMixin(overmind, propsCallback))
78+
},
79+
})
80+
81+
export const createConnect = (overmind) => (...args) => {
882
let options = args.length === 1 ? args[0] : args[1]
983
let propsCallback = args.length === 1 ? null : args[0]
1084

1185
return {
1286
...options,
13-
mixins: (options.mixins ? options.mixins : []).concat({
14-
beforeMount(this: any) {
15-
this.__tree = (overmind as any).proxyStateTree.getTrackStateTree()
16-
this.__componentInstanceId = componentInstanceId++
17-
this.__onUpdate = (mutations, paths, flushId) => {
18-
this.__currentFlushId = flushId
19-
this.$forceUpdate()
20-
}
21-
this.overmind = {
22-
state: this.__tree.state,
23-
actions: overmind.actions,
24-
effects: overmind.effects,
25-
addMutationListener: overmind.addMutationListener,
26-
}
27-
if (propsCallback) {
28-
Object.assign(
29-
this,
30-
propsCallback({
31-
state: this.__tree.state,
32-
actions: overmind.actions,
33-
effects: overmind.effects,
34-
})
35-
)
36-
}
37-
this.__tree.track(this.__onUpdate)
38-
},
39-
beforeUpdate(this: any) {
40-
this.__tree.track(this.__onUpdate)
41-
},
42-
mounted(this: any) {
43-
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
44-
componentId,
45-
componentInstanceId: this.__componentInstanceId,
46-
name: options.name || this.name || '',
47-
paths: Array.from(this.__tree.pathDependencies) as any,
48-
})
49-
},
50-
updated(this: any) {
51-
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
52-
componentId,
53-
componentInstanceId: this.__componentInstanceId,
54-
name: options.name || this.name || '',
55-
flushId: this.__currentFlushId,
56-
paths: Array.from(this.__tree.pathDependencies) as any,
57-
})
58-
},
59-
beforeDestroy(this: any) {
60-
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
61-
componentId,
62-
componentInstanceId: this.__componentInstanceId,
63-
name: options.name || this.name || '',
64-
})
65-
66-
// @ts-ignore
67-
overmind.proxyStateTree.disposeTree(this.__tree)
68-
},
69-
}),
87+
mixins: (options.mixins ? options.mixins : []).concat(
88+
createMixin(overmind, propsCallback)
89+
),
7090
overmind,
7191
} as any
7292
}

0 commit comments

Comments
 (0)