@@ -22,18 +22,18 @@ export const ops: OperationsType = {
2222 remove : ( array , index ) => array . splice ( index , 1 ) ,
2323}
2424
25- const firestoreUnbinds = new WeakMap <
26- object ,
27- Record < string , ReturnType < typeof bindCollection | typeof bindDocument > >
28- > ( )
29-
3025function internalBind (
3126 target : Ref < any > ,
27+ key : string ,
3228 ref :
3329 | firestore . CollectionReference
3430 | firestore . Query
3531 | firestore . DocumentReference ,
3632 ops : OperationsType ,
33+ unbinds : Record <
34+ string ,
35+ ReturnType < typeof bindCollection | typeof bindDocument >
36+ > ,
3737 options ?: FirestoreOptions
3838) {
3939 return new Promise ( ( resolve , reject ) => {
@@ -61,23 +61,17 @@ function internalBind(
6161 options
6262 )
6363 }
64- if ( ! firestoreUnbinds . has ( target ) ) {
65- firestoreUnbinds . set ( target , { } )
66- }
67- const unbinds = firestoreUnbinds . get ( target ) !
68- // TODO: remove and refactor the firestoreUnbinds
69- const key = 'value'
7064 unbinds [ key ] = unbind
7165 } )
7266}
7367
7468export function internalUnbind (
75- target : object ,
76- // TODO: can go during the refactor
7769 key : string ,
70+ unbinds :
71+ | Record < string , ReturnType < typeof bindCollection | typeof bindDocument > >
72+ | undefined ,
7873 reset ?: FirestoreOptions [ 'reset' ]
7974) {
80- const unbinds = firestoreUnbinds . get ( target )
8175 if ( unbinds && unbinds [ key ] ) {
8276 unbinds [ key ] ( reset )
8377 delete unbinds [ key ]
@@ -159,6 +153,11 @@ type VueFirestoreObject = Record<
159153>
160154type FirestoreOption = VueFirestoreObject | ( ( ) => VueFirestoreObject )
161155
156+ const firestoreUnbinds = new WeakMap <
157+ object ,
158+ Record < string , ReturnType < typeof bindCollection | typeof bindDocument > >
159+ > ( )
160+
162161export const firestorePlugin : Plugin = function firestorePlugin (
163162 app ,
164163 pluginOptions : PluginOptions = defaultOptions
@@ -174,7 +173,7 @@ export const firestorePlugin: Plugin = function firestorePlugin(
174173 key : string ,
175174 reset ?: FirestoreOptions [ 'reset' ]
176175 ) {
177- internalUnbind ( this , key , reset )
176+ internalUnbind ( key , firestoreUnbinds . get ( this ) , reset )
178177 delete this . $firestoreRefs [ key ]
179178 }
180179
@@ -188,35 +187,26 @@ export const firestorePlugin: Plugin = function firestorePlugin(
188187 userOptions ?: FirestoreOptions
189188 ) {
190189 const options = Object . assign ( { } , globalOptions , userOptions )
191- const unbinds = firestoreUnbinds . get ( this )
190+ const target = toRef ( this . $data as any , key )
191+ let unbinds = firestoreUnbinds . get ( this )
192192
193- if ( unbinds && unbinds [ key ] ) {
194- unbinds [ key ] (
195- // if wait, allow overriding with a function or reset, otherwise, force reset to false
196- // else pass the reset option
197- options . wait
198- ? typeof options . reset === 'function'
199- ? options . reset
200- : false
201- : options . reset
202- )
203- // this[unbindName as '$unbind'](
204- // key,
205- // // if wait, allow overriding with a function or reset, otherwise, force reset to false
206- // // else pass the reset option
207- // options.wait
208- // ? typeof options.reset === 'function'
209- // ? options.reset
210- // : false
211- // : options.reset
212- // )
193+ if ( unbinds ) {
194+ if ( unbinds [ key ] ) {
195+ unbinds [ key ] (
196+ // if wait, allow overriding with a function or reset, otherwise, force reset to false
197+ // else pass the reset option
198+ options . wait
199+ ? typeof options . reset === 'function'
200+ ? options . reset
201+ : false
202+ : options . reset
203+ )
204+ }
205+ } else {
206+ firestoreUnbinds . set ( this , ( unbinds = { } ) )
213207 }
214- const promise = internalBind (
215- toRef ( this . $data as any , key ) ,
216- ref ,
217- ops ,
218- options
219- )
208+
209+ const promise = internalBind ( target , key , ref , ops , unbinds ! , options )
220210 // @ts -ignore we are allowed to write it
221211 this . $firestoreRefs [ key ] = ref
222212 return promise
@@ -264,7 +254,9 @@ export function bind(
264254 | firestore . DocumentReference ,
265255 options ?: FirestoreOptions
266256) {
267- const promise = internalBind ( target , ref , ops , options )
257+ const unbinds = { }
258+ firestoreUnbinds . set ( target , unbinds )
259+ const promise = internalBind ( target , '' , ref , ops , unbinds , options )
268260
269261 // TODO: SSR serialize the values for Nuxt to expose them later and use them
270262 // as initial values while specifying a wait: true to only swap objects once
@@ -282,4 +274,4 @@ export function bind(
282274}
283275
284276export const unbind = ( target : Ref , reset ?: FirestoreOptions [ 'reset' ] ) =>
285- internalUnbind ( target , 'value' , reset )
277+ internalUnbind ( '' , firestoreUnbinds . get ( target ) , reset )
0 commit comments