This repository has been archived by the owner on Dec 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from mozilla/message-refactor
Centralize message handling to allow for multiple async listeners.
- Loading branch information
Showing
6 changed files
with
43 additions
and
28 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,33 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
/** | ||
* Centralized message handler for the background script. | ||
* | ||
* If you return a promise from the handler for browser.runtime.onMessage, | ||
* the browser will assume you're sending a message back to the sender and will | ||
* return whatever that Promise resolves to. A side effect is that multiple | ||
* async handlers will fail because the first one to run will set the final | ||
* return value, even if it resolves to `undefined`. | ||
* | ||
* A centralized message handler avoids this by checking all potential messages | ||
* in a single handler. | ||
* @module | ||
*/ | ||
|
||
import {handleConfigMessage} from 'commerce/config/background'; | ||
import {handleExtractedProductData} from 'commerce/background/extraction'; | ||
|
||
export const handlers = new Map([ | ||
['extracted-product', handleExtractedProductData], | ||
['config', handleConfigMessage], | ||
]); | ||
|
||
export default async function handleMessage(message, sender) { | ||
if (handlers.has(message.type)) { | ||
return handlers.get(message.type)(message, sender); | ||
} | ||
|
||
return undefined; | ||
} |
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