@@ -7,7 +7,16 @@ const IGNORE_LIST = ['constructor', 'undefined', 'NaN', 'global', 'globalThis'];
7
7
*
8
8
*/
9
9
export default class GlobalRegistrator {
10
- private static registered : { [ key : string | symbol ] : PropertyDescriptor } | null = null ;
10
+ static #registered: { [ key : string | symbol ] : PropertyDescriptor } | null = null ;
11
+
12
+ /**
13
+ * Returns the registered state.
14
+ *
15
+ * @returns Registered state.
16
+ */
17
+ public static get isRegistered ( ) : boolean {
18
+ return this . #registered !== null ;
19
+ }
11
20
12
21
/**
13
22
* Registers Happy DOM globally.
@@ -24,13 +33,13 @@ export default class GlobalRegistrator {
24
33
url ?: string ;
25
34
settings ?: IOptionalBrowserSettings ;
26
35
} ) : void {
27
- if ( this . registered !== null ) {
36
+ if ( this . # registered !== null ) {
28
37
throw new Error ( 'Failed to register. Happy DOM has already been globally registered.' ) ;
29
38
}
30
39
31
40
const window = new GlobalWindow ( { ...options , console : globalThis . console } ) ;
32
41
33
- this . registered = { } ;
42
+ this . # registered = { } ;
34
43
35
44
// Define properties on the global object
36
45
const propertyDescriptors = Object . getOwnPropertyDescriptors ( window ) ;
@@ -44,7 +53,7 @@ export default class GlobalRegistrator {
44
53
globalPropertyDescriptor ?. value === undefined ||
45
54
globalPropertyDescriptor ?. value !== windowPropertyDescriptor . value
46
55
) {
47
- this . registered [ key ] = globalPropertyDescriptor || null ;
56
+ this . # registered[ key ] = globalPropertyDescriptor || null ;
48
57
49
58
// If the property is the window object, replace it with the global object
50
59
if ( windowPropertyDescriptor . value === window ) {
@@ -65,7 +74,7 @@ export default class GlobalRegistrator {
65
74
66
75
for ( const key of propertySymbols ) {
67
76
const propertyDescriptor = Object . getOwnPropertyDescriptor ( window , key ) ;
68
- this . registered [ key ] = null ;
77
+ this . # registered[ key ] = null ;
69
78
70
79
// If the property is the window object, replace it with the global object
71
80
if ( propertyDescriptor . value === window ) {
@@ -87,23 +96,23 @@ export default class GlobalRegistrator {
87
96
* Closes the window and unregisters Happy DOM from being global.
88
97
*/
89
98
public static async unregister ( ) : Promise < void > {
90
- if ( this . registered === null ) {
99
+ if ( this . # registered === null ) {
91
100
throw new Error (
92
101
'Failed to unregister. Happy DOM has not previously been globally registered.'
93
102
) ;
94
103
}
95
104
96
105
const happyDOM = globalThis . happyDOM ;
97
106
98
- for ( const key of Object . keys ( this . registered ) ) {
99
- if ( this . registered [ key ] !== null ) {
100
- Object . defineProperty ( globalThis , key , this . registered [ key ] ) ;
107
+ for ( const key of Object . keys ( this . # registered) ) {
108
+ if ( this . # registered[ key ] !== null ) {
109
+ Object . defineProperty ( globalThis , key , this . # registered[ key ] ) ;
101
110
} else {
102
111
delete globalThis [ key ] ;
103
112
}
104
113
}
105
114
106
- this . registered = null ;
115
+ this . # registered = null ;
107
116
108
117
if ( happyDOM ) {
109
118
await happyDOM . close ( ) ;
0 commit comments