Is there a way I can "connect" another callsite to...
# coroutines
u
Is there a way I can "connect" another callsite to a running coroutine? sort of like hot observable, where observer can join in on the receiving of the value produced
g
You can use BroadcastChannel for this
u
What about if you have params for that cooroutine? Youd need a key <-> param mapping, to get that Channel instance; most likely this should be a returned from the function, unless the caller chooses the id (key) but youll have different problems
and you cant just "anonymously" create that receiver, where for observables this would be hidden
g
What do you mean about anonymous receiver?
u
if the coroutine is parametrized, in order to get those values, youd need to get correct instance of channel
g
Hard to say with this description, do you have some example, maybe, if you know how to do that, example of Rx implementation
u
yes, second
Copy code
private val someObservablesMap = hashMap<String, Observable<SomeResult>>

fun someObservable(someId: String) : Observable<SomeResult> {
	val obs = someObservable.getOrPut(someId) {
		Observable.fromCallable or whatever
	}
	return obs
}
callsite
Copy code
someObservable("abc")
  .subscribe {}
or even better if the params is not a ID, just some data, and those are mapped into an id internally
g
I don't see any connection here, you create observable which will be started again on subscription
u
yes I omitted the hotness but okay ill add it in
Copy code
class SomeManager {
	private val someObservablesMap = hashMap<String, Observable<SomeResult>>

	fun someObservable(someId: String) : Observable<SomeResult> {
		val obs = someObservable.getOrPut(someId) {
			Observable.fromCallable or whatever
				.refCount()
		}
		return obs
	}
}

call site

 someObservable("abc")
  .subscribe {}
g
With coroutine it's even easier, if you need it one value, use HashMap<String, suspend () -> SomeResult>
Exactly, you not only omit that this coroutine is hot, but also it returns single value, so I really don't see why do you need Observable here
u
im not saying I need observable, im researching coroutines and I know rx, and this is what Im missing
g
It's hard to say what you want to do in this example, but if you just want to cache result of async operation you can just use Deffered
u
because if you sideeffect to a Channel which isa different instance, how would you tell the caller the id of the channel to which it needs to subscribe to
g
See, to convert this snippet to coroutines you don't need channel
u
HashMap<String, suspend () -> SomeResult>
what would be that lamba? I dont follow
g
Still not sure what you mean, what is your use case? Do you actually want to emit multiple events
This lambda is suspend function which replaces Observable.fromCallable
u
No just one, my issue is scoping, say you have MessageManager.postMessage .. postMessage needs to complete regardless of if UI which triggered it is still there
g
Because Observable.fromCallable is essentially the same as suspend lambda
u
in Rx I'd do this via hot observables, and if UI comes back, it "reconnectes" to the same observable in order to get the return value
g
But you cannot reconnect to cold Observable, like in your example
u
my example is hot (refCount), I just forgot it sorry, I added it in the edited sample
g
Hot observable in coroutines is Channel
For single value you don't need channel
u
well yes but not really, since you need a way to grab the correct channel from the MessageManager; its an indirection, sideffecting of suspending function to the channel/relay
g
Just use
async {}
it will just cache result in Deffered
u
I dont think that will help, since, what if your UI instance goes away? How would you connect to the channel once it comes back (new UI instance)?
sec I'll write a sample
g
You just call openSubscription() or asFlow() or call await() in case of Deferred
u
I think you meant this
Copy code
suspend fun postMessage(someText: String) {
	val requestUuid = createUuiFromText(someText)

	channels.getOrPut(requestUuid) { Channel() }
	// insert to database
	channels[requestUuid].send(State.SENDING)
	// apiClient.actuallySend
	channels[requestUuid].send(State.SENT)
}
my issue is, how would you let the callsite (ui) know about the requestUuid key, so it can grab the channel instance?
you need some sort of a channel of channels
whereas with hot observables Id just subscribe to the observable with same param and Id get the same instance from the hashmap
basically the parametrization of the postMessage is the core issue, you cannot just blindly MessageManager.stateChannel ..
g
No, i don’t mean this
Quite similar implementation would be:
Copy code
private val someObservablesMap = hashMap<String, BroadcastChannel<SomeResult>>

fun someChannel(someId: String) : ReceiveChannel<SomeResult> {
    val obs = someObservable.getOrPut(someId) {
        BroadcastChannel or whatever
    }
    return obs.openSubscription() // or asFlow() 
}

someChannel("abc")
  .consumeEach {}
This version don’t have
refcount()
feature to unsubscribe, only
publish()
to broadcast value
My main question is what
bservable.fromCallable or whatever.refCount()
should do
I mean you really want to unsubscribe if all subscribers are unsubscribed?
u
ok Ill fill the body, sec
g
If so, there is no such operator as refCount() in channel, there is proposal to provide connectable Flow abstractions: https://github.com/Kotlin/kotlinx.coroutines/issues/1086
u
yes im aware of flow, im actually waiting for it; since as I said, flatmapping of singles is stupid, regular suspending functions is better and flow as replacement for Observables and im good
here is the body
Copy code
class MessageManager {
    private val observablesMap = hashMap<String, Observable<Unit>>

    fun postMessageObservable(someText: String) : Observable<Unit> {
    	val key = sha1(someText)
        val obs = someObservable.getOrPut(key) {
            Observable.fromCallable {
            	// insert message to db with MessageState.SENDING
            	// apiClient.sendMessage(..)
            	// update message to db with MessageState.SENT
			}
            .publish()
            .autoConnect()
        }
        return obs
    }
}
I know that Observable.fromCallable is synchronous, it could be flatMaps of singles but whatever, doesnt matter for this example
g
Observable.fromCallable {}.refCount()
is not valid code
You cannot refCount() Observable, only ConnectableObservable
u
yes, sorry, im writing from memory
g
Could you just explain semantics, how this should work
u
I edited it, added the publish, changed it to autoconnect
okay so I want to post a message, its a MessageManager responsibility, and the posting action should be scoped to the MessageManager, meaning it should only be canceled, when MessageManager is closed -- and UI should just observe the result of this action, i.e. show some toast Message sent or whatever
UI can come and go ..its android, and postMessage should complete normalny, regardless of if UI is there, hence the publish.autoConnect
but if you come back to the same screen again (but different instance), you should be able to hook back in to the running postMessage observable, if its running and show the toast when finished
technically it should be Single not Obserable, but publish is not defined on Single
g
publish is not defined on Single
Exactly
u
but you can have observable tha only emits 1 and completes, nothing wrong with that
ist just semantics
g
Isn’t you need something like this:
Copy code
class MessageManager {
    private val observablesMap = hashMapOf<String, Deferred<Unit>>()

    fun postMessageObservable(someText: String) : Deferred<Unit> {
        val key = sha1(someText)
        val obs = observablesMap.getOrPut(key) {
            GlobalScope.async(start = CoroutineStart.LAZY) {
                // insert message to db with MessageState.SENDING
                // apiClient.sendMessage(..)
                // update message to db with MessageState.SENT
                Unit
            }
        }
        return obs
    }
}
Not so idiomatic (upd: after fix more idiomatic that version with observable), and you may have problems with not-thread same hashmap (but you already have it in your example)
u
yes concurrent map but nevermind that
what would be the idiomatic way please?
g
But semantically the same, it’s lazy, will be shared with multiple consumers and forever cache value
u
suspend + sideffect to channel would be idiiomatic?
g
sorry, I just now realized that you don’t need refCount() feature here, so you just start it once and cache forefere
no, this is most idiomatic way imo
you even don’t need Deferred
wait
u
its pseudo code, yes youd clear the map in onFinally { observablesMap.remove(this) }
g
Done:
Copy code
class MessageManager {
    private val observablesMap = hashMapOf<String, Deferred<Unit>>()

    suspend fun postMessageObservable(someText: String) {
        val key = sha1(someText)
        val obs = observablesMap.getOrPut(key) {
            GlobalScope.async(start = CoroutineStart.LAZY) {
                // insert message to db with MessageState.SENDING
                // apiClient.sendMessage(..)
                // update message to db with MessageState.SENT

                Unit

            }
        }
        return obs.await()
    }
}
UPD: Careful with scope, GlobalScope here only for example
so no more streams, just suspending function to post message
u
hmm nice, btw that would multicast in case I needed it or cant I have multiple subs to it?
g
there is no subscription
you just suspend
u
Id need a channel for that?
g
you call it and receive result immidiately if message is delievered or suspend until it’s sending
no
because Observable with 1 element is just a suspend function
u
I mean if multipleplaces need to know the result, id need some pipe like Subject / Channel I think?
yes, this is solved, im following up
g
what I recommend to use in one of my first messages, but semantics of you code was not clear for me
mean if multipleplaces need to know the result
If you cache the same result, there is no problem, Deferred caches it
u
no no you solved it with the last messge, im following up as how would you mutlicast this result to multiple places
Deffered is like a future?
Yes, Deffered is like a suspendable fututre
u
kk thanks a lot
g
But than you start using refCount() which semantics is quite different, it will not cache value forever, only while someone is subscribed
u
btw do you have an opinion on the channel of channels?
or observable<observable<PostMessageState>>
g
I would avoid it if I have better way, but probably sometimes it’s the only way
Copy code
fun forceSync() {
    _state.offer(SyncStatus.SYNC)
    GlobalScope.launch {
      val result = if (load()) SyncStatus.IDLE else SyncStatus.FAILED
      _state.offer(result)
    }
  }
basically this
g
Also, in my example there is GlobalScope, it’s just an example, most probably you need own scope for MessageManager
u
but what if forceSync has some parameter, I dont know like Target.Foo, Target.Bar which it should sync
what would you do in this case please?
g
What is Target.Foo? Anyway, I would start new thread instead, do not start new discussion here
u
ok I will