diff --git a/Common/src/main/java/com/tabnineCommon/general/TopicBasedState.kt b/Common/src/main/java/com/tabnineCommon/general/TopicBasedState.kt index 9941e3cf..84e14b6b 100644 --- a/Common/src/main/java/com/tabnineCommon/general/TopicBasedState.kt +++ b/Common/src/main/java/com/tabnineCommon/general/TopicBasedState.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager import com.intellij.util.messages.Topic import java.util.Optional +import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.locks.Lock import java.util.concurrent.locks.ReadWriteLock import java.util.concurrent.locks.ReentrantReadWriteLock @@ -11,6 +12,7 @@ import java.util.function.Consumer open class TopicBasedState>(private val topic: Topic, private var value: T? = null) { private val rwLock: ReadWriteLock = ReentrantReadWriteLock() + private val hasChanged = AtomicBoolean(false) open fun get() = rwLock.readLock().withLock { value @@ -20,10 +22,12 @@ open class TopicBasedState>(private val topic: Topic, priv fun set(newValue: T) { rwLock.writeLock().withLock { - val hasChanged = value != newValue + hasChanged.set(value != newValue) value = newValue + } - if (hasChanged && value != null) { + rwLock.readLock().withLock { + if (hasChanged.getAndSet(false) && value != null) { ApplicationManager.getApplication().messageBus.syncPublisher(topic).accept(value!!) } }