@@ -20,7 +20,9 @@ internal expect fun disposeWhenDeallocated(resource: ActiveDatabaseResource): An
20
20
* duplicate resources being used. For this reason, each active database group has a coroutine mutex guarding the
21
21
* sync job.
22
22
*/
23
- internal class ActiveDatabaseGroup (val identifier : String ) {
23
+ internal class ActiveDatabaseGroup (
24
+ val identifier : String ,
25
+ ) {
24
26
internal var refCount = 0 // Guarded by companion object
25
27
internal val syncMutex = Mutex ()
26
28
@@ -32,7 +34,7 @@ internal class ActiveDatabaseGroup(val identifier: String) {
32
34
}
33
35
}
34
36
35
- companion object : SynchronizedObject () {
37
+ companion object : SynchronizedObject () {
36
38
internal val multipleInstancesMessage =
37
39
"""
38
40
Multiple PowerSync instances for the same database have been detected.
@@ -42,26 +44,32 @@ internal class ActiveDatabaseGroup(val identifier: String) {
42
44
43
45
private val allGroups = mutableListOf<ActiveDatabaseGroup >()
44
46
45
- private fun findGroup (warnOnDuplicate : Logger , identifier : String ): ActiveDatabaseGroup {
46
- return synchronized(this ) {
47
+ private fun findGroup (
48
+ warnOnDuplicate : Logger ,
49
+ identifier : String ,
50
+ ): ActiveDatabaseGroup =
51
+ synchronized(this ) {
47
52
val existing = allGroups.asSequence().firstOrNull { it.identifier == identifier }
48
- val resolvedGroup = if (existing == null ) {
49
- val added = ActiveDatabaseGroup (identifier)
50
- allGroups.add(added)
51
- added
52
- } else {
53
- existing
54
- }
53
+ val resolvedGroup =
54
+ if (existing == null ) {
55
+ val added = ActiveDatabaseGroup (identifier)
56
+ allGroups.add(added)
57
+ added
58
+ } else {
59
+ existing
60
+ }
55
61
56
62
if (resolvedGroup.refCount++ != 0 ) {
57
63
warnOnDuplicate.w { multipleInstancesMessage }
58
64
}
59
65
60
66
resolvedGroup
61
67
}
62
- }
63
68
64
- internal fun referenceDatabase (warnOnDuplicate : Logger , identifier : String ): Pair <ActiveDatabaseResource , Any > {
69
+ internal fun referenceDatabase (
70
+ warnOnDuplicate : Logger ,
71
+ identifier : String ,
72
+ ): Pair <ActiveDatabaseResource , Any > {
65
73
val group = findGroup(warnOnDuplicate, identifier)
66
74
val resource = ActiveDatabaseResource (group)
67
75
@@ -70,7 +78,9 @@ internal class ActiveDatabaseGroup(val identifier: String) {
70
78
}
71
79
}
72
80
73
- internal class ActiveDatabaseResource (val group : ActiveDatabaseGroup ) {
81
+ internal class ActiveDatabaseResource (
82
+ val group : ActiveDatabaseGroup ,
83
+ ) {
74
84
val disposed = atomic(false )
75
85
76
86
fun dispose () {
0 commit comments