Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit 66f723a

Browse files
committed
feat: update types firestore
1 parent 5a16e27 commit 66f723a

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

src/vuefire/firestore.ts

+47-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
OperationsType,
88
} from '../core'
99
import { firestore } from 'firebase'
10-
import Vue, { PluginFunction } from 'vue'
10+
import { ComponentPublicInstance, Plugin } from 'vue'
1111

1212
const ops: OperationsType = {
1313
set: (target, key, value) => walkSet(target, key, value),
@@ -72,9 +72,15 @@ const defaultOptions: Readonly<Required<PluginOptions>> = {
7272
wait: firestoreOptions.wait,
7373
}
7474

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+
*/
7884
$bind(
7985
name: string,
8086
reference: firestore.Query | firestore.CollectionReference,
@@ -85,7 +91,15 @@ declare module 'vue/types/vue' {
8591
reference: firestore.DocumentReference,
8692
options?: FirestoreOptions
8793
): Promise<firestore.DocumentData>
94+
95+
/**
96+
* Unbinds a bound reference
97+
*/
8898
$unbind: (name: string, reset?: FirestoreOptions['reset']) => void
99+
100+
/**
101+
* Bound firestore references
102+
*/
89103
$firestoreRefs: Readonly<
90104
Record<
91105
string,
@@ -95,35 +109,39 @@ declare module 'vue/types/vue' {
95109
// _firestoreSources: Readonly<
96110
// Record<string, firestore.CollectionReference | firestore.Query | firestore.DocumentReference>
97111
// >
112+
/**
113+
* Existing unbind functions that get automatically called when the component is unmounted
114+
* @internal
115+
*/
98116
_firestoreUnbinds: Readonly<
99117
Record<string, ReturnType<typeof bindCollection | typeof bindDocument>>
100118
>
101119
}
120+
export interface ComponentCustomOptions {
121+
/**
122+
* Calls `$bind` at created
123+
*/
124+
firestore?: FirestoreOption
125+
}
102126
}
103127

104128
type VueFirestoreObject = Record<
105129
string,
106130
firestore.DocumentReference | firestore.Query | firestore.CollectionReference
107131
>
108-
type FirestoreOption<V> = VueFirestoreObject | ((this: V) => VueFirestoreObject)
132+
type FirestoreOption = VueFirestoreObject | (() => VueFirestoreObject)
109133

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
119137
) {
120-
const strategies = Vue.config.optionMergeStrategies
138+
const strategies = app.config.optionMergeStrategies
121139
strategies.firestore = strategies.provide
122140

123141
const globalOptions = Object.assign({}, defaultOptions, pluginOptions)
124142
const { bindName, unbindName } = globalOptions
125143

126-
Vue.prototype[unbindName] = function firestoreUnbind(
144+
app.config.globalProperties[unbindName] = function firestoreUnbind(
127145
key: string,
128146
reset?: FirestoreOptions['reset']
129147
) {
@@ -132,8 +150,8 @@ export const firestorePlugin: PluginFunction<PluginOptions> = function firestore
132150
delete this.$firestoreRefs[key]
133151
}
134152

135-
Vue.prototype[bindName] = function firestoreBind(
136-
this: Vue,
153+
app.config.globalProperties[bindName] = function firestoreBind(
154+
this: ComponentPublicInstance,
137155
key: string,
138156
ref:
139157
| firestore.Query
@@ -144,7 +162,7 @@ export const firestorePlugin: PluginFunction<PluginOptions> = function firestore
144162
const options = Object.assign({}, globalOptions, userOptions)
145163

146164
if (this._firestoreUnbinds[key]) {
147-
this[unbindName as keyof Vue](
165+
this[unbindName as '$unbind'](
148166
key,
149167
// if wait, allow overriding with a function or reset, otherwise, force reset to false
150168
// else pass the reset option
@@ -161,22 +179,27 @@ export const firestorePlugin: PluginFunction<PluginOptions> = function firestore
161179
return promise
162180
}
163181

164-
Vue.mixin({
165-
beforeCreate(this: Vue) {
182+
app.mixin({
183+
beforeCreate(this: ComponentPublicInstance) {
166184
this._firestoreUnbinds = Object.create(null)
167185
this.$firestoreRefs = Object.create(null)
168186
},
169-
created(this: Vue) {
187+
created(this: ComponentPublicInstance) {
170188
const { firestore } = this.$options
171189
const refs =
172190
typeof firestore === 'function' ? firestore.call(this) : firestore
173191
if (!refs) return
174192
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+
)
176199
}
177200
},
178201

179-
beforeDestroy(this: Vue) {
202+
beforeDestroy(this: ComponentPublicInstance) {
180203
for (const subKey in this._firestoreUnbinds) {
181204
this._firestoreUnbinds[subKey]()
182205
}

0 commit comments

Comments
 (0)