From 405ace74ede9fe1bdc3d78a9627380d7981854c7 Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Thu, 20 Jul 2023 16:17:42 +0200 Subject: [PATCH] only reset resource state if resource is modified (#634) Signed-off-by: Andre Dietisheim --- .../intellij/kubernetes/editor/EditorResource.kt | 10 ++++++++-- .../intellij/kubernetes/editor/EditorResourceTest.kt | 8 ++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResource.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResource.kt index 2991dbdc1..d5a955ba9 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResource.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResource.kt @@ -50,10 +50,13 @@ open class EditorResource( /** * Sets the resource to this instance. Only modified versions of the same resource are processed. * Will do nothing if the given resource is a different resource in name, namespace, kind etc. + * Resets the existing resource state if a new resource is set * * @param new the new resource that should be set to this editor resource * - * @see isSameResource + * @see [areEqual] + * @see [isSameResource] + * @see [setState] */ fun setResource(new: HasMetadata) { resourceChangeMutex.withLock { @@ -61,8 +64,8 @@ open class EditorResource( if (new.isSameResource(existing) && !areEqual(new, existing)) { this.resource = new + setState(null) // reset state } - setState(null) // reset state } } @@ -90,8 +93,11 @@ open class EditorResource( /** * Returns the state of this editor resource. + * Returns a cached state if it exists, creates it if it doesn't. * * @return the state of this editor resource. + * + * @see [createState] */ fun getState(): EditorResourceState { return resourceChangeMutex.withLock { diff --git a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceTest.kt b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceTest.kt index 8ea42fe4e..6011cb6fc 100644 --- a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceTest.kt +++ b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceTest.kt @@ -311,10 +311,14 @@ class EditorResourceTest { doReturn(true) .whenever(clusterResource).isAuthorized() val editorResource = createEditorResource(POD2) + val modifiedPod2 = PodBuilder(POD2) + .editMetadata() + .withLabels(mapOf("jedi" to "yoda")) + .endMetadata() + .build() val error = Error("oh my!") editorResource.setState(error) - editorResource.setLastPushedPulled(POD3) // modified = (current resource != lastPushedPulled) - editorResource.setResource(POD2) // cause state to be recreated + editorResource.setResource(modifiedPod2) // new resource != existing resource, causes state to be reset and then recreated // when val state = editorResource.getState() // then