Skip to content

Commit

Permalink
Move WorkspaceAction into WorkspaceActor
Browse files Browse the repository at this point in the history
  • Loading branch information
efoerster committed Mar 7, 2019
1 parent 637b51b commit ee1cabb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
9 changes: 0 additions & 9 deletions src/main/kotlin/texlab/WorkspaceAction.kt

This file was deleted.

16 changes: 11 additions & 5 deletions src/main/kotlin/texlab/WorkspaceActor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import kotlin.coroutines.CoroutineContext
class WorkspaceActor : CoroutineScope {
override val coroutineContext: CoroutineContext = Dispatchers.Default + Job()

private val actor = actor<WorkspaceAction> {
private val actor = actor<Action> {
var documents = listOf<Document>()
for (message in channel) {
val workspace = Workspace(documents)
when (message) {
is WorkspaceAction.Get -> {
is Action.Get -> {
message.response.complete(workspace)
}
is WorkspaceAction.Put -> {
is Action.Put -> {
val document = message.updater(workspace)
documents = documents.filterNot { it.uri == document.uri }
.plus(document)
Expand All @@ -27,7 +27,7 @@ class WorkspaceActor : CoroutineScope {

suspend fun get(): Workspace {
val response = CompletableDeferred<Workspace>()
actor.send(WorkspaceAction.Get(response))
actor.send(Action.Get(response))
return response.await()
}

Expand All @@ -36,6 +36,12 @@ class WorkspaceActor : CoroutineScope {
}

fun put(updater: (Workspace) -> Document) = runBlocking {
actor.send(WorkspaceAction.Put(updater))
actor.send(Action.Put(updater))
}

private sealed class Action {
class Get(val response: CompletableDeferred<Workspace>) : Action()

class Put(val updater: (Workspace) -> Document) : Action()
}
}

0 comments on commit ee1cabb

Please sign in to comment.