Skip to content

Commit

Permalink
update intellij
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Oct 26, 2023
1 parent b78bdb6 commit 9feaf3d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 80 deletions.
1 change: 0 additions & 1 deletion daemon/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type StartPreviewRequest = {
};

export type StartPreviewResponse = {
rootDir: string;
url: string;
};

Expand Down
1 change: 0 additions & 1 deletion daemon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ export async function startDaemon({
);
}
return {
rootDir,
url: `http://localhost:${previewServer.port}`,
};
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,28 @@ fun api(baseUrl: String): PreviewJsApi {
}

interface PreviewJsApi {
@POST("/workspaces/get")
suspend fun getWorkspace(@Body req: GetWorkspaceRequest): GetWorkspaceResponse

@POST("/workspaces/dispose")
suspend fun disposeWorkspace(@Body req: DisposeWorkspaceRequest): DisposeWorkspaceResponse

@POST("/crawl-file")
suspend fun crawlFile(@Body req: CrawlFileRequest): CrawlFileResponse

@POST("/previews/start")
suspend fun startPreview(@Body req: StartPreviewRequest): StartPreviewResponse

@POST("/previews/status")
suspend fun checkPreviewStatus(@Body req: CheckPreviewStatusRequest): CheckPreviewStatusResponse

@POST("/previews/stop")
suspend fun stopPreview(@Body req: StopPreviewRequest): StopPreviewResponse

@POST("/pending-files/update")
suspend fun updatePendingFile(@Body req: UpdatePendingFileRequest): UpdatePendingFileResponse
}

data class InfoResponse(
val loaderInstallDir: String,
val packageName: String,
val versionCode: String
)

data class GetWorkspaceRequest(
val absoluteFilePath: String
)

data class GetWorkspaceResponse(
val workspaceId: String?
)

data class DisposeWorkspaceRequest(
val workspaceId: String
)

class DisposeWorkspaceResponse

data class CrawlFileRequest(
val workspaceId: String,
val absoluteFilePath: String
)

data class CrawlFileResponse(
val rootDir: String?,
val previewables: List<Previewable>
)

Expand All @@ -83,15 +60,23 @@ data class Previewable(
)

data class StartPreviewRequest(
val workspaceId: String
val rootDir: String
)

data class StartPreviewResponse(
val url: String
)

data class CheckPreviewStatusRequest(
val rootDir: String
)

data class CheckPreviewStatusResponse(
val running: Boolean
)

data class StopPreviewRequest(
val workspaceId: String
val rootDir: String
)

class StopPreviewResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.project.Project
import com.previewjs.intellij.plugin.api.DisposeWorkspaceRequest
import com.previewjs.intellij.plugin.api.GetWorkspaceRequest
import com.previewjs.intellij.plugin.api.PreviewJsApi
import com.previewjs.intellij.plugin.api.api
import kotlinx.coroutines.CompletableDeferred
Expand Down Expand Up @@ -292,34 +290,6 @@ ${e.stackTraceToString()}""",
}
}

suspend fun ensureWorkspaceReady(project: Project, absoluteFilePath: String): String? {
val api = this.api ?: return null
val workspaceId = api.getWorkspace(
GetWorkspaceRequest(
absoluteFilePath = absoluteFilePath
)
).workspaceId ?: return null
var ids = workspaceIds[project]
if (ids == null) {
ids = Collections.synchronizedSet(mutableSetOf())
workspaceIds[project] = ids
project.service<ProjectService>().printToConsole("Preview.js initialised for project ${project.name}\n")
}
ids!!.add(workspaceId)
return workspaceId
}

suspend fun disposeWorkspaces(project: Project) {
val workspaceIdsToDisposeOf = workspaceIds[project].orEmpty().toMutableSet()
workspaceIds.remove(project)
for (otherProjectWorkspaceIds in workspaceIds.values) {
workspaceIdsToDisposeOf.removeAll(otherProjectWorkspaceIds)
}
for (workspaceId in workspaceIdsToDisposeOf) {
api?.disposeWorkspace(DisposeWorkspaceRequest(workspaceId))
}
}

override fun dispose() {
pingTimer?.cancel()
daemonProcess?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ProjectService(private val project: Project) : Disposable {
private var previewToolWindowActive = false

@Volatile
private var currentPreviewWorkspaceId: String? = null
private var currentPreviewRootDir: String? = null
private var componentMap = mutableMapOf<String, Pair<String, List<Previewable>>>()
private var pendingFileChanges = mutableMapOf<String, String>()

Expand Down Expand Up @@ -284,8 +284,6 @@ class ProjectService(private val project: Project) : Disposable {
return callback(emptyList())
}
service.enqueueAction(project, { api ->
val workspaceId =
service.ensureWorkspaceReady(project, file.path) ?: return@enqueueAction callback(emptyList())
val pendingText = pendingFileChanges.remove(file.path)
if (pendingText != null) {
api.updatePendingFile(
Expand All @@ -297,7 +295,6 @@ class ProjectService(private val project: Project) : Disposable {
}
val analysisResponse = api.crawlFile(
CrawlFileRequest(
workspaceId,
absoluteFilePath = file.path
)
)
Expand All @@ -309,14 +306,17 @@ class ProjectService(private val project: Project) : Disposable {

fun openPreview(absoluteFilePath: String, previewableId: String) {
service.enqueueAction(project, { api ->
val workspaceId = service.ensureWorkspaceReady(project, absoluteFilePath) ?: return@enqueueAction
currentPreviewWorkspaceId?.let {
if (workspaceId != it) {
api.stopPreview(StopPreviewRequest(workspaceId = it))
}
val analysisResponse = api.crawlFile(
CrawlFileRequest(
absoluteFilePath = absoluteFilePath
)
)
val rootDir = analysisResponse.rootDir ?: return@enqueueAction
currentPreviewRootDir?.let {
api.stopPreview(StopPreviewRequest(rootDir = it))
}
currentPreviewWorkspaceId = workspaceId
val startPreviewResponse = api.startPreview(StartPreviewRequest(workspaceId))
currentPreviewRootDir = rootDir
val startPreviewResponse = api.startPreview(StartPreviewRequest(rootDir))
val previewBaseUrl = startPreviewResponse.url
this@ProjectService.previewBaseUrl = previewBaseUrl
val previewUrl = "$previewBaseUrl?p=${URLEncoder.encode(previewableId, "utf-8")}"
Expand Down Expand Up @@ -391,17 +391,17 @@ class ProjectService(private val project: Project) : Disposable {
Disposer.dispose(it)
}
previewBrowser = null
currentPreviewWorkspaceId?.let { workspaceId ->
currentPreviewRootDir?.let { rootDir ->
if (processKilled) {
return
}
service.enqueueAction(project, { api ->
api.stopPreview(StopPreviewRequest(workspaceId = workspaceId))
api.stopPreview(StopPreviewRequest(rootDir = rootDir))
}, {
"Warning: unable to close preview"
})
}
currentPreviewWorkspaceId = null
currentPreviewRootDir = null
previewBaseUrl = null
updateStatusBarWidget()
}
Expand All @@ -416,10 +416,5 @@ class ProjectService(private val project: Project) : Disposable {
closePreview()
consoleView = null
consoleToolWindow = null
service.enqueueAction(project, {
service.disposeWorkspaces(project)
}, {
"Warning: unable to dispose of workspaces"
})
}
}

0 comments on commit 9feaf3d

Please sign in to comment.