Skip to content

Commit bd3d32d

Browse files
authored
Merge pull request #4759 from lkk214/feat/Improve-init-and-loading
feat: Improve ProtocolClient initialization and GUI loading
2 parents 189a113 + 1c3ea67 commit bd3d32d

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinuePluginService.kt

+24-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ import com.intellij.openapi.project.DumbAware
1313
import kotlinx.coroutines.CoroutineScope
1414
import kotlinx.coroutines.Dispatchers
1515
import kotlinx.coroutines.cancel
16+
import kotlin.properties.Delegates
1617

1718
@Service(Service.Level.PROJECT)
1819
class ContinuePluginService : Disposable, DumbAware {
1920
private val coroutineScope = CoroutineScope(Dispatchers.Main)
2021
var continuePluginWindow: ContinuePluginToolWindowFactory.ContinuePluginWindow? = null
21-
var ideProtocolClient: IdeProtocolClient? = null
22+
var listener: (() -> Unit)? = null
23+
var ideProtocolClient: IdeProtocolClient? by Delegates.observable(null) { _, _, _ ->
24+
synchronized(this) { listener?.also { listener = null }?.invoke() }
25+
}
2226
var coreMessengerManager: CoreMessengerManager? = null
2327
val coreMessenger: CoreMessenger?
2428
get() = coreMessengerManager?.coreMessenger
@@ -42,6 +46,25 @@ class ContinuePluginService : Disposable, DumbAware {
4246
continuePluginWindow?.browser?.sendToWebview(messageType, data, messageId)
4347
}
4448

49+
/**
50+
* Add a listener for protocolClient initialization.
51+
* Currently, only one needs to be processed. If there are more than one,
52+
* we can use an array to add listeners to ensure that the message is processed.
53+
*/
54+
fun onProtocolClientInitialized(listener: () -> Unit) {
55+
if (ideProtocolClient == null) {
56+
synchronized(this) {
57+
if (ideProtocolClient == null) {
58+
this.listener = listener
59+
} else {
60+
listener()
61+
}
62+
}
63+
} else {
64+
listener()
65+
}
66+
}
67+
4568
fun updateLastFileSaveTimestamp() {
4669
ideProtocolClient?.updateLastFileSaveTimestamp()
4770
}

extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinueBrowser.kt

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.continuedev.continueintellijextension.toolWindow
22

33
import com.github.continuedev.continueintellijextension.activities.ContinuePluginDisposable
4-
import com.github.continuedev.continueintellijextension.constants.MessageTypes
54
import com.github.continuedev.continueintellijextension.constants.MessageTypes.Companion.PASS_THROUGH_TO_CORE
65
import com.github.continuedev.continueintellijextension.factories.CustomSchemeHandlerFactory
76
import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings
@@ -10,11 +9,11 @@ import com.github.continuedev.continueintellijextension.utils.uuid
109
import com.google.gson.Gson
1110
import com.google.gson.JsonObject
1211
import com.google.gson.JsonParser
13-
import com.intellij.openapi.components.ServiceManager
1412
import com.intellij.openapi.project.Project
1513
import com.intellij.openapi.util.Disposer
1614
import com.intellij.ui.jcef.*
1715
import com.intellij.ui.jcef.JBCefClient.Properties
16+
import com.intellij.util.application
1817
import org.cef.CefApp
1918
import org.cef.browser.CefBrowser
2019
import org.cef.handler.CefLoadHandlerAdapter
@@ -30,8 +29,10 @@ class ContinueBrowser(val project: Project, url: String) {
3029

3130
val browser: JBCefBrowser
3231

32+
val continuePluginService: ContinuePluginService = project.getService(ContinuePluginService::class.java)
33+
3334
init {
34-
val isOSREnabled = ServiceManager.getService(ContinueExtensionSettings::class.java).continueState.enableOSR
35+
val isOSREnabled = application.getService(ContinueExtensionSettings::class.java).continueState.enableOSR
3536

3637
this.browser = JBCefBrowser.createBuilder().setOffScreenRendering(isOSREnabled).build().apply {
3738
// To avoid using System.setProperty to affect other plugins,
@@ -42,7 +43,6 @@ class ContinueBrowser(val project: Project, url: String) {
4243
}
4344

4445
registerAppSchemeHandler()
45-
browser.loadURL(url)
4646
Disposer.register(ContinuePluginDisposable.getInstance(project), browser)
4747

4848
// Listen for events sent from browser
@@ -55,11 +55,6 @@ class ContinueBrowser(val project: Project, url: String) {
5555
val data = json.get("data")
5656
val messageId = json.get("messageId")?.asString
5757

58-
val continuePluginService = ServiceManager.getService(
59-
project,
60-
ContinuePluginService::class.java
61-
)
62-
6358
val respond = fun(data: Any?) {
6459
sendToWebview(messageType, data, messageId ?: uuid())
6560
}
@@ -101,6 +96,13 @@ class ContinueBrowser(val project: Project, url: String) {
10196
}
10297
}, browser.cefBrowser)
10398

99+
// Load the url only after the protocolClient is initialized,
100+
// otherwise some messages will be lost, which are some configurations when the page is loaded.
101+
// Moreover, we should add LoadHandler before loading the url.
102+
continuePluginService.onProtocolClientInitialized {
103+
browser.loadURL(url)
104+
}
105+
104106
}
105107

106108
fun executeJavaScript(browser: CefBrowser?, myJSQueryOpenInBrowser: JBCefJSQuery) {

0 commit comments

Comments
 (0)