Răsfoiți Sursa

Use window.document for React-Native (#38340)

Brennan 4 ani în urmă
părinte
comite
465f9294d0

+ 1 - 1
src/SignalR/clients/ts/signalr/src/HttpConnection.ts

@@ -533,7 +533,7 @@ export class HttpConnection implements IConnection {
             return url;
         }
 
-        if (!Platform.isBrowser || !window.document) {
+        if (!Platform.isBrowser) {
             throw new Error(`Cannot resolve '${url}'.`);
         }
 

+ 3 - 7
src/SignalR/clients/ts/signalr/src/HubConnection.ts

@@ -180,10 +180,8 @@ export class HubConnection {
             await this._startInternal();
 
             if (Platform.isBrowser) {
-                if (document) {
-                    // Log when the browser freezes the tab so users know why their connection unexpectedly stopped working
-                    document.addEventListener("freeze", this._freezeEventListener);
-                }
+                // Log when the browser freezes the tab so users know why their connection unexpectedly stopped working
+                window.document.addEventListener("freeze", this._freezeEventListener);
             }
 
             this._connectionState = HubConnectionState.Connected;
@@ -734,9 +732,7 @@ export class HubConnection {
             this._connectionStarted = false;
 
             if (Platform.isBrowser) {
-                if (document) {
-                    document.removeEventListener("freeze", this._freezeEventListener);
-                }
+                window.document.removeEventListener("freeze", this._freezeEventListener);
             }
 
             try {

+ 11 - 2
src/SignalR/clients/ts/signalr/src/Utils.ts

@@ -35,16 +35,25 @@ export class Arg {
 
 /** @private */
 export class Platform {
+    // react-native has a window but no document so we should check both
     public static get isBrowser(): boolean {
-        return typeof window === "object";
+        return typeof window === "object" && typeof window.document === "object";
     }
 
+    // WebWorkers don't have a window object so the isBrowser check would fail
     public static get isWebWorker(): boolean {
         return typeof self === "object" && "importScripts" in self;
     }
 
+    // react-native has a window but no document
+    static get isReactNative(): boolean {
+        return typeof window === "object" && typeof window.document === "undefined";
+    }
+
+    // Node apps shouldn't have a window object, but WebWorkers don't either
+    // so we need to check for both WebWorker and window
     public static get isNode(): boolean {
-        return !this.isBrowser && !this.isWebWorker;
+        return !this.isBrowser && !this.isWebWorker && !this.isReactNative;
     }
 }