Skip to content

Commit

Permalink
feat: @W-11465479 minor reduction in document and window getter calls (
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton authored Jul 21, 2022
1 parent b9fcf3c commit 8b6b160
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
27 changes: 13 additions & 14 deletions packages/near-membrane-dom/src/browser-realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,20 @@ const HTMLIFrameElementProtoContentWindowGetter: Getter = ReflectApply(
const NodeProtoLastChildGetter: Getter = ReflectApply(ObjectProtoLookupGetter, NodeProto, [
'lastChild',
])!;
const docRef = document;

const documentToBlueCreateHooksCallbackMap = toSafeWeakMap(
new WeakMapCtor<typeof document, Connector>()
const blueDocumentToBlueCreateHooksCallbackMap = toSafeWeakMap(
new WeakMapCtor<Document, Connector>()
);

let defaultGlobalOwnKeys: PropertyKeys | null = null;

function createDetachableIframe(): HTMLIFrameElement {
const iframe: HTMLIFrameElement = ReflectApply(DocumentProtoCreateElement, docRef, ['iframe']);
function createDetachableIframe(doc: Document): HTMLIFrameElement {
const iframe: HTMLIFrameElement = ReflectApply(DocumentProtoCreateElement, doc, ['iframe']);
// It is impossible to test whether the NodeProtoLastChildGetter branch is
// reached in a normal Karma test environment.
const parent: Element =
ReflectApply(DocumentProtoBodyGetter, docRef, []) ??
/* istanbul ignore next */ ReflectApply(NodeProtoLastChildGetter, docRef, []);
ReflectApply(DocumentProtoBodyGetter, doc, []) ??
/* istanbul ignore next */ ReflectApply(NodeProtoLastChildGetter, doc, []);
const style: CSSStyleDeclaration = ReflectApply(HTMLElementProtoStyleGetter, iframe, []);
style.display = 'none';
ReflectApply(ElementProtoSetAttribute, iframe, ['sandbox', IFRAME_SANDBOX_ATTRIBUTE_VALUE]);
Expand All @@ -91,6 +90,10 @@ function createIframeVirtualEnvironment(
if (typeof globalObject !== 'object' || globalObject === null) {
throw new TypeErrorCtor('Missing global object virtualization target.');
}
const blueRefs = getCachedGlobalObjectReferences(globalObject);
if (typeof blueRefs !== 'object' || blueRefs === null) {
throw new TypeErrorCtor('Invalid virtualization target.');
}
const {
distortionCallback,
endowments,
Expand All @@ -99,7 +102,7 @@ function createIframeVirtualEnvironment(
keepAlive = false,
// eslint-disable-next-line prefer-object-spread
} = ObjectAssign({ __proto__: null }, options);
const iframe = createDetachableIframe();
const iframe = createDetachableIframe(blueRefs.document);
const redWindow: Window & typeof globalThis = ReflectApply(
HTMLIFrameElementProtoContentWindowGetter,
iframe,
Expand All @@ -110,16 +113,12 @@ function createIframeVirtualEnvironment(
if (shouldUseDefaultGlobalOwnKeys && defaultGlobalOwnKeys === null) {
defaultGlobalOwnKeys = filterWindowKeys(getFilteredGlobalOwnKeys(redWindow));
}
const blueRefs = getCachedGlobalObjectReferences(globalObject);
if (typeof blueRefs !== 'object' || blueRefs === null) {
throw new TypeErrorCtor('Invalid virtualization target.');
}
let blueConnector = documentToBlueCreateHooksCallbackMap.get(blueRefs.document) as
let blueConnector = blueDocumentToBlueCreateHooksCallbackMap.get(blueRefs.document) as
| Connector
| undefined;
if (blueConnector === undefined) {
blueConnector = createBlueConnector(globalObject);
documentToBlueCreateHooksCallbackMap.set(blueRefs.document, blueConnector);
blueDocumentToBlueCreateHooksCallbackMap.set(blueRefs.document, blueConnector);
}
const env = new VirtualEnvironment({
blueConnector,
Expand Down
2 changes: 1 addition & 1 deletion packages/near-membrane-dom/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface CachedBlueReferencesRecord extends Object {
EventTargetProtoOwnKeys: PropertyKeys;
}

const blueDocumentToRecordMap: WeakMap<typeof document, CachedBlueReferencesRecord> = toSafeWeakMap(
const blueDocumentToRecordMap: WeakMap<Document, CachedBlueReferencesRecord> = toSafeWeakMap(
new WeakMap()
);

Expand Down

0 comments on commit 8b6b160

Please sign in to comment.