diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-08 23:00:25 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-17 14:15:15 +0000 |
| commit | c0c0e9577e3ec810271706a7f77541c3d34138b2 (patch) | |
| tree | bed82eb6cda7b71e4b205f0ce7ad321728fee4fe /android | |
| parent | d5cff15d929dbcc3e47c7528cd9b5b4132dc5226 (diff) | |
| download | mullvadvpn-c0c0e9577e3ec810271706a7f77541c3d34138b2.tar.xz mullvadvpn-c0c0e9577e3ec810271706a7f77541c3d34138b2.zip | |
Create `autoSubscribable` property delegate
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/talpid/util/EventNotifier.kt | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/talpid/util/EventNotifier.kt b/android/src/main/kotlin/net/mullvad/talpid/util/EventNotifier.kt index 581078ab58..405722f010 100644 --- a/android/src/main/kotlin/net/mullvad/talpid/util/EventNotifier.kt +++ b/android/src/main/kotlin/net/mullvad/talpid/util/EventNotifier.kt @@ -55,3 +55,16 @@ class EventNotifier<T>(private val initialValue: T) { notify(newValue) } } + +fun <T> autoSubscribable(id: Any, fallback: T, listener: (T) -> Unit) = + observable<EventNotifier<T>?>(null) { _, old, new -> + if (old != new) { + old?.unsubscribe(id) + + if (new == null) { + listener.invoke(fallback) + } else { + new.subscribe(id, listener) + } + } + } |
