-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathmixin.js
153 lines (132 loc) Β· 4.24 KB
/
mixin.js
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
import { reapply } from '../lib/utils'
import { DollarApollo } from './dollar-apollo'
import { isServer } from './env'
function hasProperty (holder, key) {
return typeof holder !== 'undefined' && Object.prototype.hasOwnProperty.call(holder, key)
}
function proxyData () {
this.$_apolloInitData = {}
const apollo = this.$options.apollo
if (apollo) {
// watchQuery
for (const key in apollo) {
if (key.charAt(0) !== '$') {
const options = apollo[key]
// Property proxy
if (!options.manual && !hasProperty(this.$options.props, key) && !hasProperty(this.$options.computed, key) && !hasProperty(this.$options.methods, key)) {
Object.defineProperty(this, key, {
get: () => this.$data.$apolloData.data[key],
// For component class constructor
set: value => this.$_apolloInitData[key] = value,
enumerable: true,
configurable: true,
})
}
}
}
}
}
function launch () {
const apolloProvider = this.$apolloProvider
if (this._apolloLaunched || !apolloProvider) return
this._apolloLaunched = true
// Prepare properties
let apollo = this.$options.apollo
if (apollo) {
this.$_apolloPromises = []
if (!apollo.$init) {
apollo.$init = true
// Default options applied to `apollo` options
if (apolloProvider.defaultOptions) {
apollo = this.$options.apollo = Object.assign({}, apolloProvider.defaultOptions, apollo)
}
}
defineReactiveSetter(this.$apollo, 'skipAll', apollo.$skipAll, apollo.$deep)
defineReactiveSetter(this.$apollo, 'skipAllQueries', apollo.$skipAllQueries, apollo.$deep)
defineReactiveSetter(this.$apollo, 'skipAllSubscriptions', apollo.$skipAllSubscriptions, apollo.$deep)
defineReactiveSetter(this.$apollo, 'client', apollo.$client, apollo.$deep)
defineReactiveSetter(this.$apollo, 'loadingKey', apollo.$loadingKey, apollo.$deep)
defineReactiveSetter(this.$apollo, 'error', apollo.$error, apollo.$deep)
defineReactiveSetter(this.$apollo, 'watchLoading', apollo.$watchLoading, apollo.$deep)
// Apollo Data
Object.defineProperty(this, '$apolloData', {
get: () => this.$data.$apolloData,
enumerable: true,
configurable: true,
})
// watchQuery
for (const key in apollo) {
if (key.charAt(0) !== '$') {
let options = apollo[key]
const smart = this.$apollo.addSmartQuery(key, options)
if (isServer) {
options = reapply(options, this)
if (apolloProvider.prefetch !== false && options.prefetch !== false && apollo.$prefetch !== false && !smart.skip) {
this.$_apolloPromises.push(smart.firstRun)
}
}
}
}
if (apollo.subscribe) {
console.warn('vue-apollo -> `subscribe` option is deprecated. Use the `$subscribe` option instead.')
}
if (apollo.$subscribe) {
for (const key in apollo.$subscribe) {
this.$apollo.addSmartSubscription(key, apollo.$subscribe[key])
}
}
}
}
function defineReactiveSetter ($apollo, key, value, deep) {
if (typeof value !== 'undefined') {
if (typeof value === 'function') {
$apollo.defineReactiveSetter(key, value, deep)
} else {
$apollo[key] = value
}
}
}
function destroy () {
if (this.$apollo) {
this.$apollo.destroy()
this.$apollo = null
}
}
export function installMixin (app, provider) {
app.mixin({
data () {
return {
$apolloData: {
queries: {},
loading: 0,
data: this.$_apolloInitData,
},
}
},
beforeCreate () {
this.$apollo = new DollarApollo(this, provider)
proxyData.call(this)
if (isServer) {
// Patch render function to cleanup apollo
const render = this.$options.render
this.$options.render = (h) => {
const result = render.call(this, h)
destroy.call(this)
return result
}
}
},
serverPrefetch () {
if (this.$_apolloPromises) {
return Promise.all(this.$_apolloPromises).then(() => {
destroy.call(this)
}).catch(e => {
destroy.call(this)
return Promise.reject(e)
})
}
},
created: launch,
unmounted: destroy,
})
}