-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sync SWRDevTools and a devtool panel (#27)
* fix: sync SWRDevTools and a devtool panel * fix: type errors
- Loading branch information
Showing
5 changed files
with
127 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,64 @@ | ||
import { DevToolsMessage } from "swr-devtools"; | ||
|
||
export type ContentMessage = | ||
| { | ||
type: "load"; | ||
} | ||
| { | ||
type: "initialized"; | ||
} | ||
| { | ||
type: "updated_cache"; | ||
payload: DevToolsMessage["payload"]; | ||
type: "updated_swr_cache"; | ||
payload: any; | ||
} | ||
| { | ||
type: "load"; | ||
}; | ||
|
||
// queued messages until a panel is displayed | ||
const queuedMessages: any[] = []; | ||
const enqueueMessage = (message: any) => { | ||
queuedMessages.push(message); | ||
}; | ||
|
||
let isDisplayedPanel = false; | ||
|
||
// proxy messages from applications to a background script | ||
const port = chrome.runtime.connect({ name: "content" }); | ||
port.onMessage.addListener((message: any) => { | ||
console.log("received from background -> content", message); | ||
// a panel has been displayed, so we sent queued messages | ||
if (message.type === "displayed_panel") { | ||
queuedMessages.forEach((m) => { | ||
port.postMessage(m); | ||
}); | ||
isDisplayedPanel = true; | ||
} | ||
}); | ||
|
||
// A new page has been loaded. | ||
// this event is sent with any pages that don't have SWRDevTools | ||
port.postMessage({ | ||
type: "initialized", | ||
type: "load", | ||
}); | ||
|
||
window.addEventListener("message", (e: MessageEvent<DevToolsMessage>) => { | ||
if (e.data?.type === "updated_swr_cache") { | ||
// console.log("received", e.data.cacheData); | ||
port.postMessage({ | ||
type: "updated_cache", | ||
payload: e.data.payload, | ||
}); | ||
switch (e.data?.type) { | ||
case "initialized": { | ||
port.postMessage(e.data); | ||
break; | ||
} | ||
case "updated_swr_cache": { | ||
if (isDisplayedPanel) { | ||
port.postMessage(e.data); | ||
} else { | ||
// enqueue a message if a panel hasn't been displayed | ||
enqueueMessage(e.data); | ||
} | ||
break; | ||
} | ||
default: { | ||
// noop | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ba34e6f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: