1
1
import { Timestamp , GeoPoint } from 'firebase/firestore'
2
+ import { markRaw } from 'vue'
2
3
import {
3
4
definePayloadPlugin ,
4
5
definePayloadReducer ,
@@ -20,35 +21,40 @@ export default definePayloadPlugin(() => {
20
21
const parsed = JSON . parse ( data )
21
22
22
23
if ( 'seconds' in parsed && 'nanoseconds' in parsed ) {
23
- return new Timestamp ( parsed . seconds , parsed . nanoseconds )
24
+ return markRaw ( new Timestamp ( parsed . seconds , parsed . nanoseconds ) )
24
25
}
25
26
26
27
if ( 'latitude' in parsed && 'longitude' in parsed ) {
27
- return new GeoPoint ( parsed . latitude , parsed . longitude )
28
+ return markRaw ( new GeoPoint ( parsed . latitude , parsed . longitude ) )
28
29
}
29
30
30
31
return parsed
31
32
} )
33
+
32
34
// to handle the `id` non-enumerable property
33
- definePayloadReducer (
35
+ definePayloadReducer ( 'DocumentData' , ( data : any ) => {
36
+ if ( data && typeof data === 'object' ) {
37
+ const idProp = Object . getOwnPropertyDescriptor ( data , 'id' )
38
+ // we need the non enumerable id property as it's likely used
39
+ if ( idProp && ! idProp . enumerable ) {
40
+ return {
41
+ ...data ,
42
+ id : data . id ,
43
+ }
44
+ }
45
+ }
46
+ } )
47
+ definePayloadReviver (
34
48
'DocumentData' ,
35
- ( data : any ) =>
36
- data &&
37
- typeof data === 'object' &&
38
- 'id' in data &&
39
- JSON . stringify ( {
40
- ...data ,
41
- id : data . id ,
49
+ ( data : string | Record < string , unknown > ) => {
50
+ const parsed = typeof data === 'string' ? JSON . parse ( data ) : data
51
+ // preserve the non-enumerable property
52
+ // we need to delete it first
53
+ const idValue = parsed . id
54
+ delete parsed . id
55
+ return Object . defineProperty ( parsed , 'id' , {
56
+ value : idValue ,
42
57
} )
58
+ }
43
59
)
44
- definePayloadReviver ( 'DocumentData' , ( data : string ) => {
45
- const parsed = JSON . parse ( data )
46
- // preserve the non-enumerable property
47
- // we need to delete it first
48
- const idValue = parsed . id
49
- delete parsed . id
50
- return Object . defineProperty ( parsed , 'id' , {
51
- value : idValue ,
52
- } )
53
- } )
54
60
} )
0 commit comments