question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConflatedBroadcastChannel as ReadWriteProperty

See original GitHub issue

Hello,

In our code base, we found useful to define the folowing extension functions:

operator fun <T> ConflatedBroadcastChannel<T>.getValue(thisRef: Any, property: KProperty<*>): T = value

operator fun <T> ConflatedBroadcastChannel<T>.setValue(thisRef: Any, property: KProperty<*>, value: T) {
  offer(value)
}

They allow us to use the following pattern:

class MyClass {
  private val _value = ConflatedBroadcastChannel(0)
  var value by _value 

  fun openValueSubscription() = _value.openSubscription()
}

May I ask what do you think about this ? Do you think it is an anti-pattern ?

  • If yes, why? And how would you achieve better generic observer pattern with coroutines?
  • If no, what would you think about making ConflatedBroadcastChannel implement ReadWriteProperty?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:20 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
fvascocommented, Mar 6, 2018

Make ConflatedSubscribable implement ReadOnlyPropery and ConflatedBroadcastChannel implement ReadWriteProperty

I consider better extending the interface (your alternative).

1reaction
jcornazcommented, Mar 6, 2018

Thanks @fvasco, I like the interfaces you propose.

I would only propose these changes:

  • Rename SubscribableChannel by Subscribable and ConflatedSubscribableChannel by ConflatedSubscribable because they are not necessarily Channel. They only provide a way to open new channels.
  • Add out variance for SubscribableChannel and ConflatedSubscribableChannel

And I would either:

Make ConflatedSubscribable implement ReadOnlyPropery and ConflatedBroadcastChannel implement ReadWriteProperty

Or provide:

operator fun <T> ConflatedSubscribable<T>.getValue(thisRef: Any, property: KProperty<*>): T = value
operator fun <T> ConflatedBroadcastChannel<T>.setValue(thisRef: Any, property: KProperty<*>, value: T) { this.value = value }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin delegated property for Datastore Preferences library
OnSharedPreferenceChangeListener, emit changes into ConflatedBroadcastChannel and then convert it into Flow. Firstly, this approach produces a ...
Read more >
ReadWriteProperty - Kotlin Programming Language
Implements the core logic of a property delegate for a read/write property that calls callback functions when changed. abstract class ObservableProperty<V> :
Read more >
Kotlin delegated property for Datastore Preferences library
Secondly, ConflatedBroadcastChannel deprecated and should be replaced with StateFlow. In this case, DataStore comes out as the winner.
Read more >
Kotlin ConflatedBroadcastChannel.offer() doesn't work?
Make sure you properly work with receiving values. If you use the ConflatedBroadcastChannel, you can use either OpenSubscription to get a ...
Read more >
Automated Malware Analysis Report for LBKj5ozFfN.apk
ConflatedBroadcastChannel.kotlin_metadata, data, 1955 ... ReadWriteProperty.kotlin_metadata, data, 293. MutexImpl.kotlin_metadata, data, 2181.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found