@@ -8,8 +8,12 @@ const main_queue = dispatch_get_current_queue();
8
8
9
9
import { firebase , FirebaseApp , FirebaseError , serialize } from '@nativescript/firebase-core' ;
10
10
11
+ import '@nativescript/core' ;
12
+
11
13
let defaultFirestore : Firestore ;
12
14
15
+ declare var FieldPathExt ;
16
+
13
17
const fb = firebase ( ) ;
14
18
Object . defineProperty ( fb , 'firestore' , {
15
19
value : ( app ?: FirebaseApp ) => {
@@ -141,18 +145,25 @@ function serializeItems(value) {
141
145
}
142
146
143
147
function 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
+ } ;
149
161
if ( ! fieldPathValue && ! moreFieldsAndValues ) {
150
162
Object . entries ( dataOrField ) . forEach ( ( item ) => assignKeyValue ( item [ 0 ] , item [ 1 ] ) ) ;
151
- } else {
163
+ } else {
152
164
assignKeyValue ( dataOrField , fieldPathValue ) ;
153
165
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 ) ) ;
156
167
}
157
168
}
158
169
@@ -993,19 +1004,27 @@ export class DocumentReference<T extends DocumentData = DocumentData> implements
993
1004
}
994
1005
}
995
1006
1007
+ const fp_refs = new Map < string , WeakRef < FIRFieldPath > > ( ) ;
1008
+
996
1009
export class FieldPath implements IFieldPath {
997
1010
_native : FIRFieldPath ;
998
1011
999
1012
constructor ( fieldNames : string [ ] , native = false ) {
1000
1013
if ( ! native ) {
1001
1014
this . _native = FIRFieldPath . alloc ( ) . initWithFields ( fieldNames ) ;
1015
+ this . _addToMap ( ) ;
1002
1016
}
1003
1017
}
1004
1018
1019
+ private _addToMap ( ) {
1020
+ fp_refs . set ( this . native . description , new WeakRef ( this . _native ) ) ;
1021
+ }
1022
+
1005
1023
static fromNative ( field : FIRFieldPath ) {
1006
1024
if ( field instanceof FIRFieldPath ) {
1007
1025
const path = new FieldPath ( [ ] , true ) ;
1008
1026
path . _native = field ;
1027
+ path . _addToMap ( ) ;
1009
1028
return path ;
1010
1029
}
1011
1030
return null ;
@@ -1019,14 +1038,12 @@ export class FieldPath implements IFieldPath {
1019
1038
return this . native ;
1020
1039
}
1021
1040
1022
- documentId ( ) : FieldPath {
1041
+ static documentId ( ) : FieldPath {
1023
1042
return FieldPath . fromNative ( FIRFieldPath . documentID ( ) ) ;
1024
1043
}
1025
1044
1026
- toJSON ( ) {
1027
- return {
1028
- documentId : this . documentId ,
1029
- } ;
1045
+ toString ( ) {
1046
+ return this . native . description ;
1030
1047
}
1031
1048
}
1032
1049
0 commit comments