-
-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add port reconnection mechanism (#575)
* add async just in case --------- Co-authored-by: L❤️ ☮️ ✋ <[email protected]>
- Loading branch information
Showing
4 changed files
with
93 additions
and
38 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { type PlasmoMessaging } from "./index" | ||
|
||
export const listen = <RequestBody, ResponseBody>( | ||
handler: PlasmoMessaging.Handler<string, RequestBody, ResponseBody> | ||
) => { | ||
const metaListener = async (req, sender, sendResponse) => { | ||
await handler?.( | ||
{ | ||
...req, | ||
sender | ||
}, | ||
{ | ||
send: (p) => sendResponse(p) | ||
} | ||
) | ||
} | ||
|
||
const listener = (req, sender, sendResponse) => { | ||
metaListener(req, sender, sendResponse) | ||
return // Syncronous return to indicate this is an async listener | ||
} | ||
|
||
chrome.runtime.onMessage.addListener(listener) | ||
return () => { | ||
chrome.runtime.onMessage.removeListener(listener) | ||
} | ||
} |
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