Skip to content

Commit

Permalink
Merge pull request Expensify#54188 from software-mansion-labs/initali…
Browse files Browse the repository at this point in the history
…se-pusher-before-rendering-children

Initialise pusher before rendering children
  • Loading branch information
aldo-expensify authored Dec 17, 2024
2 parents 0473dbc + 5393b37 commit f69d05b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/libs/Pusher/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ Onyx.connect({
});

let socket: PusherWithAuthParams | null;
let pusherSocketID = '';
let pusherSocketID: string | undefined;
const socketEventCallbacks: SocketEventCallback[] = [];
let customAuthorizer: ChannelAuthorizerGenerator;

let initPromise: Promise<void>;
let resolveInitPromise: () => void;
let initPromise = new Promise<void>((resolve) => {
resolveInitPromise = resolve;
});

const eventsBoundToChannels = new Map<Channel, Set<PusherEventName>>();

Expand All @@ -90,7 +93,7 @@ function callSocketEventCallbacks(eventName: SocketEventName, data?: EventCallba
* @returns resolves when Pusher has connected
*/
function init(args: Args, params?: unknown): Promise<void> {
initPromise = new Promise((resolve) => {
return new Promise<void>((resolve) => {
if (socket) {
resolve();
return;
Expand Down Expand Up @@ -129,7 +132,7 @@ function init(args: Args, params?: unknown): Promise<void> {
});

socket?.connection.bind('connected', () => {
pusherSocketID = socket?.connection.socket_id ?? '';
pusherSocketID = socket?.connection.socket_id;
callSocketEventCallbacks('connected');
resolve();
});
Expand All @@ -141,9 +144,7 @@ function init(args: Args, params?: unknown): Promise<void> {
socket?.connection.bind('state_change', (states: States) => {
callSocketEventCallbacks('state_change', states);
});
});

return initPromise;
}).then(resolveInitPromise);
}

/**
Expand Down Expand Up @@ -394,6 +395,9 @@ function disconnect() {
socket.disconnect();
socket = null;
pusherSocketID = '';
initPromise = new Promise((resolve) => {
resolveInitPromise = resolve;
});
}

/**
Expand All @@ -410,7 +414,7 @@ function reconnect() {
socket.connect();
}

function getPusherSocketID(): string {
function getPusherSocketID(): string | undefined {
return pusherSocketID;
}

Expand Down

0 comments on commit f69d05b

Please sign in to comment.