优化++
This commit is contained in:
@@ -13,8 +13,12 @@ import { createUUID } from "./utils/uuid";
|
||||
import {
|
||||
dispatchEmbedScanResult,
|
||||
acknowledgeEmbedScanConsumed,
|
||||
dispatchEmbedScanError,
|
||||
} from "./services/provider/scan";
|
||||
import { setEmbedScanResultForwarder } from "./services/embedScanBridge";
|
||||
import {
|
||||
setEmbedScanResultForwarder,
|
||||
setEmbedScanErrorForwarder,
|
||||
} from "./services/embedScanBridge";
|
||||
|
||||
const EMBED_SOURCE = "IScanEmbed";
|
||||
const EMBED_V = 1;
|
||||
@@ -25,6 +29,8 @@ const embedChildSources = new Set();
|
||||
const EMBED_LISTENER_METHODS = new Set([
|
||||
"onScanListener",
|
||||
"offScanListener",
|
||||
"onScanErrorListener",
|
||||
"offScanErrorListener",
|
||||
"clear",
|
||||
]);
|
||||
|
||||
@@ -105,6 +111,26 @@ function hydrateEmbedParams(params, messageSource, targetOrigin) {
|
||||
return params.map(walk);
|
||||
}
|
||||
|
||||
function broadcastScanErrorToEmbedChildren(error) {
|
||||
if (embedChildSources.size === 0 || error == null || error === "") {
|
||||
return;
|
||||
}
|
||||
embedChildSources.forEach((source) => {
|
||||
try {
|
||||
source.postMessage(
|
||||
{
|
||||
source: EMBED_SOURCE,
|
||||
v: EMBED_V,
|
||||
kind: "forwardScanError",
|
||||
error,
|
||||
},
|
||||
"*"
|
||||
);
|
||||
} catch (e) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function broadcastScanResultToEmbedChildren(result) {
|
||||
if (embedChildSources.size === 0 || result == null || result === "") {
|
||||
return;
|
||||
@@ -198,6 +224,12 @@ function embedChildOnMessage(ev) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (data.kind === "forwardScanError") {
|
||||
if (typeof data.error === "string") {
|
||||
dispatchEmbedScanError(data.error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (data.kind === "invokeResult") {
|
||||
const pending = pendingInvokes[data.id];
|
||||
if (!pending) {
|
||||
@@ -307,6 +339,7 @@ export function installEmbedHost(lib) {
|
||||
}
|
||||
embedHostInstalled = true;
|
||||
setEmbedScanResultForwarder(broadcastScanResultToEmbedChildren);
|
||||
setEmbedScanErrorForwarder(broadcastScanErrorToEmbedChildren);
|
||||
window.addEventListener("message", (ev) => {
|
||||
const data = ev.data;
|
||||
if (!isEmbedMessage(data)) {
|
||||
|
||||
Reference in New Issue
Block a user