@@ -8,8 +8,12 @@ const main_queue = dispatch_get_current_queue();
88
99import { firebase , FirebaseApp , FirebaseError , serialize } from '@nativescript/firebase-core' ;
1010
11+ import '@nativescript/core' ;
12+
1113let defaultFirestore : Firestore ;
1214
15+ declare var FieldPathExt ;
16+
1317const fb = firebase ( ) ;
1418Object . defineProperty ( fb , 'firestore' , {
1519 value : ( app ?: FirebaseApp ) => {
@@ -141,18 +145,25 @@ function serializeItems(value) {
141145}
142146
143147function createDictionary ( dataOrField : any , fieldPathValue ?: any , moreFieldsAndValues ?: any ) {
144- // TODO: Correctly handle FieldPaths used as keys or passed in the dataOrField and
145- // moreFieldsAndValues params e.g. new FieldPath(['test', '123']) will currently result in an
146- // object like { "<FIRFieldPath: 0x283f94640>": "value" }
147- const data = { }
148- const assignKeyValue = ( key , val ) => data [ key instanceof FieldPath ? key . native : key ] = serializeItems ( val ) ;
148+ const data = NSMutableDictionary . alloc ( ) . init ( ) ;
149+
150+ const assignKeyValue = ( key , val ) => {
151+ // check the map for weak object
152+ if ( typeof key === 'string' && key . includes ( 'FIRFieldPath' ) ) {
153+ const ref = fp_refs . get ( key ) ?. get ?.( ) ;
154+ if ( ref ) {
155+ data . setObjectForKey ( serializeItems ( val ) , ref ) ;
156+ return ;
157+ }
158+ }
159+ data . setObjectForKey ( serializeItems ( val ) , key instanceof FieldPath ? key . native : key ) ;
160+ } ;
149161 if ( ! fieldPathValue && ! moreFieldsAndValues ) {
150162 Object . entries ( dataOrField ) . forEach ( ( item ) => assignKeyValue ( item [ 0 ] , item [ 1 ] ) ) ;
151- } else {
163+ } else {
152164 assignKeyValue ( dataOrField , fieldPathValue ) ;
153165 if ( Array . isArray ( moreFieldsAndValues ) ) {
154- Object . entries ( Object . fromEntries ( moreFieldsAndValues ) )
155- . forEach ( ( [ key , value ] ) => assignKeyValue ( key , value ) ) ;
166+ Object . entries ( Object . fromEntries ( moreFieldsAndValues ) ) . forEach ( ( [ key , value ] ) => assignKeyValue ( key , value ) ) ;
156167 }
157168 }
158169
@@ -993,19 +1004,27 @@ export class DocumentReference<T extends DocumentData = DocumentData> implements
9931004 }
9941005}
9951006
1007+ const fp_refs = new Map < string , WeakRef < FIRFieldPath > > ( ) ;
1008+
9961009export class FieldPath implements IFieldPath {
9971010 _native : FIRFieldPath ;
9981011
9991012 constructor ( fieldNames : string [ ] , native = false ) {
10001013 if ( ! native ) {
10011014 this . _native = FIRFieldPath . alloc ( ) . initWithFields ( fieldNames ) ;
1015+ this . _addToMap ( ) ;
10021016 }
10031017 }
10041018
1019+ private _addToMap ( ) {
1020+ fp_refs . set ( this . native . description , new WeakRef ( this . _native ) ) ;
1021+ }
1022+
10051023 static fromNative ( field : FIRFieldPath ) {
10061024 if ( field instanceof FIRFieldPath ) {
10071025 const path = new FieldPath ( [ ] , true ) ;
10081026 path . _native = field ;
1027+ path . _addToMap ( ) ;
10091028 return path ;
10101029 }
10111030 return null ;
@@ -1019,14 +1038,12 @@ export class FieldPath implements IFieldPath {
10191038 return this . native ;
10201039 }
10211040
1022- documentId ( ) : FieldPath {
1041+ static documentId ( ) : FieldPath {
10231042 return FieldPath . fromNative ( FIRFieldPath . documentID ( ) ) ;
10241043 }
10251044
1026- toJSON ( ) {
1027- return {
1028- documentId : this . documentId ,
1029- } ;
1045+ toString ( ) {
1046+ return this . native . description ;
10301047 }
10311048}
10321049
0 commit comments