|
6 | 6 | walkSet, |
7 | 7 | OperationsType, |
8 | 8 | } 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' |
11 | 11 |
|
12 | 12 | /** |
13 | 13 | * Returns the original reference of a Firebase reference or query across SDK versions. |
@@ -90,54 +90,73 @@ const defaultOptions: Readonly<Required<PluginOptions>> = { |
90 | 90 | wait: rtdbOptions.wait, |
91 | 91 | } |
92 | 92 |
|
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 | + */ |
96 | 102 | $rtdbBind( |
97 | 103 | name: string, |
98 | 104 | reference: database.Reference | database.Query, |
99 | 105 | options?: RTDBOptions |
100 | 106 | ): Promise<database.DataSnapshot> |
| 107 | + |
| 108 | + /** |
| 109 | + * Unbinds a bound reference |
| 110 | + */ |
101 | 111 | $rtdbUnbind: (name: string, reset?: RTDBOptions['reset']) => void |
| 112 | + |
| 113 | + /** |
| 114 | + * Bound firestore references |
| 115 | + */ |
102 | 116 | $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 |
109 | 133 | } |
110 | 134 | } |
111 | 135 |
|
112 | 136 | 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) |
120 | 138 |
|
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 |
124 | 142 | ) { |
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 |
127 | 146 |
|
128 | 147 | const globalOptions = Object.assign({}, defaultOptions, pluginOptions) |
129 | 148 | const { bindName, unbindName } = globalOptions |
130 | 149 |
|
131 | | - Vue.prototype[unbindName] = function rtdbUnbind( |
| 150 | + app.config.globalProperties[unbindName] = function rtdbUnbind( |
132 | 151 | key: string, |
133 | 152 | reset?: RTDBOptions['reset'] |
134 | 153 | ) { |
135 | 154 | unbind(this, key, reset) |
136 | 155 | } |
137 | 156 |
|
138 | 157 | // add $rtdbBind and $rtdbUnbind methods |
139 | | - Vue.prototype[bindName] = function rtdbBind( |
140 | | - this: Vue, |
| 158 | + app.config.globalProperties[bindName] = function rtdbBind( |
| 159 | + this: ComponentPublicInstance, |
141 | 160 | key: string, |
142 | 161 | source: database.Reference | database.Query, |
143 | 162 | userOptions?: RTDBOptions |
|
0 commit comments