Skip to content

Commit

Permalink
The online/offline checker now presumes that globalThis is the even…
Browse files Browse the repository at this point in the history
…t target (#3658)

# Summary

In the isolated Content Scripts environment, `globalThis.window` is apparently not a thing. In this PR we eliminate that nesting and presume that the event target for listening to online/offline events is `globalThis` itself.

# Test Plan

Tested that `globalThis.addEventListener('offline', () => {})` fires the callback in Brave when the network connection goes down. Presumed that the same is true for all other browsers.

Fixes #3652.
  • Loading branch information
steveluscher authored Dec 4, 2024
1 parent a3b2202 commit 70eb596
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-zoos-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/rpc-subscriptions': patch
---

The online/offline checker in the subscriptions implementation no longer throws an error when hosted in the Content Scripts environment of a browser extension
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('getRpcSubscriptionsChannelWithAutoping', () => {
let mockOn: jest.Mock;
let mockSend: jest.Mock;
let mockWindowAddEventListener: jest.Mock;
let originalWindowAddEventListener: typeof globalThis.window.addEventListener;
let originalWindowAddEventListener: typeof globalThis.addEventListener;
function receiveError(error?: unknown) {
mockOn.mock.calls.filter(([type]) => type === 'error').forEach(([_, listener]) => listener(error));
}
Expand All @@ -25,8 +25,8 @@ describe('getRpcSubscriptionsChannelWithAutoping', () => {
beforeEach(() => {
jest.useFakeTimers();
if (__BROWSER__) {
originalWindowAddEventListener = globalThis.window.addEventListener;
globalThis.window.addEventListener = mockWindowAddEventListener = jest.fn();
originalWindowAddEventListener = globalThis.addEventListener;
globalThis.addEventListener = mockWindowAddEventListener = jest.fn();
}
mockOn = jest.fn().mockReturnValue(() => {});
mockSend = jest.fn().mockResolvedValue(void 0);
Expand All @@ -37,7 +37,7 @@ describe('getRpcSubscriptionsChannelWithAutoping', () => {
});
afterEach(() => {
if (__BROWSER__) {
globalThis.window.addEventListener = originalWindowAddEventListener;
globalThis.addEventListener = originalWindowAddEventListener;
}
});
it('sends a ping message to the channel at the specified interval', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ export function getRpcSubscriptionsChannelWithAutoping<TChannel extends RpcSubsc
restartPingTimer();
}
if (__BROWSER__) {
globalThis.window.addEventListener(
globalThis.addEventListener(
'offline',
function handleOffline() {
clearInterval(intervalId);
},
{ signal: pingerAbortController.signal },
);
globalThis.window.addEventListener(
globalThis.addEventListener(
'online',
function handleOnline() {
sendPing();
Expand Down

0 comments on commit 70eb596

Please sign in to comment.