Merge pull request #13948 from Snuffleupagus/structuredClone

Use the native `structuredClone` implementation when it's available
This commit is contained in:
Tim van der Meij 2021-10-03 13:19:36 +02:00 committed by GitHub
commit f5b79be0b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1899,13 +1899,21 @@ class PDFPageProxy {
class LoopbackPort {
constructor() {
this._listeners = [];
this._deferred = Promise.resolve(undefined);
this._deferred = Promise.resolve();
}
postMessage(obj, transfers) {
function cloneValue(value) {
function cloneValue(object) {
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
globalThis.structuredClone
) {
return globalThis.structuredClone(object, transfers);
}
// Trying to perform a structured clone close to the spec, including
// transfers.
function fallbackCloneValue(value) {
if (
typeof value === "function" ||
typeof value === "symbol" ||
@ -1942,7 +1950,7 @@ class LoopbackPort {
result = new Map();
cloned.set(value, result); // Adding to cache now for cyclic references.
for (const [key, val] of value) {
result.set(key, cloneValue(val));
result.set(key, fallbackCloneValue(val));
}
return result;
}
@ -1950,7 +1958,7 @@ class LoopbackPort {
result = new Set();
cloned.set(value, result); // Adding to cache now for cyclic references.
for (const val of value) {
result.add(cloneValue(val));
result.add(fallbackCloneValue(val));
}
return result;
}
@ -1970,12 +1978,15 @@ class LoopbackPort {
if (typeof desc.value === "function" && !value.hasOwnProperty?.(i)) {
continue;
}
result[i] = cloneValue(desc.value);
result[i] = fallbackCloneValue(desc.value);
}
return result;
}
const cloned = new WeakMap();
return fallbackCloneValue(object);
}
const event = { data: cloneValue(obj) };
this._deferred.then(() => {