Skip to content

Commit f8eb4fd

Browse files
committed
Use wrappes + extension methods instead of abstract class
1 parent f75b240 commit f8eb4fd

File tree

8 files changed

+362
-273
lines changed

8 files changed

+362
-273
lines changed

firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/_encoders.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ internal actual fun isSpecialValue(value: Any) = when(value) {
55
is NativeFieldValue,
66
is NativeGeoPoint,
77
is NativeTimestamp,
8-
is NativeDocumentReference -> true
8+
is NativeDocumentReferenceType -> true
99
else -> false
1010
}

firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 81 additions & 65 deletions
Large diffs are not rendered by default.

firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/DocumentReferenceSerializer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import kotlinx.serialization.SerializationException
1010
*/
1111
object DocumentReferenceSerializer : KSerializer<DocumentReference> by SpecialValueSerializer(
1212
serialName = "DocumentReference",
13-
toNativeValue = DocumentReference::nativeValue,
13+
toNativeValue = { it.native.nativeValue },
1414
fromNativeValue = { value ->
1515
when (value) {
16-
is NativeDocumentReference -> DocumentReference(value)
16+
is NativeDocumentReferenceType -> DocumentReference(NativeDocumentReference(value))
1717
else -> throw SerializationException("Cannot deserialize $value")
1818
}
1919
}

firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 129 additions & 87 deletions
Large diffs are not rendered by default.

firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/_encoders.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ internal actual fun isSpecialValue(value: Any) = when(value) {
77
is FIRFieldValue,
88
is NativeGeoPoint,
99
is NativeTimestamp,
10-
is NativeDocumentReference -> true
10+
is NativeDocumentReferenceType -> true
1111
else -> false
1212
}

firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 80 additions & 64 deletions
Large diffs are not rendered by default.

firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/_encoders.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ internal actual fun isSpecialValue(value: Any) = when(value) {
55
is NativeFieldValue,
66
is NativeGeoPoint,
77
is NativeTimestamp,
8-
is NativeDocumentReference -> true
8+
is NativeDocumentReferenceType -> true
99
else -> false
1010
}

firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ actual class FirebaseFirestore(jsFirestore: Firestore) {
7171
var js: Firestore = jsFirestore
7272
private set
7373

74-
actual fun collection(collectionPath: String) = rethrow { CollectionReference(jsCollection(js, collectionPath)) }
74+
actual fun collection(collectionPath: String) = rethrow { CollectionReference(NativeCollectionReference(jsCollection(js, collectionPath))) }
7575

7676
actual fun collectionGroup(collectionId: String) = rethrow { Query(jsCollectionGroup(js, collectionId)) }
7777

78-
actual fun document(documentPath: String) = rethrow { DocumentReference(doc(js, documentPath)) }
78+
actual fun document(documentPath: String) = rethrow { DocumentReference(NativeDocumentReference(doc(js, documentPath))) }
7979

80-
actual fun batch() = rethrow { WriteBatch(writeBatch(js)) }
80+
actual fun batch() = rethrow { WriteBatch(NativeWriteBatch(writeBatch(js))) }
8181

8282
actual fun setLoggingEnabled(loggingEnabled: Boolean) =
8383
rethrow { setLogLevel( if(loggingEnabled) "error" else "silent") }
8484

8585
actual suspend fun <T> runTransaction(func: suspend Transaction.() -> T) =
86-
rethrow { jsRunTransaction(js, { GlobalScope.promise { Transaction(it).func() } } ).await() }
86+
rethrow { jsRunTransaction(js, { GlobalScope.promise { Transaction(NativeTransaction(it)).func() } } ).await() }
8787

8888
actual suspend fun clearPersistence() =
8989
rethrow { clearIndexedDbPersistence(js).await() }
@@ -110,37 +110,38 @@ actual class FirebaseFirestore(jsFirestore: Firestore) {
110110
}
111111
}
112112

113-
val SetOptions.js: Json get() = when (this) {
113+
internal val SetOptions.js: Json get() = when (this) {
114114
is SetOptions.Merge -> json("merge" to true)
115115
is SetOptions.Overwrite -> json("merge" to false)
116116
is SetOptions.MergeFields -> json("mergeFields" to fields.toTypedArray())
117117
is SetOptions.MergeFieldPaths -> json("mergeFields" to encodedFieldPaths.toTypedArray())
118118
}
119119

120-
actual class WriteBatch(val js: JsWriteBatch) : BaseWriteBatch() {
120+
@PublishedApi
121+
internal actual class NativeWriteBatch(val js: JsWriteBatch) {
121122

122-
override fun setEncoded(
123+
actual fun setEncoded(
123124
documentRef: DocumentReference,
124125
encodedData: Any,
125126
setOptions: SetOptions
126-
): BaseWriteBatch = rethrow { js.set(documentRef.js, encodedData, setOptions.js) }.let { this }
127+
): NativeWriteBatch = rethrow { js.set(documentRef.js, encodedData, setOptions.js) }.let { this }
127128

128-
override fun updateEncoded(documentRef: DocumentReference, encodedData: Any): BaseWriteBatch = rethrow { js.update(documentRef.js, encodedData) }
129+
actual fun updateEncoded(documentRef: DocumentReference, encodedData: Any): NativeWriteBatch = rethrow { js.update(documentRef.js, encodedData) }
129130
.let { this }
130131

131-
override fun updateEncodedFieldsAndValues(
132+
actual fun updateEncodedFieldsAndValues(
132133
documentRef: DocumentReference,
133134
encodedFieldsAndValues: List<Pair<String, Any?>>
134-
): BaseWriteBatch = rethrow {
135+
): NativeWriteBatch = rethrow {
135136
encodedFieldsAndValues.performUpdate { field, value, moreFieldsAndValues ->
136137
js.update(documentRef.js, field, value, *moreFieldsAndValues)
137138
}
138139
}.let { this }
139140

140-
override fun updateEncodedFieldPathsAndValues(
141+
actual fun updateEncodedFieldPathsAndValues(
141142
documentRef: DocumentReference,
142143
encodedFieldsAndValues: List<Pair<EncodedFieldPath, Any?>>
143-
): BaseWriteBatch = rethrow {
144+
): NativeWriteBatch = rethrow {
144145
encodedFieldsAndValues.performUpdate { field, value, moreFieldsAndValues ->
145146
js.update(documentRef.js, field, value, *moreFieldsAndValues)
146147
}
@@ -153,33 +154,36 @@ actual class WriteBatch(val js: JsWriteBatch) : BaseWriteBatch() {
153154
actual suspend fun commit() = rethrow { js.commit().await() }
154155
}
155156

156-
actual class Transaction(val js: JsTransaction) : BaseTransaction() {
157+
val WriteBatch.js get() = native.js
157158

158-
override fun setEncoded(
159+
@PublishedApi
160+
internal actual class NativeTransaction(val js: JsTransaction) {
161+
162+
actual fun setEncoded(
159163
documentRef: DocumentReference,
160164
encodedData: Any,
161165
setOptions: SetOptions
162-
): BaseTransaction = rethrow {
166+
): NativeTransaction = rethrow {
163167
js.set(documentRef.js, encodedData, setOptions.js)
164168
}
165169
.let { this }
166170

167-
override fun updateEncoded(documentRef: DocumentReference, encodedData: Any): BaseTransaction = rethrow { js.update(documentRef.js, encodedData) }
171+
actual fun updateEncoded(documentRef: DocumentReference, encodedData: Any): NativeTransaction = rethrow { js.update(documentRef.js, encodedData) }
168172
.let { this }
169173

170-
override fun updateEncodedFieldsAndValues(
174+
actual fun updateEncodedFieldsAndValues(
171175
documentRef: DocumentReference,
172176
encodedFieldsAndValues: List<Pair<String, Any?>>
173-
): BaseTransaction = rethrow {
177+
): NativeTransaction = rethrow {
174178
encodedFieldsAndValues.performUpdate { field, value, moreFieldsAndValues ->
175179
js.update(documentRef.js, field, value, *moreFieldsAndValues)
176180
}
177181
}.let { this }
178182

179-
override fun updateEncodedFieldPathsAndValues(
183+
actual fun updateEncodedFieldPathsAndValues(
180184
documentRef: DocumentReference,
181185
encodedFieldsAndValues: List<Pair<EncodedFieldPath, Any?>>
182-
): BaseTransaction = rethrow {
186+
): NativeTransaction = rethrow {
183187
encodedFieldsAndValues.performUpdate { field, value, moreFieldsAndValues ->
184188
js.update(documentRef.js, field, value, *moreFieldsAndValues)
185189
}
@@ -190,48 +194,50 @@ actual class Transaction(val js: JsTransaction) : BaseTransaction() {
190194
.let { this }
191195

192196
actual suspend fun get(documentRef: DocumentReference) =
193-
rethrow { DocumentSnapshot(js.get(documentRef.js).await()) }
197+
rethrow { NativeDocumentSnapshot(js.get(documentRef.js).await()) }
194198
}
195199

200+
val Transaction.js get() = native.js
201+
196202
/** A class representing a platform specific Firebase DocumentReference. */
197-
actual typealias NativeDocumentReference = JsDocumentReference
203+
actual typealias NativeDocumentReferenceType = JsDocumentReference
198204

199-
@Serializable(with = DocumentReferenceSerializer::class)
200-
actual class DocumentReference actual constructor(internal actual val nativeValue: NativeDocumentReference) : BaseDocumentReference() {
201-
val js: NativeDocumentReference = nativeValue
205+
@PublishedApi
206+
internal actual class NativeDocumentReference actual constructor(actual val nativeValue: NativeDocumentReferenceType) {
207+
val js: NativeDocumentReferenceType = nativeValue
202208

203209
actual val id: String
204210
get() = rethrow { js.id }
205211

206212
actual val path: String
207213
get() = rethrow { js.path }
208214

209-
actual val parent: CollectionReference
210-
get() = rethrow { CollectionReference(js.parent) }
215+
actual val parent: NativeCollectionReference
216+
get() = rethrow { NativeCollectionReference(js.parent) }
211217

212-
actual fun collection(collectionPath: String) = rethrow { CollectionReference(jsCollection(js, collectionPath)) }
218+
actual fun collection(collectionPath: String) = rethrow { NativeCollectionReference(jsCollection(js, collectionPath)) }
213219

214-
actual suspend fun get() = rethrow { DocumentSnapshot( getDoc(js).await()) }
220+
actual suspend fun get() = rethrow { NativeDocumentSnapshot( getDoc(js).await()) }
215221

216-
actual val snapshots: Flow<DocumentSnapshot> get() = snapshots()
222+
actual val snapshots: Flow<NativeDocumentSnapshot> get() = snapshots()
217223

218-
actual fun snapshots(includeMetadataChanges: Boolean) = callbackFlow<DocumentSnapshot> {
224+
actual fun snapshots(includeMetadataChanges: Boolean) = callbackFlow<NativeDocumentSnapshot> {
219225
val unsubscribe = onSnapshot(
220226
js,
221227
json("includeMetadataChanges" to includeMetadataChanges),
222-
{ trySend(DocumentSnapshot(it)) },
228+
{ trySend(NativeDocumentSnapshot(it)) },
223229
{ close(errorToException(it)) }
224230
)
225231
awaitClose { unsubscribe() }
226232
}
227233

228-
override suspend fun setEncoded(encodedData: Any, setOptions: SetOptions) = rethrow {
234+
actual suspend fun setEncoded(encodedData: Any, setOptions: SetOptions) = rethrow {
229235
setDoc(js, encodedData, setOptions.js).await()
230236
}
231237

232-
override suspend fun updateEncoded(encodedData: Any) = rethrow { jsUpdate(js, encodedData).await() }
238+
actual suspend fun updateEncoded(encodedData: Any) = rethrow { jsUpdate(js, encodedData).await() }
233239

234-
override suspend fun updateEncodedFieldsAndValues(encodedFieldsAndValues: List<Pair<String, Any?>>) {
240+
actual suspend fun updateEncodedFieldsAndValues(encodedFieldsAndValues: List<Pair<String, Any?>>) {
235241
rethrow {
236242
encodedFieldsAndValues.takeUnless { encodedFieldsAndValues.isEmpty() }
237243
?.performUpdate { field, value, moreFieldsAndValues ->
@@ -241,7 +247,7 @@ actual class DocumentReference actual constructor(internal actual val nativeValu
241247
}
242248
}
243249

244-
override suspend fun updateEncodedFieldPathsAndValues(encodedFieldsAndValues: List<Pair<EncodedFieldPath, Any?>>) {
250+
actual suspend fun updateEncodedFieldPathsAndValues(encodedFieldsAndValues: List<Pair<EncodedFieldPath, Any?>>) {
245251
rethrow {
246252
encodedFieldsAndValues.takeUnless { encodedFieldsAndValues.isEmpty() }
247253
?.performUpdate { field, value, moreFieldsAndValues ->
@@ -250,15 +256,18 @@ actual class DocumentReference actual constructor(internal actual val nativeValu
250256
}
251257
}
252258

253-
override suspend fun delete() = rethrow { deleteDoc(js).await() }
259+
actual suspend fun delete() = rethrow { deleteDoc(js).await() }
254260

255261
override fun equals(other: Any?): Boolean =
256-
this === other || other is DocumentReference && refEqual(nativeValue, other.nativeValue)
262+
this === other || other is NativeDocumentReference && refEqual(nativeValue, other.nativeValue)
257263
override fun hashCode(): Int = nativeValue.hashCode()
258264
override fun toString(): String = "DocumentReference(path=$path)"
259265
}
260266

261-
actual data class NativeQuery(val js: JsQuery)
267+
val DocumentReference.js get() = native.js
268+
269+
@PublishedApi
270+
internal actual open class NativeQuery(open val js: JsQuery)
262271

263272
actual open class Query internal actual constructor(nativeQuery: NativeQuery) {
264273

@@ -356,38 +365,41 @@ actual open class Query internal actual constructor(nativeQuery: NativeQuery) {
356365
}
357366
}
358367

359-
actual class CollectionReference(override val js: JsCollectionReference) : BaseCollectionReference(NativeQuery(js)) {
368+
@PublishedApi
369+
internal actual class NativeCollectionReference(override val js: JsCollectionReference) : NativeQuery(js) {
360370

361371
actual val path: String
362372
get() = rethrow { js.path }
363373

364-
actual val document get() = rethrow { DocumentReference(doc(js)) }
374+
actual val document get() = rethrow { NativeDocumentReference(doc(js)) }
365375

366-
actual val parent get() = rethrow { js.parent?.let{DocumentReference(it)} }
376+
actual val parent get() = rethrow { js.parent?.let{ NativeDocumentReference(it) } }
367377

368-
actual fun document(documentPath: String) = rethrow { DocumentReference(doc(js, documentPath)) }
378+
actual fun document(documentPath: String) = rethrow { NativeDocumentReference(doc(js, documentPath)) }
369379

370-
override suspend fun addEncoded(data: Any) = rethrow {
371-
DocumentReference(addDoc(js, data).await())
380+
actual suspend fun addEncoded(data: Any) = rethrow {
381+
NativeDocumentReference(addDoc(js, data).await())
372382
}
373383
}
374384

385+
val CollectionReference.js get() = native.js
386+
375387
actual class FirebaseFirestoreException(cause: Throwable, val code: FirestoreExceptionCode) : FirebaseException(code.toString(), cause)
376388

377389
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
378390
actual val FirebaseFirestoreException.code: FirestoreExceptionCode get() = code
379391

380392
actual class QuerySnapshot(val js: JsQuerySnapshot) {
381393
actual val documents
382-
get() = js.docs.map { DocumentSnapshot(it) }
394+
get() = js.docs.map { DocumentSnapshot(NativeDocumentSnapshot(it)) }
383395
actual val documentChanges
384396
get() = js.docChanges().map { DocumentChange(it) }
385397
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(js.metadata)
386398
}
387399

388400
actual class DocumentChange(val js: JsDocumentChange) {
389401
actual val document: DocumentSnapshot
390-
get() = DocumentSnapshot(js.doc)
402+
get() = DocumentSnapshot(NativeDocumentSnapshot(js.doc))
391403
actual val newIndex: Int
392404
get() = js.newIndex
393405
actual val oldIndex: Int
@@ -396,16 +408,17 @@ actual class DocumentChange(val js: JsDocumentChange) {
396408
get() = ChangeType.values().first { it.jsString == js.type }
397409
}
398410

399-
actual class DocumentSnapshot(val js: JsDocumentSnapshot) : BaseDocumentSnapshot() {
411+
@PublishedApi
412+
internal actual class NativeDocumentSnapshot(val js: JsDocumentSnapshot) {
400413

401414
actual val id get() = rethrow { js.id }
402-
actual val reference get() = rethrow { DocumentReference(js.ref) }
415+
actual val reference get() = rethrow { NativeDocumentReference(js.ref) }
403416

404-
override fun getEncoded(field: String, serverTimestampBehavior: ServerTimestampBehavior): Any? = rethrow {
417+
actual fun getEncoded(field: String, serverTimestampBehavior: ServerTimestampBehavior): Any? = rethrow {
405418
js.get(field, getTimestampsOptions(serverTimestampBehavior))
406419
}
407420

408-
override fun encodedData(serverTimestampBehavior: ServerTimestampBehavior): Any? = rethrow {
421+
actual fun encodedData(serverTimestampBehavior: ServerTimestampBehavior): Any? = rethrow {
409422
js.data(getTimestampsOptions(serverTimestampBehavior))
410423
}
411424

@@ -417,6 +430,8 @@ actual class DocumentSnapshot(val js: JsDocumentSnapshot) : BaseDocumentSnapshot
417430
json("serverTimestamps" to serverTimestampBehavior.name.lowercase())
418431
}
419432

433+
val DocumentSnapshot.js get() = native.js
434+
420435
actual class SnapshotMetadata(val js: JsSnapshotMetadata) {
421436
actual val hasPendingWrites: Boolean get() = js.hasPendingWrites
422437
actual val isFromCache: Boolean get() = js.fromCache

0 commit comments

Comments
 (0)