From 4a7692fa9a6d415a70febbd17d33460b7c1e669d Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Thu, 25 May 2023 10:09:02 +0200 Subject: [PATCH] Detect and override hooks of the same kind This change ensures that we can have at most one hook of the same action during shutdown. Closes #6767. --- .../languageserver/ShutdownHookActivator.scala | 5 +++-- .../infrastructure/shutdown/ShutdownHook.scala | 3 +++ .../projectmanager/service/MoveProjectDirCmd.scala | 13 +++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/languageserver/ShutdownHookActivator.scala b/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/languageserver/ShutdownHookActivator.scala index bb6aa2c99d05..e7e62603cbcc 100644 --- a/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/languageserver/ShutdownHookActivator.scala +++ b/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/languageserver/ShutdownHookActivator.scala @@ -36,8 +36,9 @@ class ShutdownHookActivator[F[+_, +_]: Exec: CovariantFlatMap] scheduled: List[UUID] = Nil ): Receive = { case RegisterShutdownHook(projectId, hook) => - val realHook = hook.asInstanceOf[ShutdownHook[F]] - val updated = hooks.updated(projectId, realHook :: hooks(projectId)) + val realHook = hook.asInstanceOf[ShutdownHook[F]] + val uniqueHooks = hooks(projectId).filter(!_.isSameKind(realHook)) + val updated = hooks.updated(projectId, realHook :: uniqueHooks) context.become(running(updated, scheduled)) case ProjectClosed(projectId) => diff --git a/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/shutdown/ShutdownHook.scala b/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/shutdown/ShutdownHook.scala index 030795f70c1b..74b4e7c15be5 100644 --- a/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/shutdown/ShutdownHook.scala +++ b/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/shutdown/ShutdownHook.scala @@ -9,4 +9,7 @@ trait ShutdownHook[F[+_, +_]] { */ def execute(): F[Nothing, Unit] + /** Checks if the provided `hook`` refers to the same kind of action as `this`` */ + def isSameKind(hook: ShutdownHook[F]): Boolean + } diff --git a/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/service/MoveProjectDirCmd.scala b/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/service/MoveProjectDirCmd.scala index 55d44606ca23..9c5e73583b8d 100644 --- a/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/service/MoveProjectDirCmd.scala +++ b/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/service/MoveProjectDirCmd.scala @@ -47,4 +47,17 @@ class MoveProjectDirCmd[F[+_, +_]: CovariantFlatMap: ErrorChannel]( ) } + /** Returns the project ID to which this hook refers to */ + def getProjectId(): UUID = + projectId + + /** @inheritdoc */ + override def isSameKind(hook: ShutdownHook[F]): Boolean = { + hook match { + case cmd: MoveProjectDirCmd[_] => + projectId == cmd.getProjectId() + case _ => + false + } + } }