Skip to content

Commit

Permalink
fix(mps-sync-plugin-lib): a partially initialized registry can be dis…
Browse files Browse the repository at this point in the history
…posed (#162)

Otherwise BranchRegistry cannot be disposed if the ReplicatedModel or the IBranch was not initialized correctly.
  • Loading branch information
benedekh authored Oct 11, 2024
1 parent 460976a commit ca6d911
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class BranchRegistry : InjectableService {
private var client: ModelClientV2? = null
private var branchReference: BranchReference? = null

private lateinit var branchListener: ModelixBranchListener
private lateinit var repoChangeListener: RepositoryChangeListener
private var branchListener: ModelixBranchListener? = null
private var repoChangeListener: RepositoryChangeListener? = null

override fun initService(serviceLocator: ServiceLocator) {
this.serviceLocator = serviceLocator
Expand Down Expand Up @@ -98,11 +98,22 @@ class BranchRegistry : InjectableService {
}

override fun dispose() {
val branch = getBranch() ?: return
branch.removeListener(branchListener)
mpsRepository.removeRepositoryListener(repoChangeListener)
try {
val branch = getBranch()
if (branch != null && branchListener != null) {
branch.removeListener(branchListener!!)
}
} catch (ignored: Exception) {
// getBranch() may throw IllegalStateException exception if ReplicatedModel has not started yet
}

try {
model?.dispose()
} catch (ignored: Exception) {
// so that the other parts of the dispose() call are not affected
}

model?.dispose()
repoChangeListener?.let { mpsRepository.removeRepositoryListener(it) }

model = null
branchReference = null
Expand All @@ -111,7 +122,7 @@ class BranchRegistry : InjectableService {

private fun registerBranchListener(branch: IBranch, languageRepository: MPSLanguageRepository) {
branchListener = ModelixBranchListener(branch, serviceLocator, languageRepository)
branch.addListener(branchListener)
branch.addListener(branchListener!!)
}
}

Expand Down

0 comments on commit ca6d911

Please sign in to comment.