Skip to content

Commit

Permalink
Use window.document for React-Native (#38340)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy authored Nov 23, 2021
1 parent 9dd048a commit 465f929
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/SignalR/clients/ts/signalr/src/HttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.`);
}

Expand Down
10 changes: 3 additions & 7 deletions src/SignalR/clients/ts/signalr/src/HubConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions src/SignalR/clients/ts/signalr/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 465f929

Please sign in to comment.