mirror of https://github.com/iSoron/uhabits.git
parent
262b9460bd
commit
5d1f5168ad
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform.concurrency
|
||||
|
||||
class Observable<T> {
|
||||
private val listeners = mutableListOf<T>()
|
||||
|
||||
fun addListener(listener: T) {
|
||||
listeners.add(listener)
|
||||
}
|
||||
|
||||
fun notifyListeners(action: (T) -> Unit) {
|
||||
for (l in listeners) action.invoke(l)
|
||||
}
|
||||
|
||||
fun removeListener(listener: T) {
|
||||
listeners.remove(listener)
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform.concurrency
|
||||
|
||||
import java.util.concurrent.*
|
||||
import kotlin.test.*
|
||||
|
||||
class ObservableTest {
|
||||
|
||||
interface TestListener {
|
||||
fun onDataChanged(data: Int)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
val latch = CountDownLatch(1)
|
||||
val listener = object : TestListener {
|
||||
override fun onDataChanged(data: Int) {
|
||||
assertEquals(42, data)
|
||||
latch.countDown()
|
||||
}
|
||||
}
|
||||
val observable = Observable<TestListener>()
|
||||
observable.addListener(listener)
|
||||
observable.notifyListeners { l ->
|
||||
l.onDataChanged(42)
|
||||
}
|
||||
observable.removeListener(listener)
|
||||
assertTrue(latch.await(3, TimeUnit.SECONDS))
|
||||
}
|
||||
}
|
Loading…
Reference in new issue