https://kotlinlang.org logo
#coroutines
Title
# coroutines
u

ursus

06/19/2019, 1:48 AM
https://github.com/JakeWharton/SdkSearch/blob/master/sync/src/commonMain/kotlin/com/jakewharton/sdksearch/sync/ItemSynchronizer.kt
Copy code
private val _state = ConflatedBroadcastChannel<SyncStatus>()
   val state: ReceiveChannel<SyncStatus> get() = _state.openSubscription()

  fun forceSync() {
    _state.offer(SyncStatus.SYNC)
    GlobalScope.launch {
      val result = if (load()) SyncStatus.IDLE else SyncStatus.FAILED
      _state.offer(result)
    }
  }
how would you handle this if forceSync() took an parameter? Channel per params would be needed @gildor
g

gildor

06/19/2019, 1:50 AM
But what exactly it should sync?
In this example it just reads current status again
u

ursus

06/19/2019, 1:52 AM
Im imagining how to expand this, if forceSync took parameter, which would narrow down the sync operation for example
which means you could have mutiple syncs (per param )going on at the same time, which implies channel per running sync
g

gildor

06/19/2019, 1:53 AM
if it’s kinda continue of discussiion with
postMessageOBservable
, than this forceSync with param just would clear cached value
But again, semantics of your code is different, you just post value and do not await other values
u

ursus

06/19/2019, 1:54 AM
..would clear cached value? I dont follow
g

gildor

06/19/2019, 1:54 AM
if you need stream of events (like in this case), you need some different way
again, I’m just guessing what you want to achive
u

ursus

06/19/2019, 1:54 AM
channel is a stream of events, isnt it?
g

gildor

06/19/2019, 1:54 AM
semantics of this class from SdkSearch is pretty clear for me
yes, channeels is a stream of events
u

ursus

06/19/2019, 1:54 AM
im trying to achieve
forceSync(target: Target) {}
instead of
forceSync()
where Target is for example Foo, Bar tables, whatever
so sync of Foo and sync of Bar can run in parallel, and you need states of both in various parts of ui
tldr; you cannot have single channel for this, I think
g

gildor

06/19/2019, 1:57 AM
Could you show full example of such class
u

ursus

06/19/2019, 1:58 AM
I dont have an example, im just imagining this Jake's class how would one extend it, since now forceSync presumably only syncs everything, therefore no param is needed and single _state Channel is sufficient
g

gildor

06/19/2019, 1:59 AM
this class exposes one channel of events
u

ursus

06/19/2019, 1:59 AM
yes, which would not do if we were to parametrize the forceSync function
g

gildor

06/19/2019, 1:59 AM
if you need abstraction that exposes different channel depending on Target it will be very different, so forceSync is not your only problem in this case
u

ursus

06/19/2019, 1:59 AM
yes, possibly
g

gildor

06/19/2019, 1:59 AM
definitely
u

ursus

06/19/2019, 2:00 AM
so..how? map of channels?
g

gildor

06/19/2019, 2:00 AM
I don’t know, depending on your case and semantics which you want to have
there is no universal solution
u

ursus

06/19/2019, 2:01 AM
well Id like semantincs like in the sample but be able to add params to forceSync
g

gildor

06/19/2019, 2:01 AM
If we talking about this example, you can build some manager of ItemSynchronizer that create new ItemSynchronizer for each Target
u

ursus

06/19/2019, 2:02 AM
true, buts thats just moving the map from synchronizer to manager, right?
i.e. map of channels becomes map of itemsynchronizers
g

gildor

06/19/2019, 2:02 AM
well Id like semantincs like in the sample but be able to add params to forceSync
It’s not the only thing what you need, I believe you also need some sort of parameterized
state
which require completely different implementation
u

ursus

06/19/2019, 2:03 AM
yea I thought about parametrizing State like State(target) and then filter in the callsite
but, that would have issue of replays,i.e it would only cache last, regardless of target, you'd need cache per target, to not miss stuff
in which case you might awswell have map of channels per target
this all COULD work if Target is exhaustive, like enum
but what if there are more variable params like Int or whatever
@jw maybe?
3 Views