Skip to content

Commit

Permalink
only reset resource state if resource is modified (redhat-developer#634)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jul 20, 2023
1 parent e07d0b7 commit 405ace7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,22 @@ 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 {
val existing = this.resource
if (new.isSameResource(existing)
&& !areEqual(new, existing)) {
this.resource = new
setState(null) // reset state
}
setState(null) // reset state
}
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,14 @@ class EditorResourceTest {
doReturn(true)
.whenever(clusterResource).isAuthorized()
val editorResource = createEditorResource(POD2)
val modifiedPod2 = PodBuilder(POD2)
.editMetadata()
.withLabels<String, String>(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
Expand Down

0 comments on commit 405ace7

Please sign in to comment.