-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathdatabase.ts
245 lines (218 loc) · 7.16 KB
/
database.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import type { FirebaseApp } from 'firebase/app'
import type { DatabaseReference, DataSnapshot, Query } from 'firebase/database'
import { App, ComponentPublicInstance, effectScope, toRef } from 'vue-demi'
import { isVue3 } from 'vue-demi'
import { useFirebaseApp } from '../app'
import { getGlobalScope } from '../globals'
import { ResetOption, UnbindWithReset } from '../shared'
import { databaseUnbinds, internalUnbind } from '../database/unbind'
import { _DatabaseRefOptions } from '../database/bind'
import { _useDatabaseRef } from '../database/useDatabaseRef'
/**
* Options for the Firebase Database Plugin that enables the Options API such as `$databaseBind` and `$databaseUnbind`.
*/
export interface DatabasePluginOptions extends _DatabaseRefOptions {
/**
* @deprecated: was largely unused and not very useful. Please open an issue with use cases if you need this.
*/
bindName?: string
/**
* @deprecated: was largely unused and not very useful. Please open an issue with use cases if you need this.
*/
unbindName?: string
}
const databasePluginDefaults: Readonly<
Required<Omit<DatabasePluginOptions, keyof _DatabaseRefOptions>>
> = {
bindName: '$databaseBind',
unbindName: '$databaseUnbind',
}
export type VueFirebaseObject = Record<string, Query | DatabaseReference>
export type FirebaseOption = VueFirebaseObject | (() => VueFirebaseObject)
/**
* Install this plugin if you want to add `$databaseBind` and `$databaseUnbind` functions. Note this plugin is only necessary if
* you use the Options API. If you **exclusively use the Composition API** (e.g. `useDatabaseObject()` and `useDatabaseList()`), you
* should not add it.
*
* @deprecated Use `VueFire` and `VueFireDatabaseOptionsAPI` with the `modules` option instead.
*
* @param app
* @param pluginOptions
*/
export function databasePlugin(
app: App,
pluginOptions?: DatabasePluginOptions,
firebaseApp?: FirebaseApp
) {
// TODO: implement
// const strategies = Vue.config.optionMergeStrategies
// strategies.firebase = strategies.provide
const globalOptions = Object.assign({}, databasePluginDefaults, pluginOptions)
const { bindName, unbindName } = globalOptions
const GlobalTarget = isVue3
? app.config.globalProperties
: (app as any).prototype
GlobalTarget[unbindName] = function databaseUnbind(
this: ComponentPublicInstance,
key: string,
reset?: ResetOption
) {
internalUnbind(key, databaseUnbinds.get(this), reset)
// @ts-expect-error: readonly for the users
delete this.$firebaseRefs[key]
// delete this._firebaseSources[key]
// delete this._firebaseUnbinds[key]
}
GlobalTarget[bindName] = function databaseBind(
this: ComponentPublicInstance,
key: string,
source: DatabaseReference | Query,
userOptions?: _DatabaseRefOptions
) {
const options = Object.assign({}, globalOptions, userOptions)
const target = toRef(this.$data as any, key)
if (!databaseUnbinds.has(this)) {
databaseUnbinds.set(this, {})
}
const unbinds = databaseUnbinds.get(this)!
if (unbinds[key]) {
unbinds[key](options.reset)
}
// add the old rtdb* methods if the user was using the defaults
if (pluginOptions) {
if (!pluginOptions.bindName) {
GlobalTarget['$rtdbBind'] = GlobalTarget[bindName]
}
if (!pluginOptions.unbindName) {
GlobalTarget['$rtdbUnbind'] = GlobalTarget[unbindName]
}
}
// we create a local scope to avoid leaking the effect since it's created outside of the component
const scope = getGlobalScope(firebaseApp || useFirebaseApp(), app).run(() =>
effectScope()
)!
const { promise, stop: _unbind } = scope.run(() =>
_useDatabaseRef(source, { target, ...options })
)!
// override the unbind to also free th variables created
const unbind: UnbindWithReset = (reset) => {
_unbind(reset)
scope.stop()
}
unbinds[key] = unbind
// we make it readonly for the user but we must change it. Maybe there is a way to have an internal type here but expose a readonly type through a d.ts
;(this.$firebaseRefs as Mutable<Record<string, DatabaseReference>>)[key] =
source.ref
return promise
}
// handle firebase option
app.mixin({
beforeCreate(this: ComponentPublicInstance) {
this.$firebaseRefs = Object.create(null)
},
created(this: ComponentPublicInstance) {
let bindings = this.$options.firebase
if (typeof bindings === 'function') {
bindings = bindings.call(this)
}
if (!bindings) return
for (const key in bindings) {
// @ts-expect-error: bindName is a string here
this[bindName](
// ts
key,
bindings[key],
globalOptions
)
}
},
beforeUnmount(this: ComponentPublicInstance) {
const unbinds = databaseUnbinds.get(this)
if (unbinds) {
for (const key in unbinds) {
unbinds[key]()
}
}
// @ts-expect-error: we are freeing the references to avoid memory leaks
this.$firebaseRefs = null
},
})
}
/**
* VueFire Database Module to be added to the `VueFire` Vue plugin options. If you **exclusively use the Composition
* API** (e.g. `useDatabaseObject()` and `useDatabaseList()`), you should not add it.
*
* @example
*
* ```ts
* import { createApp } from 'vue'
* import { VueFire, VueFireDatabaseOptionsAPI } from 'vuefire'
*
* const app = createApp(App)
* app.use(VueFire, {
* modules: [VueFireDatabaseOptionsAPI()],
* })
* ```
*/
export function VueFireDatabaseOptionsAPI(
pluginOptions?: DatabasePluginOptions
) {
return (firebaseApp: FirebaseApp, app: App) => {
return databasePlugin(app, pluginOptions, firebaseApp)
}
}
type Mutable<T> = { -readonly [P in keyof T]: T[P] }
declare module '@vue/runtime-core' {
export interface ComponentCustomProperties {
/**
* Binds a reference
*
* @param name
* @param reference
* @param options
*/
$databaseBind(
name: string,
reference: DatabaseReference | Query,
options?: _DatabaseRefOptions
): Promise<DataSnapshot>
/**
* {@inheritDoc ComponentCustomProperties.$databaseBind}
* @deprecated Use `$databaseBind` instead.
*/
$rtdbBind(
name: string,
reference: DatabaseReference | Query,
options?: _DatabaseRefOptions
): Promise<DataSnapshot>
/**
* Unbinds a bound reference
*/
$databaseUnbind: (name: string, reset?: ResetOption) => void
/**
* {@inheritDoc ComponentCustomProperties.$databaseUnbind}
* @deprecated Use `$databaseUnbind` instead.
*/
$rtdbUnbind: (name: string, reset?: ResetOption) => void
/**
* Bound database references
*/
$firebaseRefs: Readonly<Record<string, DatabaseReference>>
// _firebaseSources: Readonly<
// Record<string, Reference | Query>
// >
/**
* Existing unbind functions that get automatically called when the component is unmounted
* @internal
*/
// _firebaseUnbinds: Readonly<
// Record<string, UnbindWithReset>
// >
}
export interface ComponentCustomOptions {
/**
* Calls `$databaseBind` at created
*/
firebase?: FirebaseOption
}
}