-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from BeeMargarida/feat/tab_leader_write
#15321: Only one tab writes to the DB
- Loading branch information
Showing
38 changed files
with
67,766 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: e2e | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize] | ||
paths: ['tests/e2e/**', 'lib/**', 'package.json', 'package-lock.json'] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/cache@v3 | ||
with: | ||
path: ~/.npm | ||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | ||
restore-keys: ${{ runner.os }}-node- | ||
|
||
- name: Setup Node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version-file: '.nvmrc' | ||
|
||
- run: npm ci | ||
|
||
- run: npx playwright install --with-deps | ||
|
||
- run: npm run e2e | ||
env: | ||
CI: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Determines when the client is ready. We need to wait till the init method is called and the leader message is sent. | ||
*/ | ||
declare function isReady(): Promise<void>; | ||
|
||
/** | ||
* Subscribes to the broadcast channel to listen for messages from other tabs, so that | ||
* all tabs agree on who the leader is, which should always be the last tab to open. | ||
*/ | ||
declare function init(): void; | ||
|
||
/** | ||
* Returns a boolean indicating if the current client is the leader. | ||
*/ | ||
declare function isClientTheLeader(): boolean; | ||
|
||
/** | ||
* Subscribes to when the client changes. | ||
*/ | ||
declare function subscribeToClientChange(callback: () => {}): void; | ||
|
||
export {isReady, init, isClientTheLeader, subscribeToClientChange}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* For native devices, there will never be more than one | ||
* client running at a time, so this lib is a big no-op | ||
*/ | ||
|
||
function isReady() { | ||
return Promise.resolve(); | ||
} | ||
|
||
function isClientTheLeader() { | ||
return true; | ||
} | ||
|
||
function init() {} | ||
|
||
function subscribeToClientChange() {} | ||
|
||
export { | ||
isClientTheLeader, | ||
init, | ||
isReady, | ||
subscribeToClientChange, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* When you have many tabs in one browser, the data of Onyx is shared between all of them. Since we persist write requests in Onyx, we need to ensure that | ||
* only one tab is processing those saved requests or we would be duplicating data (or creating errors). | ||
* This file ensures exactly that by tracking all the clientIDs connected, storing the most recent one last and it considers that last clientID the "leader". | ||
*/ | ||
|
||
import * as Str from '../Str'; | ||
import * as Broadcast from '../broadcast'; | ||
|
||
const NEW_LEADER_MESSAGE = 'NEW_LEADER'; | ||
const REMOVED_LEADER_MESSAGE = 'REMOVE_LEADER'; | ||
|
||
const clientID = Str.guid(); | ||
const subscribers = []; | ||
let timestamp = null; | ||
|
||
let activeClientID = null; | ||
let setIsReady = () => {}; | ||
const isReadyPromise = new Promise((resolve) => { | ||
setIsReady = resolve; | ||
}); | ||
|
||
/** | ||
* Determines when the client is ready. We need to wait both till we saved our ID in onyx AND the init method was called | ||
* @returns {Promise} | ||
*/ | ||
function isReady() { | ||
return isReadyPromise; | ||
} | ||
|
||
/** | ||
* Returns a boolean indicating if the current client is the leader. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function isClientTheLeader() { | ||
return activeClientID === clientID; | ||
} | ||
|
||
/** | ||
* Subscribes to when the client changes. | ||
* @param {Function} callback | ||
*/ | ||
function subscribeToClientChange(callback) { | ||
subscribers.push(callback); | ||
} | ||
|
||
/** | ||
* Subscribe to the broadcast channel to listen for messages from other tabs, so that | ||
* all tabs agree on who the leader is, which should always be the last tab to open. | ||
*/ | ||
function init() { | ||
Broadcast.subscribe((message) => { | ||
switch (message.data.type) { | ||
case NEW_LEADER_MESSAGE: { | ||
// Only update the active leader if the message received was from another | ||
// tab that initialized after the current one; if the timestamps are the | ||
// same, it uses the client ID to tie-break | ||
const isTimestampEqual = timestamp === message.data.timestamp; | ||
const isTimestampNewer = timestamp > message.data.timestamp; | ||
if (isClientTheLeader() && (isTimestampNewer || (isTimestampEqual && clientID > message.data.clientID))) { | ||
return; | ||
} | ||
activeClientID = message.data.clientID; | ||
|
||
subscribers.forEach(callback => callback()); | ||
break; | ||
} | ||
case REMOVED_LEADER_MESSAGE: | ||
activeClientID = clientID; | ||
timestamp = Date.now(); | ||
Broadcast.sendMessage({type: NEW_LEADER_MESSAGE, clientID, timestamp}); | ||
subscribers.forEach(callback => callback()); | ||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
|
||
activeClientID = clientID; | ||
timestamp = Date.now(); | ||
|
||
Broadcast.sendMessage({type: NEW_LEADER_MESSAGE, clientID, timestamp}); | ||
setIsReady(); | ||
|
||
window.addEventListener('beforeunload', () => { | ||
if (!isClientTheLeader()) { | ||
return; | ||
} | ||
Broadcast.sendMessage({type: REMOVED_LEADER_MESSAGE, clientID}); | ||
}); | ||
} | ||
|
||
export { | ||
isClientTheLeader, | ||
init, | ||
isReady, | ||
subscribeToClientChange, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.