7
7
OperationsType ,
8
8
} from '../core'
9
9
import { firestore } from 'firebase'
10
- import Vue , { PluginFunction } from 'vue'
10
+ import { ComponentPublicInstance , Plugin } from 'vue'
11
11
12
12
const ops : OperationsType = {
13
13
set : ( target , key , value ) => walkSet ( target , key , value ) ,
@@ -72,9 +72,15 @@ const defaultOptions: Readonly<Required<PluginOptions>> = {
72
72
wait : firestoreOptions . wait ,
73
73
}
74
74
75
- declare module 'vue/types/vue' {
76
- // TODO: export types to allow custom function names
77
- interface Vue {
75
+ declare module 'vue' {
76
+ export interface ComponentCustomProperties {
77
+ /**
78
+ * Binds a reference
79
+ *
80
+ * @param name
81
+ * @param reference
82
+ * @param options
83
+ */
78
84
$bind (
79
85
name : string ,
80
86
reference : firestore . Query | firestore . CollectionReference ,
@@ -85,7 +91,15 @@ declare module 'vue/types/vue' {
85
91
reference : firestore . DocumentReference ,
86
92
options ?: FirestoreOptions
87
93
) : Promise < firestore . DocumentData >
94
+
95
+ /**
96
+ * Unbinds a bound reference
97
+ */
88
98
$unbind : ( name : string , reset ?: FirestoreOptions [ 'reset' ] ) => void
99
+
100
+ /**
101
+ * Bound firestore references
102
+ */
89
103
$firestoreRefs : Readonly <
90
104
Record <
91
105
string ,
@@ -95,35 +109,39 @@ declare module 'vue/types/vue' {
95
109
// _firestoreSources: Readonly<
96
110
// Record<string, firestore.CollectionReference | firestore.Query | firestore.DocumentReference>
97
111
// >
112
+ /**
113
+ * Existing unbind functions that get automatically called when the component is unmounted
114
+ * @internal
115
+ */
98
116
_firestoreUnbinds : Readonly <
99
117
Record < string , ReturnType < typeof bindCollection | typeof bindDocument > >
100
118
>
101
119
}
120
+ export interface ComponentCustomOptions {
121
+ /**
122
+ * Calls `$bind` at created
123
+ */
124
+ firestore ?: FirestoreOption
125
+ }
102
126
}
103
127
104
128
type VueFirestoreObject = Record <
105
129
string ,
106
130
firestore . DocumentReference | firestore . Query | firestore . CollectionReference
107
131
>
108
- type FirestoreOption < V > = VueFirestoreObject | ( ( this : V ) => VueFirestoreObject )
132
+ type FirestoreOption = VueFirestoreObject | ( ( ) => VueFirestoreObject )
109
133
110
- declare module 'vue/types/options' {
111
- interface ComponentOptions < V extends Vue > {
112
- firestore ?: FirestoreOption < V >
113
- }
114
- }
115
-
116
- export const firestorePlugin : PluginFunction < PluginOptions > = function firestorePlugin (
117
- Vue ,
118
- pluginOptions = defaultOptions
134
+ export const firestorePlugin : Plugin = function firestorePlugin (
135
+ app ,
136
+ pluginOptions : PluginOptions = defaultOptions
119
137
) {
120
- const strategies = Vue . config . optionMergeStrategies
138
+ const strategies = app . config . optionMergeStrategies
121
139
strategies . firestore = strategies . provide
122
140
123
141
const globalOptions = Object . assign ( { } , defaultOptions , pluginOptions )
124
142
const { bindName, unbindName } = globalOptions
125
143
126
- Vue . prototype [ unbindName ] = function firestoreUnbind (
144
+ app . config . globalProperties [ unbindName ] = function firestoreUnbind (
127
145
key : string ,
128
146
reset ?: FirestoreOptions [ 'reset' ]
129
147
) {
@@ -132,8 +150,8 @@ export const firestorePlugin: PluginFunction<PluginOptions> = function firestore
132
150
delete this . $firestoreRefs [ key ]
133
151
}
134
152
135
- Vue . prototype [ bindName ] = function firestoreBind (
136
- this : Vue ,
153
+ app . config . globalProperties [ bindName ] = function firestoreBind (
154
+ this : ComponentPublicInstance ,
137
155
key : string ,
138
156
ref :
139
157
| firestore . Query
@@ -144,7 +162,7 @@ export const firestorePlugin: PluginFunction<PluginOptions> = function firestore
144
162
const options = Object . assign ( { } , globalOptions , userOptions )
145
163
146
164
if ( this . _firestoreUnbinds [ key ] ) {
147
- this [ unbindName as keyof Vue ] (
165
+ this [ unbindName as '$unbind' ] (
148
166
key ,
149
167
// if wait, allow overriding with a function or reset, otherwise, force reset to false
150
168
// else pass the reset option
@@ -161,22 +179,27 @@ export const firestorePlugin: PluginFunction<PluginOptions> = function firestore
161
179
return promise
162
180
}
163
181
164
- Vue . mixin ( {
165
- beforeCreate ( this : Vue ) {
182
+ app . mixin ( {
183
+ beforeCreate ( this : ComponentPublicInstance ) {
166
184
this . _firestoreUnbinds = Object . create ( null )
167
185
this . $firestoreRefs = Object . create ( null )
168
186
} ,
169
- created ( this : Vue ) {
187
+ created ( this : ComponentPublicInstance ) {
170
188
const { firestore } = this . $options
171
189
const refs =
172
190
typeof firestore === 'function' ? firestore . call ( this ) : firestore
173
191
if ( ! refs ) return
174
192
for ( const key in refs ) {
175
- this [ bindName as keyof Vue ] ( key , refs [ key ] , globalOptions )
193
+ this [ bindName as '$bind' ] (
194
+ key ,
195
+ // @ts -ignore: FIXME: there is probably a wrong type in global properties
196
+ refs [ key ] ,
197
+ globalOptions
198
+ )
176
199
}
177
200
} ,
178
201
179
- beforeDestroy ( this : Vue ) {
202
+ beforeDestroy ( this : ComponentPublicInstance ) {
180
203
for ( const subKey in this . _firestoreUnbinds ) {
181
204
this . _firestoreUnbinds [ subKey ] ( )
182
205
}
0 commit comments