Skip to content

Commit

Permalink
fix lost port connect issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vaayne committed Nov 30, 2023
1 parent af158a1 commit bc228f0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
13 changes: 10 additions & 3 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ browser.commands.onCommand.addListener(function (command) {
})

browser.runtime.onConnect.addListener(function (port) {
port.onMessage.addListener(function (msg) {
handleStream(msg, port)
})
if (port.name === "stream") {
// console.log("Connected to the content script")
port.onMessage.addListener(function (msg) {
handleStream(msg, port)
})

// port.onDisconnect.addListener(() => {
// console.log("Content script disconnected")
// })
}
})

browser.runtime.onMessage.addListener(function (request, sender, sendResponse) {
Expand Down
11 changes: 10 additions & 1 deletion src/components/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
} from "~/lib/state"
import { PromptTypeEnum } from "~lib/enums"
import ContextMenuComponent from "./context_menu"
import { streamPort } from "~lib/runtime"
import browser from "webextension-polyfill"

export default function ComboxComponent() {
Expand Down Expand Up @@ -58,6 +57,16 @@ export default function ComboxComponent() {
}
}, [selectedPrompt])

const streamPort = browser.runtime.connect({ name: "stream" })

streamPort.onMessage.addListener(function (msg) {
if (msg === "[DONE]") {
setIsLoading(false)
} else {
setResponseMessage(msg)
}
})

const handleMessage = async (fouce: boolean) => {
if (!engine) {
handleToast("Please select an engine")
Expand Down
14 changes: 0 additions & 14 deletions src/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ import Draggable from "react-draggable"
import { useMessage } from "@plasmohq/messaging/hook"
import {
contextAtom,
isLoadingAtom,
isShowElementAtom,
isShowIconAtom,
isShowToastAtom,
notificationAtom,
responseMessageAtom,
selectedElementAtom,
} from "~/lib/state"
import ComboxComponent from "~components/combobox"
import DropdownMenuComponent from "~components/dropdown"
import NotificationComponent from "~components/notification"
import { OutputComponent } from "~components/output"
import { ToolsComponent } from "~components/tools"
import { streamPort } from "~lib/runtime"

export const config: PlasmoCSConfig = {
matches: ["<all_urls>"],
all_frames: false,
Expand All @@ -37,8 +33,6 @@ const positionAtom = atom<{ x: number; y: number } | null>(null)
const Index = () => {
const [screenWidth, setScreenWidth] = useState(window.innerWidth)
const setContext = useSetAtom(contextAtom)
const setResponseMessage = useSetAtom(responseMessageAtom)
const setIsLoading = useSetAtom(isLoadingAtom)
const [isShowElement, setIsShowElement] = useAtom(isShowElementAtom)
const [isShowIcon, setIsShowIcon] = useAtom(isShowIconAtom)
const notification = useAtomValue(notificationAtom)
Expand All @@ -61,14 +55,6 @@ const Index = () => {
// }
}

streamPort.onMessage.addListener(function (msg) {
if (msg === "[DONE]") {
setIsLoading(false)
} else {
setResponseMessage(msg)
}
})

const handleMouseUp = event => {
// do not update on shadowRoot
const shadowRoot = document.querySelector("plasmo-csui")?.shadowRoot
Expand Down
3 changes: 0 additions & 3 deletions src/lib/runtime.ts

This file was deleted.

0 comments on commit bc228f0

Please sign in to comment.