keyborg 1.0.0-alpha.0 → 1.1.0-alpha.1

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/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Keyborg ⌨️🤖
2
+
3
+ Keyborg is a library that tracks the state of current keyboard input on a web page through focus events.
4
+
5
+ **It does not do anything invasive to the DOM** but provides an event subscription system that allows users to choose how they want to react to changes in focus.
6
+
7
+ ## Getting started
8
+
9
+ ### Installation
10
+
11
+ ```bash
12
+ # NPM
13
+ npm install --save keyborg
14
+ # Yarn
15
+ yarn add keyborg
16
+ ```
17
+
18
+ ### Usage
19
+
20
+ ```js
21
+ import { createKeyborg } from "keyborg";
22
+
23
+ // initializes keyborg on the current window
24
+ const keyborg = createKeyborg(window);
25
+
26
+ // This is called every time the keyboard input state changes
27
+ const handler = (isUsingKeyboard) => {
28
+ if (isUsingKeyboard) {
29
+ document.body.setAttribute("data-is-keyboard", "true");
30
+ } else {
31
+ document.body.removeAttribute("data-is-keyboard");
32
+ }
33
+ };
34
+
35
+ keyborg.subscribe(handler);
36
+ keyborg.unsubscribe(handler);
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ Pretty simple currently, you only need to know about theese commands
42
+
43
+ - `npm install` - install dependencies
44
+ - `npm run build` - builds the library
45
+ - `npm run format:fix` - runs prettier to format code
46
+ - `npm run lint:fix` - runs eslint and fixes issues
47
+
48
+ This project welcomes contributions and suggestions. Most contributions require you to agree to a
49
+ Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
50
+ the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
51
+
52
+ When you submit a pull request, a CLA bot will automatically determine whether you need to provide
53
+ a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
54
+ provided by the bot. You will only need to do this once across all repos using our CLA.
55
+
56
+ This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
57
+ For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
58
+ contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
59
+
60
+ ## Trademarks
61
+
62
+ This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
63
+ trademarks or logos is subject to and must follow
64
+ [Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
65
+ Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
66
+ Any use of third-party trademarks or logos are subject to those third-party's policies.
@@ -0,0 +1,26 @@
1
+ export declare const KEYBORG_FOCUSIN = "keyborg:focusin";
2
+ export interface KeyborgFocusInEventDetails {
3
+ relatedTarget?: HTMLElement;
4
+ isFocusedProgrammatically?: boolean;
5
+ }
6
+ export interface KeyborgFocusInEvent extends Event {
7
+ details: KeyborgFocusInEventDetails;
8
+ }
9
+ /**
10
+ * Guarantees that the native `focus` will be used
11
+ */
12
+ export declare function nativeFocus(element: HTMLElement): void;
13
+ /**
14
+ * Overrides the native `focus` and setups the keyborg focus event
15
+ */
16
+ export declare function setupFocusEvent(win: Window): void;
17
+ /**
18
+ * Removes keyborg event listeners and custom focus override
19
+ * @param win The window that stores keyborg focus events
20
+ */
21
+ export declare function disposeFocusEvent(win: Window): void;
22
+ /**
23
+ * @param win The window that stores keyborg focus events
24
+ * @returns The last element focused with element.focus()
25
+ */
26
+ export declare function getLastFocusedProgrammatically(win: Window): HTMLElement | null | undefined;
@@ -0,0 +1,130 @@
1
+ import { WeakRefInstance } from './WeakRefInstance.js';
2
+
3
+ /*!
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ * Licensed under the MIT License.
6
+ */
7
+ const KEYBORG_FOCUSIN = "keyborg:focusin";
8
+
9
+ function canOverrideNativeFocus(win) {
10
+ const HTMLElement = win.HTMLElement;
11
+ const origFocus = HTMLElement.prototype.focus;
12
+ let isCustomFocusCalled = false;
13
+
14
+ HTMLElement.prototype.focus = function focus() {
15
+ isCustomFocusCalled = true;
16
+ };
17
+
18
+ const btn = win.document.createElement("button");
19
+ btn.focus();
20
+ HTMLElement.prototype.focus = origFocus;
21
+ return isCustomFocusCalled;
22
+ }
23
+
24
+ let _canOverrideNativeFocus = false;
25
+ /**
26
+ * Guarantees that the native `focus` will be used
27
+ */
28
+
29
+ function nativeFocus(element) {
30
+ const focus = element.focus;
31
+
32
+ if (focus.__keyborgNativeFocus) {
33
+ focus.__keyborgNativeFocus.call(element);
34
+ } else {
35
+ element.focus();
36
+ }
37
+ }
38
+ /**
39
+ * Overrides the native `focus` and setups the keyborg focus event
40
+ */
41
+
42
+ function setupFocusEvent(win) {
43
+ const kwin = win;
44
+
45
+ if (!_canOverrideNativeFocus) {
46
+ _canOverrideNativeFocus = canOverrideNativeFocus(kwin);
47
+ }
48
+
49
+ const origFocus = kwin.HTMLElement.prototype.focus;
50
+
51
+ if (origFocus.__keyborgNativeFocus) {
52
+ // Already set up.
53
+ return;
54
+ }
55
+
56
+ kwin.HTMLElement.prototype.focus = focus;
57
+ const data = kwin.__keyborgData = {
58
+ focusInHandler: e => {
59
+ var _a;
60
+
61
+ const target = e.target;
62
+
63
+ if (!target) {
64
+ return;
65
+ }
66
+
67
+ const event = document.createEvent("HTMLEvents");
68
+ event.initEvent(KEYBORG_FOCUSIN, true, true);
69
+ const details = {
70
+ relatedTarget: e.relatedTarget || undefined
71
+ };
72
+
73
+ if (_canOverrideNativeFocus || data.lastFocusedProgrammatically) {
74
+ details.isFocusedProgrammatically = target === ((_a = data.lastFocusedProgrammatically) === null || _a === void 0 ? void 0 : _a.deref());
75
+ data.lastFocusedProgrammatically = undefined;
76
+ }
77
+
78
+ event.details = details;
79
+ target.dispatchEvent(event);
80
+ }
81
+ };
82
+ kwin.document.addEventListener("focusin", kwin.__keyborgData.focusInHandler, true);
83
+
84
+ function focus() {
85
+ const keyborgNativeFocusEvent = kwin.__keyborgData;
86
+
87
+ if (keyborgNativeFocusEvent) {
88
+ keyborgNativeFocusEvent.lastFocusedProgrammatically = new WeakRefInstance(this);
89
+ } // eslint-disable-next-line prefer-rest-params
90
+
91
+
92
+ return origFocus.apply(this, arguments);
93
+ }
94
+
95
+ focus.__keyborgNativeFocus = origFocus;
96
+ }
97
+ /**
98
+ * Removes keyborg event listeners and custom focus override
99
+ * @param win The window that stores keyborg focus events
100
+ */
101
+
102
+ function disposeFocusEvent(win) {
103
+ const kwin = win;
104
+ const proto = kwin.HTMLElement.prototype;
105
+ const origFocus = proto.focus.__keyborgNativeFocus;
106
+ const keyborgNativeFocusEvent = kwin.__keyborgData;
107
+
108
+ if (keyborgNativeFocusEvent) {
109
+ kwin.document.removeEventListener("focusin", keyborgNativeFocusEvent.focusInHandler, true);
110
+ delete kwin.__keyborgData;
111
+ }
112
+
113
+ if (origFocus) {
114
+ proto.focus = origFocus;
115
+ }
116
+ }
117
+ /**
118
+ * @param win The window that stores keyborg focus events
119
+ * @returns The last element focused with element.focus()
120
+ */
121
+
122
+ function getLastFocusedProgrammatically(win) {
123
+ var _a;
124
+
125
+ const keyborgNativeFocusEvent = win.__keyborgData;
126
+ return keyborgNativeFocusEvent ? ((_a = keyborgNativeFocusEvent.lastFocusedProgrammatically) === null || _a === void 0 ? void 0 : _a.deref()) || null : undefined;
127
+ }
128
+
129
+ export { KEYBORG_FOCUSIN, disposeFocusEvent, getLastFocusedProgrammatically, nativeFocus, setupFocusEvent };
130
+ //# sourceMappingURL=FocusEvent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FocusEvent.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,81 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { Disposable } from "./WeakRefInstance";
6
+ interface WindowWithKeyborg extends Window {
7
+ __keyborg?: {
8
+ core: KeyborgCore;
9
+ refs: {
10
+ [id: string]: Keyborg;
11
+ };
12
+ };
13
+ }
14
+ export declare type KeyborgCallback = (isNavigatingWithKeyboard: boolean) => void;
15
+ /**
16
+ * Source of truth for all the keyborg core instances and the current keyboard navigation state
17
+ */
18
+ export declare class KeyborgState {
19
+ private __keyborgCoreRefs;
20
+ private _isNavigatingWithKeyboard;
21
+ add(keyborg: KeyborgCore): void;
22
+ remove(id: string): void;
23
+ setVal(isNavigatingWithKeyboard: boolean): void;
24
+ getVal(): boolean;
25
+ }
26
+ /**
27
+ * Manages a collection of Keyborg instances in a window/document and updates keyborg state
28
+ */
29
+ declare class KeyborgCore implements Disposable {
30
+ readonly id: string;
31
+ private _win?;
32
+ private _isMouseUsed;
33
+ private _dismissTimer;
34
+ constructor(win: WindowWithKeyborg);
35
+ dispose(): void;
36
+ isDisposed(): boolean;
37
+ /**
38
+ * Updates all keyborg instances with the keyboard navigation state
39
+ */
40
+ update(isNavigatingWithKeyboard: boolean): void;
41
+ private _onFocusIn;
42
+ private _onMouseDown;
43
+ private _onKeyDown;
44
+ private _scheduleDismiss;
45
+ }
46
+ /**
47
+ * Used to determine the keyboard navigation state
48
+ */
49
+ export declare class Keyborg {
50
+ private _id;
51
+ private _win?;
52
+ private _core?;
53
+ private _cb;
54
+ static create(win: WindowWithKeyborg): Keyborg;
55
+ static dispose(instance: Keyborg): void;
56
+ /**
57
+ * Updates all subscribed callbacks with the keyboard navigation state
58
+ */
59
+ static update(instance: Keyborg, isNavigatingWithKeyboard: boolean): void;
60
+ private constructor();
61
+ private dispose;
62
+ /**
63
+ * @returns Whether the user is navigating with keyboard
64
+ */
65
+ isNavigatingWithKeyboard(): boolean;
66
+ /**
67
+ * @param callback - Called when the keyboard navigation state changes
68
+ */
69
+ subscribe(callback: KeyborgCallback): void;
70
+ /**
71
+ * @param callback - Registered with subscribe
72
+ */
73
+ unsubscribe(callback: KeyborgCallback): void;
74
+ /**
75
+ * Manually set the keyboard navigtion state
76
+ */
77
+ setVal(isNavigatingWithKeyboard: boolean): void;
78
+ }
79
+ export declare function createKeyborg(win: Window): Keyborg;
80
+ export declare function disposeKeyborg(instance: Keyborg): void;
81
+ export {};
package/lib/Keyborg.js ADDED
@@ -0,0 +1,313 @@
1
+ import { KEYBORG_FOCUSIN, setupFocusEvent, disposeFocusEvent } from './FocusEvent.js';
2
+ import { WeakRefInstance } from './WeakRefInstance.js';
3
+
4
+ /*!
5
+ * Copyright (c) Microsoft Corporation. All rights reserved.
6
+ * Licensed under the MIT License.
7
+ */
8
+ const KeyTab = 9;
9
+ const KeyEsc = 27;
10
+ const _dismissTimeout = 500; // When Esc is pressed and the focused is not moved
11
+ // during _dismissTimeout time, dismiss the keyboard
12
+ // navigation mode.
13
+
14
+ let _lastId = 0;
15
+ /**
16
+ * Source of truth for all the keyborg core instances and the current keyboard navigation state
17
+ */
18
+
19
+ class KeyborgState {
20
+ constructor() {
21
+ this.__keyborgCoreRefs = {};
22
+ this._isNavigatingWithKeyboard = false;
23
+ }
24
+
25
+ add(keyborg) {
26
+ const id = keyborg.id;
27
+
28
+ if (!(id in this.__keyborgCoreRefs)) {
29
+ this.__keyborgCoreRefs[id] = new WeakRefInstance(keyborg);
30
+ }
31
+ }
32
+
33
+ remove(id) {
34
+ delete this.__keyborgCoreRefs[id];
35
+
36
+ if (Object.keys(this.__keyborgCoreRefs).length === 0) {
37
+ this._isNavigatingWithKeyboard = false;
38
+ }
39
+ }
40
+
41
+ setVal(isNavigatingWithKeyboard) {
42
+ if (this._isNavigatingWithKeyboard === isNavigatingWithKeyboard) {
43
+ return;
44
+ }
45
+
46
+ this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;
47
+
48
+ for (const id of Object.keys(this.__keyborgCoreRefs)) {
49
+ const ref = this.__keyborgCoreRefs[id];
50
+ const keyborg = ref.deref();
51
+
52
+ if (keyborg) {
53
+ keyborg.update(isNavigatingWithKeyboard);
54
+ } else {
55
+ this.remove(id);
56
+ }
57
+ }
58
+ }
59
+
60
+ getVal() {
61
+ return this._isNavigatingWithKeyboard;
62
+ }
63
+
64
+ }
65
+
66
+ const _state = /*#__PURE__*/new KeyborgState();
67
+ /**
68
+ * Manages a collection of Keyborg instances in a window/document and updates keyborg state
69
+ */
70
+
71
+
72
+ class KeyborgCore {
73
+ constructor(win) {
74
+ this._isMouseUsed = false;
75
+
76
+ this._onFocusIn = e => {
77
+ if (this._isMouseUsed) {
78
+ this._isMouseUsed = false;
79
+ return;
80
+ }
81
+
82
+ if (_state.getVal()) {
83
+ return;
84
+ }
85
+
86
+ const details = e.details;
87
+
88
+ if (!details.relatedTarget) {
89
+ return;
90
+ }
91
+
92
+ if (details.isFocusedProgrammatically || details.isFocusedProgrammatically === undefined) {
93
+ // The element is focused programmatically, or the programmatic focus detection
94
+ // is not working.
95
+ return;
96
+ }
97
+
98
+ _state.setVal(true);
99
+ };
100
+
101
+ this._onMouseDown = e => {
102
+ if (e.buttons === 0 || e.clientX === 0 && e.clientY === 0 && e.screenX === 0 && e.screenY === 0) {
103
+ // This is most likely an event triggered by the screen reader to perform
104
+ // an action on an element, do not dismiss the keyboard navigation mode.
105
+ return;
106
+ }
107
+
108
+ this._isMouseUsed = true;
109
+
110
+ _state.setVal(false);
111
+ };
112
+
113
+ this._onKeyDown = e => {
114
+ const isNavigatingWithKeyboard = _state.getVal();
115
+
116
+ if (!isNavigatingWithKeyboard && e.keyCode === KeyTab) {
117
+ _state.setVal(true);
118
+ } else if (isNavigatingWithKeyboard && e.keyCode === KeyEsc) {
119
+ this._scheduleDismiss();
120
+ }
121
+ };
122
+
123
+ this.id = "c" + ++_lastId;
124
+ this._win = win;
125
+ const doc = win.document;
126
+ doc.addEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!
127
+
128
+ doc.addEventListener("mousedown", this._onMouseDown, true); // Capture!
129
+
130
+ win.addEventListener("keydown", this._onKeyDown, true); // Capture!
131
+
132
+ setupFocusEvent(win);
133
+
134
+ _state.add(this);
135
+ }
136
+
137
+ dispose() {
138
+ const win = this._win;
139
+
140
+ if (win) {
141
+ if (this._dismissTimer) {
142
+ win.clearTimeout(this._dismissTimer);
143
+ this._dismissTimer = undefined;
144
+ }
145
+
146
+ disposeFocusEvent(win);
147
+ const doc = win.document;
148
+ doc.removeEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!
149
+
150
+ doc.removeEventListener("mousedown", this._onMouseDown, true); // Capture!
151
+
152
+ win.removeEventListener("keydown", this._onKeyDown, true); // Capture!
153
+
154
+ delete this._win;
155
+
156
+ _state.remove(this.id);
157
+ }
158
+ }
159
+
160
+ isDisposed() {
161
+ return !!this._win;
162
+ }
163
+ /**
164
+ * Updates all keyborg instances with the keyboard navigation state
165
+ */
166
+
167
+
168
+ update(isNavigatingWithKeyboard) {
169
+ var _a, _b;
170
+
171
+ const keyborgs = (_b = (_a = this._win) === null || _a === void 0 ? void 0 : _a.__keyborg) === null || _b === void 0 ? void 0 : _b.refs;
172
+
173
+ if (keyborgs) {
174
+ for (const id of Object.keys(keyborgs)) {
175
+ Keyborg.update(keyborgs[id], isNavigatingWithKeyboard);
176
+ }
177
+ }
178
+ }
179
+
180
+ _scheduleDismiss() {
181
+ const win = this._win;
182
+
183
+ if (win) {
184
+ if (this._dismissTimer) {
185
+ win.clearTimeout(this._dismissTimer);
186
+ this._dismissTimer = undefined;
187
+ }
188
+
189
+ const was = win.document.activeElement;
190
+ this._dismissTimer = win.setTimeout(() => {
191
+ this._dismissTimer = undefined;
192
+ const cur = win.document.activeElement;
193
+
194
+ if (was && cur && was === cur) {
195
+ // Esc was pressed, currently focused element hasn't changed.
196
+ // Just dismiss the keyboard navigation mode.
197
+ _state.setVal(false);
198
+ }
199
+ }, _dismissTimeout);
200
+ }
201
+ }
202
+
203
+ }
204
+ /**
205
+ * Used to determine the keyboard navigation state
206
+ */
207
+
208
+
209
+ class Keyborg {
210
+ constructor(win) {
211
+ this._cb = [];
212
+ this._id = "k" + ++_lastId;
213
+ this._win = win;
214
+ const current = win.__keyborg;
215
+
216
+ if (current) {
217
+ this._core = current.core;
218
+ current.refs[this._id] = this;
219
+ } else {
220
+ this._core = new KeyborgCore(win);
221
+ win.__keyborg = {
222
+ core: this._core,
223
+ refs: {
224
+ [this._id]: this
225
+ }
226
+ };
227
+ }
228
+ }
229
+
230
+ static create(win) {
231
+ return new Keyborg(win);
232
+ }
233
+
234
+ static dispose(instance) {
235
+ instance.dispose();
236
+ }
237
+ /**
238
+ * Updates all subscribed callbacks with the keyboard navigation state
239
+ */
240
+
241
+
242
+ static update(instance, isNavigatingWithKeyboard) {
243
+ instance._cb.forEach(callback => callback(isNavigatingWithKeyboard));
244
+ }
245
+
246
+ dispose() {
247
+ var _a;
248
+
249
+ const current = (_a = this._win) === null || _a === void 0 ? void 0 : _a.__keyborg;
250
+
251
+ if (current === null || current === void 0 ? void 0 : current.refs[this._id]) {
252
+ delete current.refs[this._id];
253
+
254
+ if (Object.keys(current.refs).length === 0) {
255
+ current.core.dispose(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
256
+
257
+ delete this._win.__keyborg;
258
+ }
259
+ } else if (process.env.NODE_ENV === 'development') {
260
+ console.error("Keyborg instance " + this._id + " is being disposed incorrectly.");
261
+ }
262
+
263
+ this._cb = [];
264
+ delete this._core;
265
+ delete this._win;
266
+ }
267
+ /**
268
+ * @returns Whether the user is navigating with keyboard
269
+ */
270
+
271
+
272
+ isNavigatingWithKeyboard() {
273
+ return _state.getVal();
274
+ }
275
+ /**
276
+ * @param callback - Called when the keyboard navigation state changes
277
+ */
278
+
279
+
280
+ subscribe(callback) {
281
+ this._cb.push(callback);
282
+ }
283
+ /**
284
+ * @param callback - Registered with subscribe
285
+ */
286
+
287
+
288
+ unsubscribe(callback) {
289
+ const index = this._cb.indexOf(callback);
290
+
291
+ if (index >= 0) {
292
+ this._cb.splice(index, 1);
293
+ }
294
+ }
295
+ /**
296
+ * Manually set the keyboard navigtion state
297
+ */
298
+
299
+
300
+ setVal(isNavigatingWithKeyboard) {
301
+ _state.setVal(isNavigatingWithKeyboard);
302
+ }
303
+
304
+ }
305
+ function createKeyborg(win) {
306
+ return Keyborg.create(win);
307
+ }
308
+ function disposeKeyborg(instance) {
309
+ Keyborg.dispose(instance);
310
+ }
311
+
312
+ export { Keyborg, KeyborgState, createKeyborg, disposeKeyborg };
313
+ //# sourceMappingURL=Keyborg.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Keyborg.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,25 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ export declare const _canUseWeakRef: boolean;
6
+ /**
7
+ * Allows disposable instances to be used
8
+ */
9
+ export interface Disposable {
10
+ isDisposed?(): boolean;
11
+ }
12
+ /**
13
+ * WeakRef wrapper around a HTMLElement that also supports IE11
14
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef}
15
+ * @internal
16
+ */
17
+ export declare class WeakRefInstance<T extends Disposable | object> {
18
+ private _weakRef?;
19
+ private _instance?;
20
+ constructor(instance: T);
21
+ /**
22
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef/deref}
23
+ */
24
+ deref(): T | undefined;
25
+ }
@@ -0,0 +1,51 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ // IE11 compat, checks if WeakRef is supported
6
+ const _canUseWeakRef = typeof WeakRef !== "undefined";
7
+ /**
8
+ * WeakRef wrapper around a HTMLElement that also supports IE11
9
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef}
10
+ * @internal
11
+ */
12
+
13
+ class WeakRefInstance {
14
+ constructor(instance) {
15
+ if (_canUseWeakRef && typeof instance === "object") {
16
+ this._weakRef = new WeakRef(instance);
17
+ } else {
18
+ this._instance = instance;
19
+ }
20
+ }
21
+ /**
22
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef/deref}
23
+ */
24
+
25
+
26
+ deref() {
27
+ var _a, _b, _c;
28
+
29
+ let instance;
30
+
31
+ if (this._weakRef) {
32
+ instance = (_a = this._weakRef) === null || _a === void 0 ? void 0 : _a.deref();
33
+
34
+ if (!instance) {
35
+ delete this._weakRef;
36
+ }
37
+ } else {
38
+ instance = this._instance;
39
+
40
+ if ((_c = (_b = instance) === null || _b === void 0 ? void 0 : _b.isDisposed) === null || _c === void 0 ? void 0 : _c.call(_b)) {
41
+ delete this._instance;
42
+ }
43
+ }
44
+
45
+ return instance;
46
+ }
47
+
48
+ }
49
+
50
+ export { WeakRefInstance, _canUseWeakRef };
51
+ //# sourceMappingURL=WeakRefInstance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WeakRefInstance.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}