Skip to content

Commit 7e2db5a

Browse files
committed
refactor: wip rtdb
1 parent a164cfc commit 7e2db5a

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

src/vuefire/rtdb.ts

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
walkSet,
77
OperationsType,
88
} from '../core'
9-
import { database } from 'firebase'
10-
import Vue, { PluginFunction } from 'vue'
9+
import { app, database } from 'firebase'
10+
import { ComponentPublicInstance, Plugin } from 'vue'
1111

1212
/**
1313
* Returns the original reference of a Firebase reference or query across SDK versions.
@@ -90,54 +90,73 @@ const defaultOptions: Readonly<Required<PluginOptions>> = {
9090
wait: rtdbOptions.wait,
9191
}
9292

93-
declare module 'vue/types/vue' {
94-
// TODO: export types to allow custom function names
95-
interface Vue {
93+
declare module '@vue/runtime-core' {
94+
export interface ComponentCustomProperties {
95+
/**
96+
* Binds a reference
97+
*
98+
* @param name
99+
* @param reference
100+
* @param options
101+
*/
96102
$rtdbBind(
97103
name: string,
98104
reference: database.Reference | database.Query,
99105
options?: RTDBOptions
100106
): Promise<database.DataSnapshot>
107+
108+
/**
109+
* Unbinds a bound reference
110+
*/
101111
$rtdbUnbind: (name: string, reset?: RTDBOptions['reset']) => void
112+
113+
/**
114+
* Bound firestore references
115+
*/
102116
$firebaseRefs: Readonly<Record<string, database.Reference>>
103-
_firebaseSources: Readonly<
104-
Record<string, database.Reference | database.Query>
105-
>
106-
_firebaseUnbinds: Readonly<
107-
Record<string, ReturnType<typeof bindAsArray | typeof bindAsObject>>
108-
>
117+
// _firebaseSources: Readonly<
118+
// Record<string, database.Reference | database.Query>
119+
// >
120+
/**
121+
* Existing unbind functions that get automatically called when the component is unmounted
122+
* @internal
123+
*/
124+
// _firebaseUnbinds: Readonly<
125+
// Record<string, ReturnType<typeof bindAsArray | typeof bindAsObject>>
126+
// >
127+
}
128+
export interface ComponentCustomOptions {
129+
/**
130+
* Calls `$bind` at created
131+
*/
132+
firebase?: FirebaseOption
109133
}
110134
}
111135

112136
type VueFirebaseObject = Record<string, database.Query | database.Reference>
113-
type FirebaseOption<V> = VueFirebaseObject | ((this: V) => VueFirebaseObject)
114-
115-
declare module 'vue/types/options' {
116-
interface ComponentOptions<V extends Vue> {
117-
firebase?: FirebaseOption<V>
118-
}
119-
}
137+
type FirebaseOption = VueFirebaseObject | (() => VueFirebaseObject)
120138

121-
export const rtdbPlugin: PluginFunction<PluginOptions> = function rtdbPlugin(
122-
Vue,
123-
pluginOptions = defaultOptions
139+
export const rtdbPlugin: Plugin = function rtdbPlugin(
140+
app,
141+
pluginOptions: PluginOptions = defaultOptions
124142
) {
125-
const strategies = Vue.config.optionMergeStrategies
126-
strategies.firebase = strategies.provide
143+
// TODO: implement
144+
// const strategies = Vue.config.optionMergeStrategies
145+
// strategies.firebase = strategies.provide
127146

128147
const globalOptions = Object.assign({}, defaultOptions, pluginOptions)
129148
const { bindName, unbindName } = globalOptions
130149

131-
Vue.prototype[unbindName] = function rtdbUnbind(
150+
app.config.globalProperties[unbindName] = function rtdbUnbind(
132151
key: string,
133152
reset?: RTDBOptions['reset']
134153
) {
135154
unbind(this, key, reset)
136155
}
137156

138157
// add $rtdbBind and $rtdbUnbind methods
139-
Vue.prototype[bindName] = function rtdbBind(
140-
this: Vue,
158+
app.config.globalProperties[bindName] = function rtdbBind(
159+
this: ComponentPublicInstance,
141160
key: string,
142161
source: database.Reference | database.Query,
143162
userOptions?: RTDBOptions

0 commit comments

Comments
 (0)