|
1 | | -import { TApp, EventType, Overmind, Configuration } from 'overmind' |
2 | | -import Vue, { ComponentOptions } from 'vue' |
3 | | -import { IMutation } from 'proxy-state-tree' |
4 | | - |
5 | | -export type TConnect<Config extends Configuration> = { |
6 | | - state: TApp<Config>['state'] |
7 | | - actions: TApp<Config>['actions'] |
8 | | - addMutationListener: (cb: (mutation: IMutation) => void) => () => void |
9 | | -} |
10 | | - |
11 | | -type DefaultData<V> = object | ((this: V) => object) |
12 | | -type DefaultProps = Record<string, any> |
13 | | -type DefaultMethods<V> = { [key: string]: (this: V, ...args: any[]) => any } |
14 | | -type DefaultComputed = { [key: string]: any } |
| 1 | +import { EventType, Overmind } from 'overmind' |
15 | 2 |
|
16 | 3 | let nextComponentId = 0 |
17 | 4 |
|
18 | | -export const createConnect = <A extends Overmind<any>>(overmind: A) => < |
19 | | - V extends Vue & A, |
20 | | - Data extends DefaultData<V>, |
21 | | - Methods extends DefaultMethods<V>, |
22 | | - Computed extends DefaultComputed, |
23 | | - PropsDef, |
24 | | - Props extends DefaultProps |
25 | | ->( |
26 | | - componentOptions: ComponentOptions< |
27 | | - V, |
28 | | - Data, |
29 | | - Methods, |
30 | | - Computed, |
31 | | - PropsDef, |
32 | | - Props |
33 | | - > |
34 | | -): ComponentOptions<V, Data, Methods, Computed, PropsDef, Props> => { |
| 5 | +export const createConnect = (overmind) => (options) => { |
35 | 6 | const componentId = nextComponentId++ |
36 | 7 | let componentInstanceId = 0 |
37 | | - const beforeMount = componentOptions.beforeMount |
38 | | - const mounted = componentOptions.mounted |
39 | | - const beforeUpdate = componentOptions.beforeUpdate |
40 | | - const updated = componentOptions.updated |
41 | | - const beforeDestroy = componentOptions.beforeDestroy |
42 | | - |
43 | | - componentOptions.beforeMount = function() { |
44 | | - this.__tree = (overmind as any).proxyStateTree.getTrackStateTree() |
45 | | - this.__componentInstanceId = componentInstanceId++ |
46 | | - this.__onUpdate = (mutations, paths, flushId) => { |
47 | | - this.__currentFlushId = flushId |
48 | | - this.$forceUpdate() |
49 | | - } |
50 | | - this.overmind = { |
51 | | - state: this.__tree.state, |
52 | | - actions: overmind.actions, |
53 | | - addMutationListener: overmind.addMutationListener, |
54 | | - } |
55 | | - this.__tree.track(this.__onUpdate) |
56 | | - beforeMount && beforeMount.call(this) |
57 | | - } |
58 | | - |
59 | | - componentOptions.beforeUpdate = function() { |
60 | | - this.__tree.track(this.__onUpdate) |
61 | | - beforeUpdate && beforeUpdate.call(this) |
62 | | - } |
63 | | - |
64 | | - componentOptions.mounted = function() { |
65 | | - overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, { |
66 | | - componentId, |
67 | | - componentInstanceId: this.__componentInstanceId, |
68 | | - name: componentOptions.name || this.name || '', |
69 | | - paths: Array.from(this.__tree.pathDependencies) as any, |
70 | | - }) |
71 | | - mounted && mounted.call(this) |
72 | | - } |
73 | | - |
74 | | - componentOptions.updated = function() { |
75 | | - overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, { |
76 | | - componentId, |
77 | | - componentInstanceId: this.__componentInstanceId, |
78 | | - name: componentOptions.name || this.name || '', |
79 | | - flushId: this.__currentFlushId, |
80 | | - paths: Array.from(this.__tree.pathDependencies) as any, |
81 | | - }) |
82 | | - updated && updated.call(this) |
83 | | - } |
84 | | - |
85 | | - componentOptions.beforeDestroy = function() { |
86 | | - overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, { |
87 | | - componentId, |
88 | | - componentInstanceId: this.__componentInstanceId, |
89 | | - name: componentOptions.name || this.name || '', |
90 | | - }) |
91 | | - |
92 | | - // @ts-ignore |
93 | | - overmind.proxyStateTree.disposeTree(this.__tree) |
94 | | - beforeDestroy && beforeDestroy.call(this) |
95 | | - } |
96 | 8 |
|
97 | | - return componentOptions as any |
| 9 | + return { |
| 10 | + ...options, |
| 11 | + mixins: (options.mixins ? options.mixins : []).concat({ |
| 12 | + beforeMount(this: any) { |
| 13 | + this.__tree = (overmind as any).proxyStateTree.getTrackStateTree() |
| 14 | + this.__componentInstanceId = componentInstanceId++ |
| 15 | + this.__onUpdate = (mutations, paths, flushId) => { |
| 16 | + this.__currentFlushId = flushId |
| 17 | + this.$forceUpdate() |
| 18 | + } |
| 19 | + this.overmind = { |
| 20 | + state: this.__tree.state, |
| 21 | + actions: overmind.actions, |
| 22 | + addMutationListener: overmind.addMutationListener, |
| 23 | + } |
| 24 | + this.__tree.track(this.__onUpdate) |
| 25 | + }, |
| 26 | + beforeUpdate(this: any) { |
| 27 | + this.__tree.track(this.__onUpdate) |
| 28 | + }, |
| 29 | + mounted(this: any) { |
| 30 | + overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, { |
| 31 | + componentId, |
| 32 | + componentInstanceId: this.__componentInstanceId, |
| 33 | + name: options.name || this.name || '', |
| 34 | + paths: Array.from(this.__tree.pathDependencies) as any, |
| 35 | + }) |
| 36 | + }, |
| 37 | + updated(this: any) { |
| 38 | + overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, { |
| 39 | + componentId, |
| 40 | + componentInstanceId: this.__componentInstanceId, |
| 41 | + name: options.name || this.name || '', |
| 42 | + flushId: this.__currentFlushId, |
| 43 | + paths: Array.from(this.__tree.pathDependencies) as any, |
| 44 | + }) |
| 45 | + }, |
| 46 | + beforeDestroy(this: any) { |
| 47 | + overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, { |
| 48 | + componentId, |
| 49 | + componentInstanceId: this.__componentInstanceId, |
| 50 | + name: options.name || this.name || '', |
| 51 | + }) |
| 52 | + |
| 53 | + // @ts-ignore |
| 54 | + overmind.proxyStateTree.disposeTree(this.__tree) |
| 55 | + }, |
| 56 | + }), |
| 57 | + overmind, |
| 58 | + } as any |
98 | 59 | } |
0 commit comments