-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make children immutable to avoid concurrent modification exception #387
Draft
KovalevAndrey
wants to merge
1
commit into
master
Choose a base branch
from
concurrent-modification-exception-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,8 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
is AncestryInfo.Child -> ancestryInfo.anchor | ||
} | ||
|
||
private var isDestroyed: Boolean = false | ||
|
||
val plugins: List<Plugin> = | ||
buildContext.defaultPlugins(this) + plugins + if (this is Plugin) listOf(this) else emptyList() | ||
|
||
|
@@ -107,9 +109,8 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
|
||
internal val externalLifecycleRegistry = LifecycleRegistry(this) | ||
|
||
@VisibleForTesting | ||
internal val _children: MutableList<Node<*>> = mutableListOf() | ||
val children: List<Node<*>> get() = _children | ||
var children: List<Node<*>> = listOf() | ||
internal set | ||
|
||
internal open val lifecycleManager = LifecycleManager(this) | ||
|
||
|
@@ -145,6 +146,14 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
|
||
@CallSuper | ||
open fun onCreate() { | ||
if (isDestroyed) { | ||
RIBs.errorHandler.handleNonFatalError( | ||
"Calling onCreate when Node has been destroyed. $this", | ||
RuntimeException("Calling onCreate when Node has been destroyed. $this") | ||
) | ||
return | ||
} | ||
|
||
plugins | ||
.filterIsInstance<NodeLifecycleAware>() | ||
.forEach { it.onCreate(lifecycleManager.ribLifecycle.lifecycle) } | ||
|
@@ -209,6 +218,16 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
} | ||
|
||
open fun onDestroy(isRecreating: Boolean) { | ||
if (isDestroyed) { | ||
RIBs.errorHandler.handleNonFatalError( | ||
"Calling onDestroy when Node has been destroyed. $this", | ||
RuntimeException("Calling onDestroy when Node has been destroyed. $this") | ||
) | ||
return | ||
} | ||
|
||
isDestroyed = true | ||
|
||
if (view != null) { | ||
RIBs.errorHandler.handleNonFatalError( | ||
"View was not detached before node detach!", | ||
|
@@ -237,7 +256,8 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
@MainThread | ||
fun attachChildNode(child: Node<*>) { | ||
verifyNotRoot(child) | ||
_children.add(child) | ||
val newChildren = children.toMutableList().apply { add(child) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
children = newChildren | ||
lifecycleManager.onAttachChild(child) | ||
child.onCreate() | ||
onAttachChildNode(child) | ||
|
@@ -309,7 +329,9 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
@MainThread | ||
fun detachChildNode(child: Node<*>, isRecreating: Boolean) { | ||
plugins.filterIsInstance<SubtreeChangeAware>().forEach { it.onChildDetached(child) } | ||
_children.remove(child) | ||
|
||
val newChildren = children.toMutableList().apply { remove(child) } | ||
children = newChildren | ||
child.onDestroy(isRecreating) | ||
} | ||
|
||
|
@@ -325,6 +347,14 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
* To be called from the hosting environment (Activity, Fragment, etc.) | ||
*/ | ||
fun onStart() { | ||
if (isDestroyed) { | ||
RIBs.errorHandler.handleNonFatalError( | ||
"Calling onStart when Node has been destroyed. $this", | ||
RuntimeException("Calling onStart when Node has been destroyed. $this") | ||
) | ||
return | ||
} | ||
|
||
lifecycleManager.onStartExternal() | ||
plugins.filterIsInstance<AndroidLifecycleAware>().forEach { it.onStart() } | ||
} | ||
|
@@ -333,6 +363,14 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
* To be called from the hosting environment (Activity, Fragment, etc.) | ||
*/ | ||
fun onStop() { | ||
if (isDestroyed) { | ||
RIBs.errorHandler.handleNonFatalError( | ||
"Calling onStop when Node has been destroyed. $this", | ||
RuntimeException("Calling onStop when Node has been destroyed. $this") | ||
) | ||
return | ||
} | ||
|
||
lifecycleManager.onStopExternal() | ||
plugins.filterIsInstance<AndroidLifecycleAware>().forEach { it.onStop() } | ||
} | ||
|
@@ -341,6 +379,14 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
* To be called from the hosting environment (Activity, Fragment, etc.) | ||
*/ | ||
fun onResume() { | ||
if (isDestroyed) { | ||
RIBs.errorHandler.handleNonFatalError( | ||
"Calling onResume when Node has been destroyed. $this", | ||
RuntimeException("Calling onResume when Node has been destroyed. $this") | ||
) | ||
return | ||
} | ||
|
||
lifecycleManager.onResumeExternal() | ||
plugins.filterIsInstance<AndroidLifecycleAware>().forEach { it.onResume() } | ||
} | ||
|
@@ -349,6 +395,14 @@ open class Node<V : RibView> @VisibleForTesting internal constructor( | |
* To be called from the hosting environment (Activity, Fragment, etc.) | ||
*/ | ||
fun onPause() { | ||
if (isDestroyed) { | ||
RIBs.errorHandler.handleNonFatalError( | ||
"Calling onPause when Node has been destroyed. $this", | ||
RuntimeException("Calling onPause when Node has been destroyed. $this") | ||
) | ||
return | ||
} | ||
|
||
lifecycleManager.onPauseExternal() | ||
plugins.filterIsInstance<AndroidLifecycleAware>().forEach { it.onPause() } | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get() = lifecycleManager.ribLifecycle.state == Lifecycle.DESTROYED
to eliminate possibility of desynchronisation of properties.