[lib] refactor sequential handling of Tunnelbroker messages
Summary:
This is a fix for ENG-8968.
In the previous implementation, peerToPeerMessageHandler was not always reactive to state changes, this scenario was possible:
- peerToPeerMessageHandler is a callback and memoized based on current dependencies.
- tunnelbrokerMessageListener is invoked two times in a row with create_thread, and send_text_message. There is a promise created for create_thread and immediately there is a second one spawned for send_text_message (with the memoized value of peerToPeerMessageHandler).
- Promise for create_thread is resolved, it causes some changes in the thread store.
- Promise for send_text_message is starting, but it is using the memoized value of peerToPeerMessageHandler (from point 1), at that time, the DM thread is not yet in the store, so this fails because there is no thread associated with the text message.
This fix makes sure, that we always use an updated version of peerToPeerMessageHandler. Instead of changing promises, use messages queue, peerToPeerMessageHandler is called right before execution, not ahead of time.
Test Plan:
- Open two clients, A and B.
- Makes B goes offline.
- On A, send thread creation and a couple of text messages.
- Open Client B.
- Thread and all DM messages are visible.
Additionally, testing this effect does not do any unnecessary computations.
Reviewers: tomek, inka
Reviewed By: tomek
Subscribers: ashoat
Differential Revision: https://phab.comm.dev/D13078