Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
move ports higher in file and return true in the correct place to kee… (
Browse files Browse the repository at this point in the history
#190)

* move ports higher in file and return true in the correct place to keep connections open

* listen to the response not the request :face-palm:

---------

Co-authored-by: Kayla Fitzsimmons <[email protected]>
  • Loading branch information
fitzk and Kayla Fitzsimmons authored Sep 20, 2024
1 parent 52be27e commit eaaa9da
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions apps/mocksi-lite-next/src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ const MOCKSI_AUTH = "mocksi-auth";
let fallbackTab: null | chrome.tabs.Tab = null;
let prevLayoutEvent = "";

let mainIframeSrcPort: null | chrome.runtime.Port = null;
let topIframeSrcPort: null | chrome.runtime.Port = null;

addEventListener("install", () => {
// TODO test if this works on other browsers
chrome.tabs.create({
url: import.meta.env.VITE_NEST_APP,
});
});

chrome.runtime.onConnectExternal.addListener((port) => {
console.log("connecting...", port);
if (port.name === "extension/main") {
mainIframeSrcPort = port;
}
if (port.name === "extension/top") {
topIframeSrcPort = port;
}
});

const getAuth = async (): Promise<null | {
accessToken: string;
email: string;
Expand Down Expand Up @@ -107,31 +127,11 @@ async function showPlayIcon(tabId: number) {
});
}

addEventListener("install", () => {
// TODO test if this works on other browsers
chrome.tabs.create({
url: import.meta.env.VITE_NEST_APP,
});
});

let mainIframeSrcPort: null | chrome.runtime.Port = null;
let topIframeSrcPort: null | chrome.runtime.Port = null;

chrome.runtime.onConnectExternal.addListener((port) => {
console.log("connecting...", port);
if (port.name === "extension/main") {
mainIframeSrcPort = port;
}
if (port.name === "extension/top") {
topIframeSrcPort = port;
}
});

// when user clicks toolbar mount extension
chrome.action.onClicked.addListener((tab) => {
if (!tab?.id) {
console.log("No tab exits click, could not mount extension");
return;
return true;
}
// store the tab they clicked on to open the extension
// so we can use it as a fallback
Expand All @@ -147,6 +147,7 @@ chrome.action.onClicked.addListener((tab) => {
});
prevLayoutEvent = LayoutEvents.HIDE;
}
return true;
});

chrome.runtime.onMessageExternal.addListener(
Expand Down Expand Up @@ -199,12 +200,7 @@ chrome.runtime.onMessageExternal.addListener(
} else {
const tab = await getCurrentTab();
if (!tab?.id) {
sendResponse({
message: LayoutEvents.NO_TAB,
source: "background",
status: "ok",
});
console.error("No tab found");
console.error("No tab found: ");
return true;
}

Expand Down Expand Up @@ -243,9 +239,9 @@ chrome.runtime.onMessageExternal.addListener(
}
}
if (
request.message === AppEvents.EDIT_DEMO_START ||
request.message === DemoEditEvents.NEW_EDIT ||
request.message === DemoEditEvents.CHAT_RESPONSE
response.message === AppEvents.EDIT_DEMO_START ||
response.message === DemoEditEvents.CHAT_RESPONSE ||
response.message === DemoEditEvents.NEW_EDIT
) {
// notify extension/top # of edits changed
if (topIframeSrcPort) {
Expand All @@ -259,11 +255,10 @@ chrome.runtime.onMessageExternal.addListener(
}

sendResponse(response);
return true;
});
return true;
}
})();

return true;
},
);

0 comments on commit eaaa9da

Please sign in to comment.