@amplitude/session-replay-browser 1.52.0-codexsemantic-review-integration.0 → 1.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/config/joined-config.d.ts +1 -0
- package/lib/cjs/config/joined-config.d.ts.map +1 -1
- package/lib/cjs/config/joined-config.js +10 -1
- package/lib/cjs/config/joined-config.js.map +1 -1
- package/lib/cjs/config/local-config.d.ts +3 -3
- package/lib/cjs/config/local-config.d.ts.map +1 -1
- package/lib/cjs/config/local-config.js +5 -3
- package/lib/cjs/config/local-config.js.map +1 -1
- package/lib/cjs/config/types.d.ts +17 -9
- package/lib/cjs/config/types.d.ts.map +1 -1
- package/lib/cjs/config/types.js.map +1 -1
- package/lib/cjs/helpers.d.ts.map +1 -1
- package/lib/cjs/helpers.js +7 -4
- package/lib/cjs/helpers.js.map +1 -1
- package/lib/cjs/hooks/click.d.ts +2 -8
- package/lib/cjs/hooks/click.d.ts.map +1 -1
- package/lib/cjs/hooks/click.js +3 -42
- package/lib/cjs/hooks/click.js.map +1 -1
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.d.ts.map +1 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/plugins/url-tracking-plugin.d.ts +0 -3
- package/lib/cjs/plugins/url-tracking-plugin.d.ts.map +1 -1
- package/lib/cjs/plugins/url-tracking-plugin.js +9 -16
- package/lib/cjs/plugins/url-tracking-plugin.js.map +1 -1
- package/lib/cjs/semantic-identity.d.ts +1 -1
- package/lib/cjs/semantic-identity.d.ts.map +1 -1
- package/lib/cjs/semantic-identity.js +4 -3
- package/lib/cjs/semantic-identity.js.map +1 -1
- package/lib/cjs/semantic-types.d.ts +45 -0
- package/lib/cjs/semantic-types.d.ts.map +1 -0
- package/lib/cjs/semantic-types.js +3 -0
- package/lib/cjs/semantic-types.js.map +1 -0
- package/lib/cjs/semantic.d.ts +9 -12
- package/lib/cjs/semantic.d.ts.map +1 -1
- package/lib/cjs/semantic.js +17 -90
- package/lib/cjs/semantic.js.map +1 -1
- package/lib/cjs/session-replay.d.ts +39 -2
- package/lib/cjs/session-replay.d.ts.map +1 -1
- package/lib/cjs/session-replay.js +267 -132
- package/lib/cjs/session-replay.js.map +1 -1
- package/lib/cjs/utils/rrweb.d.ts +0 -3
- package/lib/cjs/utils/rrweb.d.ts.map +1 -1
- package/lib/cjs/utils/rrweb.js.map +1 -1
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.d.ts.map +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/cjs/version.js.map +1 -1
- package/lib/cjs/window-focus.d.ts +40 -0
- package/lib/cjs/window-focus.d.ts.map +1 -0
- package/lib/cjs/window-focus.js +182 -0
- package/lib/cjs/window-focus.js.map +1 -0
- package/lib/esm/config/joined-config.d.ts +1 -0
- package/lib/esm/config/joined-config.d.ts.map +1 -1
- package/lib/esm/config/joined-config.js +10 -1
- package/lib/esm/config/joined-config.js.map +1 -1
- package/lib/esm/config/local-config.d.ts +3 -3
- package/lib/esm/config/local-config.d.ts.map +1 -1
- package/lib/esm/config/local-config.js +5 -3
- package/lib/esm/config/local-config.js.map +1 -1
- package/lib/esm/config/types.d.ts +17 -9
- package/lib/esm/config/types.d.ts.map +1 -1
- package/lib/esm/config/types.js.map +1 -1
- package/lib/esm/helpers.d.ts.map +1 -1
- package/lib/esm/helpers.js +8 -5
- package/lib/esm/helpers.js.map +1 -1
- package/lib/esm/hooks/click.d.ts +2 -8
- package/lib/esm/hooks/click.d.ts.map +1 -1
- package/lib/esm/hooks/click.js +3 -42
- package/lib/esm/hooks/click.js.map +1 -1
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.d.ts.map +1 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/plugins/url-tracking-plugin.d.ts +0 -3
- package/lib/esm/plugins/url-tracking-plugin.d.ts.map +1 -1
- package/lib/esm/plugins/url-tracking-plugin.js +9 -16
- package/lib/esm/plugins/url-tracking-plugin.js.map +1 -1
- package/lib/esm/semantic-identity.d.ts +1 -1
- package/lib/esm/semantic-identity.d.ts.map +1 -1
- package/lib/esm/semantic-identity.js +4 -3
- package/lib/esm/semantic-identity.js.map +1 -1
- package/lib/esm/semantic-types.d.ts +45 -0
- package/lib/esm/semantic-types.d.ts.map +1 -0
- package/lib/esm/semantic-types.js +2 -0
- package/lib/esm/semantic-types.js.map +1 -0
- package/lib/esm/semantic.d.ts +9 -12
- package/lib/esm/semantic.d.ts.map +1 -1
- package/lib/esm/semantic.js +17 -90
- package/lib/esm/semantic.js.map +1 -1
- package/lib/esm/session-replay.d.ts +39 -2
- package/lib/esm/session-replay.d.ts.map +1 -1
- package/lib/esm/session-replay.js +267 -132
- package/lib/esm/session-replay.js.map +1 -1
- package/lib/esm/utils/rrweb.d.ts +0 -3
- package/lib/esm/utils/rrweb.d.ts.map +1 -1
- package/lib/esm/utils/rrweb.js.map +1 -1
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.d.ts.map +1 -1
- package/lib/esm/version.js +1 -1
- package/lib/esm/version.js.map +1 -1
- package/lib/esm/window-focus.d.ts +40 -0
- package/lib/esm/window-focus.d.ts.map +1 -0
- package/lib/esm/window-focus.js +179 -0
- package/lib/esm/window-focus.js.map +1 -0
- package/lib/scripts/index-min.js +1 -1
- package/lib/scripts/index-min.js.gz +0 -0
- package/lib/scripts/index-min.js.map +1 -1
- package/lib/scripts/session-replay-browser-min.js +1 -1
- package/lib/scripts/session-replay-browser-min.js.gz +0 -0
- package/lib/scripts/session-replay-browser-min.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { getGlobalScope } from '@amplitude/analytics-core';
|
|
2
|
+
/**
|
|
3
|
+
* Decides when focus arrived from outside this window.
|
|
4
|
+
*
|
|
5
|
+
* A blur whose next frame lands on an iframe stayed in this page. Anything else
|
|
6
|
+
* left the window. The parent is already blurred once an iframe is focused, so
|
|
7
|
+
* that iframe's window is watched too: moving into a nested frame is not a
|
|
8
|
+
* leave, and moving from the frame to another visible window is.
|
|
9
|
+
*/
|
|
10
|
+
var WindowFocusTracker = /** @class */ (function () {
|
|
11
|
+
function WindowFocusTracker() {
|
|
12
|
+
this.focusLeftWindow = false;
|
|
13
|
+
}
|
|
14
|
+
/** Call from the window `blur` listener. `activeElement` is not updated yet. */
|
|
15
|
+
WindowFocusTracker.prototype.noteBlur = function () {
|
|
16
|
+
var _this = this;
|
|
17
|
+
this.focusLeftWindow = false;
|
|
18
|
+
this.cancelFocusDestinationCheck();
|
|
19
|
+
var scope = getGlobalScope();
|
|
20
|
+
this.scheduleFocusInspect(scope, function () {
|
|
21
|
+
var _a;
|
|
22
|
+
var active = (_a = scope === null || scope === void 0 ? void 0 : scope.document) === null || _a === void 0 ? void 0 : _a.activeElement;
|
|
23
|
+
if ((active === null || active === void 0 ? void 0 : active.tagName) === 'IFRAME') {
|
|
24
|
+
_this.watchFocusedFrame(active);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
_this.releaseFocusedFrame();
|
|
28
|
+
_this.focusLeftWindow = true;
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
/** The document hid. Drop the iframe watcher; visibility owns the checkout. */
|
|
32
|
+
WindowFocusTracker.prototype.onDocumentHidden = function () {
|
|
33
|
+
this.releaseFocusedFrame();
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* The document was shown. Drop the external-focus flag so the focus event
|
|
37
|
+
* that follows a tab return does not take a second checkout.
|
|
38
|
+
*/
|
|
39
|
+
WindowFocusTracker.prototype.onDocumentVisible = function () {
|
|
40
|
+
this.focusLeftWindow = false;
|
|
41
|
+
this.cancelFocusDestinationCheck();
|
|
42
|
+
this.releaseFocusedFrame();
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Call from the window `focus` listener. True when focus arrived from outside
|
|
46
|
+
* this window while the document stayed visible.
|
|
47
|
+
*/
|
|
48
|
+
WindowFocusTracker.prototype.consumeArrivedFromOutside = function () {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
var arrivedFromOutside = this.focusLeftWindow && ((_b = (_a = getGlobalScope()) === null || _a === void 0 ? void 0 : _a.document) === null || _b === void 0 ? void 0 : _b.visibilityState) === 'visible';
|
|
51
|
+
this.focusLeftWindow = false;
|
|
52
|
+
this.releaseFocusedFrame();
|
|
53
|
+
return arrivedFromOutside;
|
|
54
|
+
};
|
|
55
|
+
WindowFocusTracker.prototype.stop = function () {
|
|
56
|
+
this.focusLeftWindow = false;
|
|
57
|
+
this.cancelFocusDestinationCheck();
|
|
58
|
+
this.releaseFocusedFrame();
|
|
59
|
+
};
|
|
60
|
+
WindowFocusTracker.prototype.noteFrameBlur = function () {
|
|
61
|
+
var _this = this;
|
|
62
|
+
this.cancelFocusDestinationCheck();
|
|
63
|
+
var scope = getGlobalScope();
|
|
64
|
+
this.scheduleFocusInspect(scope, function () {
|
|
65
|
+
var _a;
|
|
66
|
+
var active = (_a = scope === null || scope === void 0 ? void 0 : scope.document) === null || _a === void 0 ? void 0 : _a.activeElement;
|
|
67
|
+
var parentDocument = scope === null || scope === void 0 ? void 0 : scope.document;
|
|
68
|
+
var parentHasFocus = typeof (parentDocument === null || parentDocument === void 0 ? void 0 : parentDocument.hasFocus) === 'function' && parentDocument.hasFocus();
|
|
69
|
+
if (parentHasFocus || (active === null || active === void 0 ? void 0 : active.tagName) !== 'IFRAME') {
|
|
70
|
+
_this.releaseFocusedFrame();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
var holder = _this.focusHolder(active);
|
|
74
|
+
if (holder) {
|
|
75
|
+
_this.watchFocusedFrame(holder);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
_this.focusLeftWindow = true;
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* The iframe that still holds focus, walking into nested frames. Undefined means
|
|
83
|
+
* the frame's document is visible and neither it nor a child frame is focused.
|
|
84
|
+
*/
|
|
85
|
+
WindowFocusTracker.prototype.focusHolder = function (frame, seen) {
|
|
86
|
+
if (seen === null || seen === void 0 ? void 0 : seen.has(frame)) {
|
|
87
|
+
return frame;
|
|
88
|
+
}
|
|
89
|
+
var visited = seen !== null && seen !== void 0 ? seen : new Set();
|
|
90
|
+
visited.add(frame);
|
|
91
|
+
var doc;
|
|
92
|
+
try {
|
|
93
|
+
doc = frame.contentDocument;
|
|
94
|
+
}
|
|
95
|
+
catch (_a) {
|
|
96
|
+
// A cross-origin frame is still part of this page.
|
|
97
|
+
return frame;
|
|
98
|
+
}
|
|
99
|
+
if (doc == null) {
|
|
100
|
+
return frame;
|
|
101
|
+
}
|
|
102
|
+
if (typeof doc.hasFocus === 'function' && doc.hasFocus()) {
|
|
103
|
+
return frame;
|
|
104
|
+
}
|
|
105
|
+
var inner = doc.activeElement;
|
|
106
|
+
if ((inner === null || inner === void 0 ? void 0 : inner.tagName) === 'IFRAME') {
|
|
107
|
+
return this.focusHolder(inner, visited);
|
|
108
|
+
}
|
|
109
|
+
return undefined;
|
|
110
|
+
};
|
|
111
|
+
WindowFocusTracker.prototype.watchFocusedFrame = function (frame) {
|
|
112
|
+
var _this = this;
|
|
113
|
+
this.releaseFocusedFrame();
|
|
114
|
+
var child;
|
|
115
|
+
var onBlur = function () {
|
|
116
|
+
_this.noteFrameBlur();
|
|
117
|
+
};
|
|
118
|
+
try {
|
|
119
|
+
child = frame.contentWindow;
|
|
120
|
+
// Reading `addEventListener` on a cross-origin Window throws a SecurityError.
|
|
121
|
+
if (child == null || typeof child.addEventListener !== 'function') {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
child.addEventListener('blur', onBlur);
|
|
125
|
+
}
|
|
126
|
+
catch (_a) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
this.focusedFrameWindow = child;
|
|
130
|
+
this.focusedFrameBlur = onBlur;
|
|
131
|
+
};
|
|
132
|
+
WindowFocusTracker.prototype.releaseFocusedFrame = function () {
|
|
133
|
+
var child = this.focusedFrameWindow;
|
|
134
|
+
var onBlur = this.focusedFrameBlur;
|
|
135
|
+
this.focusedFrameWindow = undefined;
|
|
136
|
+
this.focusedFrameBlur = undefined;
|
|
137
|
+
if (child === undefined) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
child.removeEventListener('blur', onBlur);
|
|
142
|
+
}
|
|
143
|
+
catch (_a) {
|
|
144
|
+
// The frame can reject listener removal once it is cross-origin.
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
WindowFocusTracker.prototype.scheduleFocusInspect = function (scope, inspect) {
|
|
148
|
+
var _this = this;
|
|
149
|
+
var run = function () {
|
|
150
|
+
_this.focusDestinationFrame = undefined;
|
|
151
|
+
try {
|
|
152
|
+
inspect();
|
|
153
|
+
}
|
|
154
|
+
catch (_a) {
|
|
155
|
+
// This runs from a rAF callback, so a throw would surface as an uncaught
|
|
156
|
+
// error on the host page. Focus tracking is best effort: stop watching.
|
|
157
|
+
_this.releaseFocusedFrame();
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
if (typeof (scope === null || scope === void 0 ? void 0 : scope.requestAnimationFrame) === 'function') {
|
|
161
|
+
this.focusDestinationFrame = scope.requestAnimationFrame(run);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
run();
|
|
165
|
+
};
|
|
166
|
+
WindowFocusTracker.prototype.cancelFocusDestinationCheck = function () {
|
|
167
|
+
if (this.focusDestinationFrame === undefined) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
var scope = getGlobalScope();
|
|
171
|
+
if (typeof (scope === null || scope === void 0 ? void 0 : scope.cancelAnimationFrame) === 'function') {
|
|
172
|
+
scope.cancelAnimationFrame(this.focusDestinationFrame);
|
|
173
|
+
}
|
|
174
|
+
this.focusDestinationFrame = undefined;
|
|
175
|
+
};
|
|
176
|
+
return WindowFocusTracker;
|
|
177
|
+
}());
|
|
178
|
+
export { WindowFocusTracker };
|
|
179
|
+
//# sourceMappingURL=window-focus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"window-focus.js","sourceRoot":"","sources":["../../src/window-focus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D;;;;;;;GAOG;AACH;IAAA;QACU,oBAAe,GAAG,KAAK,CAAC;IAsKlC,CAAC;IAjKC,gFAAgF;IAChF,qCAAQ,GAAR;QAAA,iBAaC;QAZC,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;QAC7B,IAAI,CAAC,2BAA2B,EAAE,CAAC;QACnC,IAAM,KAAK,GAAG,cAAc,EAAE,CAAC;QAC/B,IAAI,CAAC,oBAAoB,CAAC,KAAK,EAAE;;YAC/B,IAAM,MAAM,GAAG,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,QAAQ,0CAAE,aAAa,CAAC;YAC9C,IAAI,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,MAAK,QAAQ,EAAE;gBAChC,KAAI,CAAC,iBAAiB,CAAC,MAA2B,CAAC,CAAC;gBACpD,OAAO;aACR;YACD,KAAI,CAAC,mBAAmB,EAAE,CAAC;YAC3B,KAAI,CAAC,eAAe,GAAG,IAAI,CAAC;QAC9B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,+EAA+E;IAC/E,6CAAgB,GAAhB;QACE,IAAI,CAAC,mBAAmB,EAAE,CAAC;IAC7B,CAAC;IAED;;;OAGG;IACH,8CAAiB,GAAjB;QACE,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;QAC7B,IAAI,CAAC,2BAA2B,EAAE,CAAC;QACnC,IAAI,CAAC,mBAAmB,EAAE,CAAC;IAC7B,CAAC;IAED;;;OAGG;IACH,sDAAyB,GAAzB;;QACE,IAAM,kBAAkB,GAAG,IAAI,CAAC,eAAe,IAAI,CAAA,MAAA,MAAA,cAAc,EAAE,0CAAE,QA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E;YACrD,KAAK,CAAC,oBAAoB,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;SACxD;QACD,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;IACzC,CAAC;IACH,yBAAC;AAAD,CAAC,AAvKD,IAuKC","sourcesContent":["import { getGlobalScope } from '@amplitude/analytics-core';\n\n/**\n * Decides when focus arrived from outside this window.\n *\n * A blur whose next frame lands on an iframe stayed in this page. Anything else\n * left the window. The parent is already blurred once an iframe is focused, so\n * that iframe's window is watched too: moving into a nested frame is not a\n * leave, and moving from the frame to another visible window is.\n */\nexport class WindowFocusTracker {\n private focusLeftWindow = false;\n private focusDestinationFrame: number | undefined;\n private focusedFrameWindow: Window | undefined;\n private focusedFrameBlur: (() => void) | undefined;\n\n /** Call from the window `blur` listener. `activeElement` is not updated yet. */\n noteBlur(): void {\n this.focusLeftWindow = false;\n this.cancelFocusDestinationCheck();\n const scope = getGlobalScope();\n this.scheduleFocusInspect(scope, () => {\n const active = scope?.document?.activeElement;\n if (active?.tagName === 'IFRAME') {\n this.watchFocusedFrame(active as HTMLIFrameElement);\n return;\n }\n this.releaseFocusedFrame();\n this.focusLeftWindow = true;\n });\n }\n\n /** The document hid. Drop the iframe watcher; visibility owns the checkout. */\n onDocumentHidden(): void {\n this.releaseFocusedFrame();\n }\n\n /**\n * The document was shown. Drop the external-focus flag so the focus event\n * that follows a tab return does not take a second checkout.\n */\n onDocumentVisible(): void {\n this.focusLeftWindow = false;\n this.cancelFocusDestinationCheck();\n this.releaseFocusedFrame();\n }\n\n /**\n * Call from the window `focus` listener. True when focus arrived from outside\n * this window while the document stayed visible.\n */\n consumeArrivedFromOutside(): boolean {\n const arrivedFromOutside = this.focusLeftWindow && getGlobalScope()?.document?.visibilityState === 'visible';\n this.focusLeftWindow = false;\n this.releaseFocusedFrame();\n return arrivedFromOutside;\n }\n\n stop(): void {\n this.focusLeftWindow = false;\n this.cancelFocusDestinationCheck();\n this.releaseFocusedFrame();\n }\n\n private noteFrameBlur() {\n this.cancelFocusDestinationCheck();\n const scope = getGlobalScope();\n this.scheduleFocusInspect(scope, () => {\n const active = scope?.document?.activeElement;\n const parentDocument = scope?.document;\n const parentHasFocus = typeof parentDocument?.hasFocus === 'function' && parentDocument.hasFocus();\n if (parentHasFocus || active?.tagName !== 'IFRAME') {\n this.releaseFocusedFrame();\n return;\n }\n const holder = this.focusHolder(active as HTMLIFrameElement);\n if (holder) {\n this.watchFocusedFrame(holder);\n return;\n }\n this.focusLeftWindow = true;\n });\n }\n\n /**\n * The iframe that still holds focus, walking into nested frames. Undefined means\n * the frame's document is visible and neither it nor a child frame is focused.\n */\n private focusHolder(frame: HTMLIFrameElement, seen?: Set<HTMLIFrameElement>): HTMLIFrameElement | undefined {\n if (seen?.has(frame)) {\n return frame;\n }\n const visited = seen ?? new Set<HTMLIFrameElement>();\n visited.add(frame);\n let doc: Document | null | undefined;\n try {\n doc = frame.contentDocument;\n } catch {\n // A cross-origin frame is still part of this page.\n return frame;\n }\n if (doc == null) {\n return frame;\n }\n if (typeof doc.hasFocus === 'function' && doc.hasFocus()) {\n return frame;\n }\n const inner = doc.activeElement;\n if (inner?.tagName === 'IFRAME') {\n return this.focusHolder(inner as HTMLIFrameElement, visited);\n }\n return undefined;\n }\n\n private watchFocusedFrame(frame: HTMLIFrameElement) {\n this.releaseFocusedFrame();\n let child: Window | null | undefined;\n const onBlur = () => {\n this.noteFrameBlur();\n };\n try {\n child = frame.contentWindow;\n // Reading `addEventListener` on a cross-origin Window throws a SecurityError.\n if (child == null || typeof child.addEventListener !== 'function') {\n return;\n }\n child.addEventListener('blur', onBlur);\n } catch {\n return;\n }\n this.focusedFrameWindow = child;\n this.focusedFrameBlur = onBlur;\n }\n\n private releaseFocusedFrame() {\n const child = this.focusedFrameWindow;\n const onBlur = this.focusedFrameBlur;\n this.focusedFrameWindow = undefined;\n this.focusedFrameBlur = undefined;\n if (child === undefined) {\n return;\n }\n try {\n child.removeEventListener('blur', onBlur as () => void);\n } catch {\n // The frame can reject listener removal once it is cross-origin.\n }\n }\n\n private scheduleFocusInspect(scope: ReturnType<typeof getGlobalScope>, inspect: () => void) {\n const run = () => {\n this.focusDestinationFrame = undefined;\n try {\n inspect();\n } catch {\n // This runs from a rAF callback, so a throw would surface as an uncaught\n // error on the host page. Focus tracking is best effort: stop watching.\n this.releaseFocusedFrame();\n }\n };\n if (typeof scope?.requestAnimationFrame === 'function') {\n this.focusDestinationFrame = scope.requestAnimationFrame(run);\n return;\n }\n run();\n }\n\n private cancelFocusDestinationCheck() {\n if (this.focusDestinationFrame === undefined) {\n return;\n }\n const scope = getGlobalScope();\n if (typeof scope?.cancelAnimationFrame === 'function') {\n scope.cancelAnimationFrame(this.focusDestinationFrame);\n }\n this.focusDestinationFrame = undefined;\n }\n}\n"]}
|