Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertp committed Aug 11, 2022
1 parent 7a9047f commit e1c98e3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ class SaveFileHandler(
replyTo ! ResponseError(Some(id), Errors.RequestTimeout)
context.stop(self)

case FileSaved(_) =>
case FileSaved =>
replyTo ! ResponseResult(SaveFile, id, Unused)
cancellable.cancel()
context.stop(self)

case SaveFailed(fsFailure, _) =>
case SaveFailed(fsFailure) =>
replyTo ! ResponseError(
Some(id),
FileSystemFailureMapper.mapFailure(fsFailure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ class CollaborativeBuffer(
clientId,
ContentVersion(clientVersion),
autoSave,
None
isAutoSave = false
)
case AutoSave(replyTo, clientId, clientVersion) =>
case AutoSave(clientId, clientVersion) =>
saveFile(
buffer,
clients,
lockHolder,
clientId,
clientVersion,
autoSave.removed(clientId),
Some(replyTo)
isAutoSave = true
)
}

Expand All @@ -186,30 +186,32 @@ class CollaborativeBuffer(
clients: Map[ClientId, JsonSession],
autoSave: Map[ClientId, Cancellable],
lockHolder: Option[JsonSession],
replyTo: ActorRef,
timeoutCancellable: Cancellable,
triggeredByAutoSave: Boolean
replyTo: Option[ActorRef],
timeoutCancellable: Cancellable
): Receive = {
case IOTimeout =>
replyTo ! SaveFailed(OperationTimeout, triggeredByAutoSave)
replyTo.foreach(_ ! SaveFailed(OperationTimeout))
unstashAll()
context.become(
collaborativeEditing(buffer, clients, lockHolder, autoSave)
)

case WriteFileResult(Left(failure)) =>
replyTo ! SaveFailed(failure, triggeredByAutoSave)

replyTo.foreach(_ ! SaveFailed(failure))
unstashAll()
timeoutCancellable.cancel()
context.become(
collaborativeEditing(buffer, clients, lockHolder, autoSave)
)

case WriteFileResult(Right(())) =>
replyTo ! FileSaved(triggeredByAutoSave)
if (triggeredByAutoSave)
clients.values.foreach { _.rpcController ! FileAutoSaved(bufferPath) }
replyTo match {
case Some(replyTo) => replyTo ! FileSaved
case None =>
clients.values.foreach {
_.rpcController ! FileAutoSaved(bufferPath)
}
}
unstashAll()
timeoutCancellable.cancel()
context.become(
Expand All @@ -227,10 +229,9 @@ class CollaborativeBuffer(
clientId: ClientId,
clientVersion: ContentVersion,
currentAutoSaves: Map[ClientId, Cancellable],
autoSaveReplyTo: Option[ActorRef]
isAutoSave: Boolean
): Unit = {
val hasLock = lockHolder.exists(_.clientId == clientId)
val triggeredByAutoSave = autoSaveReplyTo.nonEmpty
val hasLock = lockHolder.exists(_.clientId == clientId)
if (hasLock) {
if (clientVersion == buffer.version) {
fileManager ! FileManagerProtocol.WriteFile(
Expand All @@ -247,26 +248,24 @@ class CollaborativeBuffer(
clients,
currentAutoSaves.removed(clientId),
lockHolder,
autoSaveReplyTo.getOrElse(sender()),
timeoutCancellable,
autoSaveReplyTo.nonEmpty
if (isAutoSave) None else Some(sender()),
timeoutCancellable
)
)
} else if (autoSaveReplyTo.isEmpty)
} else if (!isAutoSave)
sender() ! SaveFileInvalidVersion(
clientVersion.toHexString,
buffer.version.toHexString
)
} else {
if (!triggeredByAutoSave) {
if (!isAutoSave) {
sender() ! SaveDenied
}
}
}

private def upsertAutoSaveTimer(
currentAutoSave: Map[ClientId, Cancellable],
replyTo: ActorRef,
clientId: ClientId,
clientVersion: ContentVersion
): Map[ClientId, Cancellable] = {
Expand All @@ -280,7 +279,7 @@ class CollaborativeBuffer(
.scheduleOnce(
delay,
self,
AutoSave(replyTo, clientId, clientVersion)
AutoSave(clientId, clientVersion)
)
)
)
Expand Down Expand Up @@ -315,7 +314,6 @@ class CollaborativeBuffer(
)
val newAutoSave: Map[ClientId, Cancellable] = upsertAutoSaveTimer(
autoSave,
sender(),
clientId,
modifiedBuffer.version
)
Expand Down Expand Up @@ -347,7 +345,6 @@ class CollaborativeBuffer(
)
val newAutoSave: Map[ClientId, Cancellable] = upsertAutoSaveTimer(
autoSave,
sender(),
clientId,
modifiedBuffer.version
)
Expand Down Expand Up @@ -596,7 +593,6 @@ object CollaborativeBuffer {
case object IOTimeout

private case class AutoSave(
replyTo: ActorRef,
clientId: ClientId,
clientVersion: ContentVersion
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,8 @@ object TextProtocol {
sealed trait SaveFileResult

/** Signals that saving a file was executed successfully.
*
* @param autoSave if true, saving was triggered by autoSave action
*/
case class FileSaved(autoSave: Boolean) extends SaveFileResult
case object FileSaved extends SaveFileResult

/** Signals that the client doesn't hold write lock to the buffer.
*/
Expand All @@ -172,9 +170,7 @@ object TextProtocol {
/** Signals that saving a file failed due to IO error.
*
* @param fsFailure a filesystem failure
* @param autoSave if true, saving was triggered by autoSave action
*/
case class SaveFailed(fsFailure: FileSystemFailure, autoSave: Boolean)
extends SaveFileResult
case class SaveFailed(fsFailure: FileSystemFailure) extends SaveFileResult

}

0 comments on commit e1c98e3

Please sign in to comment.