Remove double async wrapping

Summary:
Our usage of requestIdleCallback (probably) causes more trouble than it solves:

1. It makes sure everything is processed asynchronously. But since everything is arriving over a network stack, that is already the case without wrapping it again to run on a separate event loop tick
2. The timeout we set before `500` forces the app to give _more_ priority to message processing instead of less
3. In a next diff (D20151700) in this stack we will make sure that messages are not processed immediately, but simple stored, which should not be significantly more expensive (probably even cheaper) than scheduling another tick on the event loop

Reviewed By: jknoxville

Differential Revision: D20557104

fbshipit-source-id: 6cc10ba537e3cb5f31e6c32e1fdeb57c20f06f17
This commit is contained in:
Michel Weststrate
2020-03-23 06:42:08 -07:00
committed by Facebook GitHub Bot
parent d103692883
commit aa2879d6e8
2 changed files with 12 additions and 15 deletions

View File

@@ -121,7 +121,6 @@ export default class Client extends EventEmitter {
logger: Logger; logger: Logger;
lastSeenDeviceList: Array<BaseDevice>; lastSeenDeviceList: Array<BaseDevice>;
broadcastCallbacks: Map<string, Map<string, Set<Function>>>; broadcastCallbacks: Map<string, Map<string, Set<Function>>>;
rIC: any;
requestCallbacks: Map< requestCallbacks: Map<
number, number,
@@ -167,14 +166,6 @@ export default class Client extends EventEmitter {
} }
const client = this; const client = this;
// node.js doesn't support requestIdleCallback
this.rIC =
typeof window === 'undefined' || !window.requestIdleCallback
? (cb: Function, _: any) => {
cb();
}
: window.requestIdleCallback.bind(window);
if (conn) { if (conn) {
conn.connectionStatus().subscribe({ conn.connectionStatus().subscribe({
onNext(payload) { onNext(payload) {

View File

@@ -252,7 +252,10 @@ class Server extends EventEmitter {
socket, socket,
{app, os, device, device_id, sdk_version}, {app, os, device, device_id, sdk_version},
{csr, csr_path}, {csr, csr_path},
); ).then(client => {
return (resolvedClient = client);
});
let resolvedClient: Client | undefined;
socket.connectionStatus().subscribe({ socket.connectionStatus().subscribe({
onNext(payload) { onNext(payload) {
@@ -269,12 +272,15 @@ class Server extends EventEmitter {
}); });
return { return {
fireAndForget: (payload: {data: string}) => fireAndForget: (payload: {data: string}) => {
if (resolvedClient) {
resolvedClient.onMessage(payload.data);
} else {
client.then(client => { client.then(client => {
client.rIC(() => client.onMessage(payload.data), { client.onMessage(payload.data);
timeout: 500,
}); });
}), }
},
}; };
}; };