fix
This commit is contained in:
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -14,6 +14,7 @@ import {
|
|||||||
dispatchEmbedScanResult,
|
dispatchEmbedScanResult,
|
||||||
acknowledgeEmbedScanConsumed,
|
acknowledgeEmbedScanConsumed,
|
||||||
dispatchEmbedScanError,
|
dispatchEmbedScanError,
|
||||||
|
setEmbedScanHostEnabled,
|
||||||
} from "./services/provider/scan";
|
} from "./services/provider/scan";
|
||||||
import {
|
import {
|
||||||
setEmbedScanResultForwarder,
|
setEmbedScanResultForwarder,
|
||||||
@@ -139,7 +140,7 @@ function broadcastScanErrorToEmbedChildren(error) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function broadcastScanResultToEmbedChildren(result) {
|
function broadcastScanResultToEmbedChildren(result, meta) {
|
||||||
if (embedChildSources.size === 0 || result == null || result === "") {
|
if (embedChildSources.size === 0 || result == null || result === "") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -151,6 +152,7 @@ function broadcastScanResultToEmbedChildren(result) {
|
|||||||
v: EMBED_V,
|
v: EMBED_V,
|
||||||
kind: "forwardScanResult",
|
kind: "forwardScanResult",
|
||||||
result,
|
result,
|
||||||
|
scanSource: meta && meta.source,
|
||||||
},
|
},
|
||||||
"*"
|
"*"
|
||||||
);
|
);
|
||||||
@@ -217,7 +219,9 @@ function embedChildOnMessage(ev) {
|
|||||||
}
|
}
|
||||||
if (data.kind === "forwardScanResult") {
|
if (data.kind === "forwardScanResult") {
|
||||||
if (typeof data.result === "string") {
|
if (typeof data.result === "string") {
|
||||||
const consumed = dispatchEmbedScanResult(data.result);
|
const consumed = dispatchEmbedScanResult(data.result, {
|
||||||
|
source: data.scanSource,
|
||||||
|
});
|
||||||
if (consumed && resolveUseParentProxy()) {
|
if (consumed && resolveUseParentProxy()) {
|
||||||
window.parent.postMessage(
|
window.parent.postMessage(
|
||||||
{
|
{
|
||||||
@@ -349,6 +353,7 @@ export function installEmbedHost(lib) {
|
|||||||
embedHostInstalled = true;
|
embedHostInstalled = true;
|
||||||
setEmbedScanResultForwarder(broadcastScanResultToEmbedChildren);
|
setEmbedScanResultForwarder(broadcastScanResultToEmbedChildren);
|
||||||
setEmbedScanErrorForwarder(broadcastScanErrorToEmbedChildren);
|
setEmbedScanErrorForwarder(broadcastScanErrorToEmbedChildren);
|
||||||
|
setEmbedScanHostEnabled(true);
|
||||||
window.addEventListener("message", (ev) => {
|
window.addEventListener("message", (ev) => {
|
||||||
const data = ev.data;
|
const data = ev.data;
|
||||||
if (!isEmbedMessage(data)) {
|
if (!isEmbedMessage(data)) {
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ export function setEmbedScanErrorForwarder(fn) {
|
|||||||
embedScanErrorForwarder = typeof fn === "function" ? fn : null;
|
embedScanErrorForwarder = typeof fn === "function" ? fn : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function forwardEmbedScanResultIfNeeded(result) {
|
export function forwardEmbedScanResultIfNeeded(result, meta) {
|
||||||
if (embedScanResultForwarder && result != null && result !== "") {
|
if (embedScanResultForwarder && result != null && result !== "") {
|
||||||
embedScanResultForwarder(result);
|
embedScanResultForwarder(result, meta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ let _scan_error_listener_list = [];
|
|||||||
let _scan_resolve = null;
|
let _scan_resolve = null;
|
||||||
let _scan_closing = false;
|
let _scan_closing = false;
|
||||||
let _scan_next_start_time = 0;
|
let _scan_next_start_time = 0;
|
||||||
|
let _embed_scan_host_enabled = false;
|
||||||
|
|
||||||
const SCAN_RESTART_DELAY = 500;
|
const SCAN_RESTART_DELAY = 500;
|
||||||
const BRIDGE_SCAN_TIMEOUT = 5000;
|
const BRIDGE_SCAN_TIMEOUT = 5000;
|
||||||
@@ -134,10 +135,10 @@ function parseBarcodeString(input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function __checkScanner() {
|
function __checkScanner() {
|
||||||
if (_scan_listener_list.length > 0) {
|
if (_scan_listener_list.length > 0 || _embed_scan_host_enabled) {
|
||||||
startScanner((result) => {
|
startScanner((result) => {
|
||||||
result = parseBarcodeString(result);
|
result = parseBarcodeString(result);
|
||||||
__scannerResult(result);
|
__scannerResult(result, { source: "scanner", skipBeep: true });
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
stopScanner();
|
stopScanner();
|
||||||
@@ -155,10 +156,17 @@ function __match(result, match) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function __shouldSkipBeep(meta) {
|
||||||
|
if (meta && meta.skipBeep) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const source = meta && meta.source;
|
||||||
|
return source === "scanner" || source === "bridge" || source === "wx";
|
||||||
|
}
|
||||||
|
|
||||||
function __result(result) {
|
function __result(result, meta) {
|
||||||
result = parseBarcodeString(result);
|
result = parseBarcodeString(result);
|
||||||
forwardEmbedScanResultIfNeeded(result);
|
forwardEmbedScanResultIfNeeded(result, meta);
|
||||||
let matched = false;
|
let matched = false;
|
||||||
for (let i = 0; i < _scan_listener_list.length; i++) {
|
for (let i = 0; i < _scan_listener_list.length; i++) {
|
||||||
const item = _scan_listener_list[i];
|
const item = _scan_listener_list[i];
|
||||||
@@ -169,7 +177,9 @@ function __result(result) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (matched) {
|
if (matched) {
|
||||||
playScanBeep();
|
if (!__shouldSkipBeep(meta)) {
|
||||||
|
playScanBeep();
|
||||||
|
}
|
||||||
_scan_next_start_time = Date.now() + getScanRestartDelay();
|
_scan_next_start_time = Date.now() + getScanRestartDelay();
|
||||||
}
|
}
|
||||||
return matched;
|
return matched;
|
||||||
@@ -289,10 +299,10 @@ function __stopCurrentScan() {
|
|||||||
* 父页通过 postMessage 将识别结果投递到嵌入 iframe 时调用(与本地扫码枪/监听同一链路)。
|
* 父页通过 postMessage 将识别结果投递到嵌入 iframe 时调用(与本地扫码枪/监听同一链路)。
|
||||||
* @returns {boolean} 是否有监听消费了该结果
|
* @returns {boolean} 是否有监听消费了该结果
|
||||||
*/
|
*/
|
||||||
export function dispatchEmbedScanResult(raw) {
|
export function dispatchEmbedScanResult(raw, meta) {
|
||||||
const result =
|
const result =
|
||||||
typeof raw === "string" ? parseBarcodeString(raw) : raw;
|
typeof raw === "string" ? parseBarcodeString(raw) : raw;
|
||||||
return __scannerResult(result);
|
return __scannerResult(result, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -324,9 +334,9 @@ export function acknowledgeEmbedScanConsumed(raw) {
|
|||||||
resolve && resolve({ result });
|
resolve && resolve({ result });
|
||||||
}
|
}
|
||||||
|
|
||||||
function __scannerResult(result) {
|
function __scannerResult(result, meta) {
|
||||||
if (!__hasMatchedListener(result)) {
|
if (!__hasMatchedListener(result)) {
|
||||||
return __result(result);
|
return __result(result, meta);
|
||||||
}
|
}
|
||||||
if (isScanning()) {
|
if (isScanning()) {
|
||||||
const resolve = __finishScan();
|
const resolve = __finishScan();
|
||||||
@@ -336,13 +346,13 @@ function __scannerResult(result) {
|
|||||||
_scan_closing = false;
|
_scan_closing = false;
|
||||||
}, 0);
|
}, 0);
|
||||||
});
|
});
|
||||||
const matched = __result(result);
|
const matched = __result(result, meta);
|
||||||
resolve && resolve({
|
resolve && resolve({
|
||||||
result
|
result
|
||||||
});
|
});
|
||||||
return matched;
|
return matched;
|
||||||
}
|
}
|
||||||
return __result(result);
|
return __result(result, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
function __startBridgeScan() {
|
function __startBridgeScan() {
|
||||||
@@ -355,7 +365,7 @@ function __startBridgeScan() {
|
|||||||
if (!resp || !resp.result) {
|
if (!resp || !resp.result) {
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
if (__result(resp.result)) {
|
if (__result(resp.result, { source: "bridge" })) {
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
if (isScanning()) {
|
if (isScanning()) {
|
||||||
@@ -369,7 +379,7 @@ function __startBridgeScan() {
|
|||||||
if (!err || !err.result) {
|
if (!err || !err.result) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
if (__result(err.result)) {
|
if (__result(err.result, { source: "bridge" })) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
if (isScanning()) {
|
if (isScanning()) {
|
||||||
@@ -393,7 +403,7 @@ function __startWxScan() {
|
|||||||
if (!resp || !resp.result) {
|
if (!resp || !resp.result) {
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
if (__result(resp.result)) {
|
if (__result(resp.result, { source: "wx" })) {
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
return resp;
|
return resp;
|
||||||
@@ -402,7 +412,7 @@ function __startWxScan() {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
if (err && err.result) {
|
if (err && err.result) {
|
||||||
if (__result(err.result)) {
|
if (__result(err.result, { source: "wx" })) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
return err;
|
return err;
|
||||||
@@ -481,6 +491,15 @@ export function isScanning() {
|
|||||||
return _scan_status === "scanning";
|
return _scan_status === "scanning";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setEmbedScanHostEnabled(enabled) {
|
||||||
|
const nextEnabled = enabled !== false;
|
||||||
|
if (_embed_scan_host_enabled === nextEnabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_embed_scan_host_enabled = nextEnabled;
|
||||||
|
__checkScanner();
|
||||||
|
}
|
||||||
|
|
||||||
export function clear() {
|
export function clear() {
|
||||||
for (let i = 0; i < _scan_listener_list.length; i++) {
|
for (let i = 0; i < _scan_listener_list.length; i++) {
|
||||||
const item = _scan_listener_list[i];
|
const item = _scan_listener_list[i];
|
||||||
|
|||||||
Reference in New Issue
Block a user