1
+ /**
2
+ * @typedef {firebase.firestore.DocumentReference | firebase.firestore.CollectionReference } Reference
3
+ */
4
+ /**
5
+ *
6
+ * @param {firebase.firestore.DocumentSnapshot } doc
7
+ * @return {DocumentData }
8
+ */
1
9
export function createSnapshot ( doc ) {
2
10
// defaults everything to false, so no need to set
3
11
return Object . defineProperty ( doc . data ( ) , 'id' , {
4
12
value : doc . id ,
5
13
} )
6
14
}
7
15
16
+ /**
17
+ *
18
+ * @param {any } o
19
+ * @returns {boolean }
20
+ */
8
21
function isObject ( o ) {
9
22
return o && typeof o === 'object'
10
23
}
11
24
25
+ /**
26
+ *
27
+ * @param {any } o
28
+ * should be o is Date https://github.com/Microsoft/TypeScript/issues/26297
29
+ * @returns {boolean }
30
+ */
12
31
function isTimestamp ( o ) {
13
32
return o . toDate
14
33
}
15
34
35
+ /**
36
+ *
37
+ * @param {* } o
38
+ * @returns {boolean }
39
+ */
16
40
function isRef ( o ) {
17
41
return o && o . onSnapshot
18
42
}
19
43
20
- export function extractRefs ( doc , oldDoc , path = '' , result = [ { } , { } ] ) {
44
+ /**
45
+ *
46
+ * @param {firebase.firestore.DocumentData } doc
47
+ * @param {firebase.firestore.DocumentData } [oldDoc]
48
+ * @param {string } [path]
49
+ * @param {[firebase.firestore.DocumentData, Record<string, Reference>] } result
50
+ * @returns {[firebase.firestore.DocumentData, Record<string, Reference>] }
51
+ */
52
+ export function extractRefs ( doc , oldDoc = { } , path = '' , result = [ { } , { } ] ) {
21
53
// must be set here because walkGet can return null or undefined
22
54
oldDoc = oldDoc || { }
23
55
const idDescriptor = Object . getOwnPropertyDescriptor ( doc , 'id' )
@@ -53,7 +85,15 @@ export function extractRefs(doc, oldDoc, path = '', result = [{}, {}]) {
53
85
} , result )
54
86
}
55
87
88
+ /**
89
+ * @template T any
90
+ * @template K any
91
+ * @param {(arg: T) => K } fn
92
+ * @param {() => T } argFn
93
+ * @returns {() => K | undefined }
94
+ */
56
95
export function callOnceWithArg ( fn , argFn ) {
96
+ /** @type {boolean | undefined } */
57
97
let called
58
98
return ( ) => {
59
99
if ( ! called ) {
@@ -63,10 +103,23 @@ export function callOnceWithArg(fn, argFn) {
63
103
}
64
104
}
65
105
106
+ /**
107
+ *
108
+ * @param {Record<string, any> } obj
109
+ * @param {string } path
110
+ * @returns {any }
111
+ */
66
112
export function walkGet ( obj , path ) {
67
113
return path . split ( '.' ) . reduce ( ( target , key ) => target [ key ] , obj )
68
114
}
69
115
116
+ /**
117
+ *
118
+ * @param {Record<string, any> } obj
119
+ * @param {string } path
120
+ * @param {any } value
121
+ * @returns
122
+ */
70
123
export function walkSet ( obj , path , value ) {
71
124
// path can be a number
72
125
const keys = ( '' + path ) . split ( '.' )
0 commit comments