Merge pull request #13948 from Snuffleupagus/structuredClone
Use the native `structuredClone` implementation when it's available
This commit is contained in:
commit
f5b79be0b7
@ -1899,13 +1899,21 @@ class PDFPageProxy {
|
|||||||
class LoopbackPort {
|
class LoopbackPort {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._listeners = [];
|
this._listeners = [];
|
||||||
this._deferred = Promise.resolve(undefined);
|
this._deferred = Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
postMessage(obj, transfers) {
|
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
|
// Trying to perform a structured clone close to the spec, including
|
||||||
// transfers.
|
// transfers.
|
||||||
|
function fallbackCloneValue(value) {
|
||||||
if (
|
if (
|
||||||
typeof value === "function" ||
|
typeof value === "function" ||
|
||||||
typeof value === "symbol" ||
|
typeof value === "symbol" ||
|
||||||
@ -1942,7 +1950,7 @@ class LoopbackPort {
|
|||||||
result = new Map();
|
result = new Map();
|
||||||
cloned.set(value, result); // Adding to cache now for cyclic references.
|
cloned.set(value, result); // Adding to cache now for cyclic references.
|
||||||
for (const [key, val] of value) {
|
for (const [key, val] of value) {
|
||||||
result.set(key, cloneValue(val));
|
result.set(key, fallbackCloneValue(val));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -1950,7 +1958,7 @@ class LoopbackPort {
|
|||||||
result = new Set();
|
result = new Set();
|
||||||
cloned.set(value, result); // Adding to cache now for cyclic references.
|
cloned.set(value, result); // Adding to cache now for cyclic references.
|
||||||
for (const val of value) {
|
for (const val of value) {
|
||||||
result.add(cloneValue(val));
|
result.add(fallbackCloneValue(val));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -1970,12 +1978,15 @@ class LoopbackPort {
|
|||||||
if (typeof desc.value === "function" && !value.hasOwnProperty?.(i)) {
|
if (typeof desc.value === "function" && !value.hasOwnProperty?.(i)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
result[i] = cloneValue(desc.value);
|
result[i] = fallbackCloneValue(desc.value);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cloned = new WeakMap();
|
const cloned = new WeakMap();
|
||||||
|
return fallbackCloneValue(object);
|
||||||
|
}
|
||||||
|
|
||||||
const event = { data: cloneValue(obj) };
|
const event = { data: cloneValue(obj) };
|
||||||
|
|
||||||
this._deferred.then(() => {
|
this._deferred.then(() => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user