@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
38 lines (37 loc) • 1.19 kB
JavaScript
import { GLOBAL_OBJ } from "./worldwide.js";
// src/utils/weakRef.ts
/**
* Creates a weak reference to an object if WeakRef is available,
* otherwise returns the object directly.
*
* This is useful for breaking circular references while maintaining
* compatibility with environments that don't support WeakRef (e.g., older browsers).
*
* @param value - The object to create a weak reference to
* @returns A WeakRef wrapper if available, or the original object as fallback
*/
function makeWeakRef(value) {
try {
const WeakRefImpl = GLOBAL_OBJ.WeakRef;
if (typeof WeakRefImpl === "function") return new WeakRefImpl(value);
} catch {}
return value;
}
/**
* Resolves a potentially weak reference, returning the underlying object
* or undefined if the reference has been garbage collected.
*
* @param ref - A MaybeWeakRef or undefined
* @returns The referenced object, or undefined if GC'd or ref was undefined
*/
function derefWeakRef(ref) {
if (!ref) return;
if (typeof ref === "object" && "deref" in ref && typeof ref.deref === "function") try {
return ref.deref();
} catch {
return;
}
return ref;
}
export { derefWeakRef, makeWeakRef };
//# sourceMappingURL=weakRef.js.map