11package com.ably.example
22
3- import androidx.compose.runtime.Composable
4- import androidx.compose.runtime.DisposableEffect
5- import androidx.compose.runtime.LaunchedEffect
6- import androidx.compose.runtime.getValue
7- import androidx.compose.runtime.mutableStateOf
8- import androidx.compose.runtime.remember
9- import androidx.compose.runtime.setValue
10- import io.ably.lib.objects.RealtimeObjects
3+ import androidx.compose.runtime.*
114import io.ably.lib.objects.ObjectsCallback
5+ import io.ably.lib.objects.RealtimeObjects
126import io.ably.lib.objects.type.counter.LiveCounter
137import io.ably.lib.objects.type.counter.LiveCounterUpdate
148import io.ably.lib.objects.type.map.LiveMap
@@ -28,7 +22,7 @@ import kotlinx.coroutines.launch
2822import kotlinx.coroutines.suspendCancellableCoroutine
2923import kotlin.coroutines.resume
3024
31- suspend fun RealtimeObjects.getRootCoroutines (): LiveMap = suspendCancellableCoroutine { continuation ->
25+ private suspend fun RealtimeObjects.getRootCoroutines (): LiveMap = suspendCancellableCoroutine { continuation ->
3226 getRootAsync(object : ObjectsCallback <LiveMap > {
3327 override fun onSuccess (result : LiveMap ? ) {
3428 continuation.resume(result!! )
@@ -40,19 +34,20 @@ suspend fun RealtimeObjects.getRootCoroutines(): LiveMap = suspendCancellableCor
4034 })
4135}
4236
43- suspend fun RealtimeObjects.createCounterCoroutine (): LiveCounter = suspendCancellableCoroutine { continuation ->
44- createCounterAsync(object : ObjectsCallback <LiveCounter > {
45- override fun onSuccess (result : LiveCounter ? ) {
46- continuation.resume(result!! )
47- }
37+ private suspend fun RealtimeObjects.createCounterCoroutine (): LiveCounter =
38+ suspendCancellableCoroutine { continuation ->
39+ createCounterAsync(object : ObjectsCallback <LiveCounter > {
40+ override fun onSuccess (result : LiveCounter ? ) {
41+ continuation.resume(result!! )
42+ }
4843
49- override fun onError (exception : AblyException ? ) {
50- continuation.cancel(exception)
51- }
52- })
53- }
44+ override fun onError (exception : AblyException ? ) {
45+ continuation.cancel(exception)
46+ }
47+ })
48+ }
5449
55- suspend fun RealtimeObjects.createMapCoroutine (): LiveMap = suspendCancellableCoroutine { continuation ->
50+ private suspend fun RealtimeObjects.createMapCoroutine (): LiveMap = suspendCancellableCoroutine { continuation ->
5651 createMapAsync(object : ObjectsCallback <LiveMap > {
5752 override fun onSuccess (result : LiveMap ? ) {
5853 continuation.resume(result!! )
@@ -64,40 +59,46 @@ suspend fun RealtimeObjects.createMapCoroutine(): LiveMap = suspendCancellableCo
6459 })
6560}
6661
67- suspend fun LiveCounter.incrementCoroutine (amount : Int ): Unit = suspendCancellableCoroutine { continuation ->
68- incrementAsync(amount, object : ObjectsCallback <Void > {
69- override fun onSuccess (result : Void ? ) {
70- continuation.resume(Unit )
71- }
62+ suspend fun LiveCounter.incrementCoroutine (amount : Int ): Unit = supressCoroutineExceptions {
63+ suspendCancellableCoroutine { continuation ->
64+ incrementAsync(amount, object : ObjectsCallback <Void > {
65+ override fun onSuccess (result : Void ? ) {
66+ continuation.resume(Unit )
67+ }
7268
73- override fun onError (exception : AblyException ? ) {
74- continuation.cancel(exception)
75- }
76- })
69+ override fun onError (exception : AblyException ? ) {
70+ continuation.cancel(exception)
71+ }
72+ })
73+ }
7774}
7875
79- suspend fun LiveCounter.decrementCoroutine (amount : Int ): Unit = suspendCancellableCoroutine { continuation ->
80- decrementAsync(amount, object : ObjectsCallback <Void > {
81- override fun onSuccess (result : Void ? ) {
82- continuation.resume(Unit )
83- }
76+ suspend fun LiveCounter.decrementCoroutine (amount : Int ): Unit = supressCoroutineExceptions {
77+ suspendCancellableCoroutine { continuation ->
78+ decrementAsync(amount, object : ObjectsCallback <Void > {
79+ override fun onSuccess (result : Void ? ) {
80+ continuation.resume(Unit )
81+ }
8482
85- override fun onError (exception : AblyException ? ) {
86- continuation.cancel(exception)
87- }
88- })
83+ override fun onError (exception : AblyException ? ) {
84+ continuation.cancel(exception)
85+ }
86+ })
87+ }
8988}
9089
91- suspend fun Channel.updateOptions (options : ChannelOptions ): Unit = suspendCancellableCoroutine { continuation ->
92- setOptions(options, object : io.ably.lib.realtime.CompletionListener {
93- override fun onSuccess () {
94- continuation.resume(Unit )
95- }
90+ suspend fun Channel.updateOptions (options : ChannelOptions ): Unit = supressCoroutineExceptions {
91+ suspendCancellableCoroutine { continuation ->
92+ setOptions(options, object : io.ably.lib.realtime.CompletionListener {
93+ override fun onSuccess () {
94+ continuation.resume(Unit )
95+ }
9696
97- override fun onError (reason : ErrorInfo ? ) {
98- continuation.cancel(AblyException .fromErrorInfo(reason))
99- }
100- })
97+ override fun onError (reason : ErrorInfo ? ) {
98+ continuation.cancel(AblyException .fromErrorInfo(reason))
99+ }
100+ })
101+ }
101102}
102103
103104suspend fun getOrCreateCounter (channel : Channel , root : LiveMap ? , path : String ): LiveCounter {
@@ -122,28 +123,32 @@ suspend fun getOrCreateMap(channel: Channel, root: LiveMap?, path: String): Live
122123 }
123124}
124125
125- suspend fun LiveMap.setCoroutine (key : String , value : LiveMapValue ) = suspendCancellableCoroutine<Unit > { continuation ->
126- setAsync(key, value, object : ObjectsCallback <Void > {
127- override fun onSuccess (result : Void ? ) {
128- continuation.resume(Unit )
129- }
126+ suspend fun LiveMap.setCoroutine (key : String , value : LiveMapValue ) = supressCoroutineExceptions {
127+ suspendCancellableCoroutine<Unit > { continuation ->
128+ setAsync(key, value, object : ObjectsCallback <Void > {
129+ override fun onSuccess (result : Void ? ) {
130+ continuation.resume(Unit )
131+ }
130132
131- override fun onError (exception : AblyException ? ) {
132- continuation.cancel(exception)
133- }
134- })
133+ override fun onError (exception : AblyException ? ) {
134+ continuation.cancel(exception)
135+ }
136+ })
137+ }
135138}
136139
137- suspend fun LiveMap.removeCoroutine (key : String ) = suspendCancellableCoroutine<Unit > { continuation ->
138- removeAsync(key, object : ObjectsCallback <Void > {
139- override fun onSuccess (result : Void ? ) {
140- continuation.resume(Unit )
141- }
140+ suspend fun LiveMap.removeCoroutine (key : String ) = supressCoroutineExceptions {
141+ suspendCancellableCoroutine<Unit > { continuation ->
142+ removeAsync(key, object : ObjectsCallback <Void > {
143+ override fun onSuccess (result : Void ? ) {
144+ continuation.resume(Unit )
145+ }
142146
143- override fun onError (exception : AblyException ? ) {
144- continuation.cancel(exception)
145- }
146- })
147+ override fun onError (exception : AblyException ? ) {
148+ continuation.cancel(exception)
149+ }
150+ })
151+ }
147152}
148153
149154@Composable
@@ -152,7 +157,9 @@ fun observeCounter(channel: Channel, root: LiveMap?, path: String): CounterState
152157 var counterValue by remember { mutableStateOf<Int ?>(null ) }
153158
154159 LaunchedEffect (root) {
155- counter = getOrCreateCounter(channel, root, path)
160+ supressCoroutineExceptions {
161+ counter = getOrCreateCounter(channel, root, path)
162+ }
156163 }
157164
158165 DisposableEffect (counter) {
@@ -225,7 +232,9 @@ fun observeMap(channel: Channel, root: LiveMap?, path: String): Pair<Map<String,
225232 var mapValue by remember { mutableStateOf<Map <String , String >>(mapOf ()) }
226233
227234 LaunchedEffect (root) {
228- map = getOrCreateMap(channel, root, path)
235+ supressCoroutineExceptions {
236+ map = getOrCreateMap(channel, root, path)
237+ }
229238 }
230239
231240 DisposableEffect (map) {
@@ -256,7 +265,9 @@ fun observeRootObject(channel: Channel): LiveMap? {
256265
257266 LaunchedEffect (channelState) {
258267 if (channelState == ChannelState .attached) {
259- root = channel.objects.getRootCoroutines()
268+ supressCoroutineExceptions {
269+ root = channel.objects.getRootCoroutines()
270+ }
260271 }
261272 }
262273
@@ -282,3 +293,10 @@ fun getRealtimeChannel(realtimeClient: AblyRealtime, channelName: String): Chann
282293
283294 return channel
284295}
296+
297+ suspend fun supressCoroutineExceptions (block : suspend () -> Unit ) {
298+ try {
299+ block()
300+ } catch (_: Exception ) {
301+ }
302+ }
0 commit comments