-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed onDisconnect logic to use deviceId #1010
Draft
maneesht
wants to merge
6
commits into
main
Choose a base branch
from
mtewani/fix-ondisconnect-logic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdc7669
Fixed onDisconnect logic to use deviceId
maneesht 0e8fccc
Removed unnecessary changes
maneesht 2cdfa44
Renamed some variables
maneesht b0ddd63
Addressed comment
maneesht 60e0d05
Fixed offline mode bug
maneesht 066cbe2
Removed unnecessary code
maneesht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -21,7 +21,8 @@ function rtdb_presence() { | |
|
||
// Create a reference to this user's specific status node. | ||
// This is where we will store data about being online/offline. | ||
var userStatusDatabaseRef = firebase.database().ref('/status/' + uid); | ||
// `push()` will add a new key to the user's path, acting as a deviceId | ||
var deviceStatusDatabaseRef = firebase.database().ref('/status/' + uid + '/devices').push(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// We'll create two constants which we will write to | ||
// the Realtime database when this device is offline | ||
|
@@ -49,25 +50,26 @@ function rtdb_presence() { | |
// method to add a set which will only trigger once this | ||
// client has disconnected by closing the app, | ||
// losing internet, or any other means. | ||
userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { | ||
deviceStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { | ||
// The promise returned from .onDisconnect().set() will | ||
// resolve as soon as the server acknowledges the onDisconnect() | ||
// request, NOT once we've actually disconnected: | ||
// https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect | ||
|
||
// We can now safely set ourselves as 'online' knowing that the | ||
// server will mark us as offline once we lose connection. | ||
userStatusDatabaseRef.set(isOnlineForDatabase); | ||
deviceStatusDatabaseRef.set(isOnlineForDatabase); | ||
}); | ||
}); | ||
// [END rtdb_presence] | ||
} | ||
|
||
function rtdb_and_local_fs_presence() { | ||
async function rtdb_and_local_fs_presence() { | ||
// [START rtdb_and_local_fs_presence] | ||
// [START_EXCLUDE] | ||
var uid = firebase.auth().currentUser.uid; | ||
var userStatusDatabaseRef = firebase.database().ref('/status/' + uid); | ||
var deviceStatusDatabaseRef = await firebase.database().ref('/status/' + uid + '/devices').push(); | ||
var deviceId = deviceStatusDatabaseRef.key; | ||
|
||
var isOfflineForDatabase = { | ||
state: 'offline', | ||
|
@@ -80,34 +82,39 @@ function rtdb_and_local_fs_presence() { | |
}; | ||
|
||
// [END_EXCLUDE] | ||
var userStatusFirestoreRef = firebase.firestore().doc('/status/' + uid); | ||
var userFirestoreRef = firebase.firestore().collection('status').doc(uid); | ||
await userFirestoreRef.set({ uid }); // In case the user's id document doesn't already exist. | ||
|
||
var devicesCollectionRef = userFirestoreRef.collection('devices'); | ||
var deviceStatusFirestoreRef = devicesCollectionRef.doc(deviceId); | ||
|
||
|
||
// Firestore uses a different server timestamp value, so we'll | ||
// create two more constants for Firestore state. | ||
var isOfflineForFirestore = { | ||
state: 'offline', | ||
last_changed: firebase.firestore.FieldValue.serverTimestamp(), | ||
}; | ||
|
||
var isOnlineForFirestore = { | ||
state: 'online', | ||
last_changed: firebase.firestore.FieldValue.serverTimestamp(), | ||
}; | ||
|
||
firebase.database().ref('.info/connected').on('value', function(snapshot) { | ||
if (snapshot.val() == false) { | ||
// Instead of simply returning, we'll also set Firestore's state | ||
// to 'offline'. This ensures that our Firestore cache is aware | ||
// of the switch to 'offline.' | ||
userStatusFirestoreRef.set(isOfflineForFirestore); | ||
firebase.database().ref('.info/connected').on('value', async function(snapshot) { | ||
if (snapshot.val() === false) { | ||
// Instead of simply returning, we'll also remove the device id from the user, | ||
// and if no devices are left, we should delete the user as well. | ||
// This ensures that our Firestore cache is aware that the device and/or user has been deleted | ||
deviceStatusFirestoreRef.delete(); | ||
const deviceCollection = await devicesCollectionRef.get(); | ||
if(deviceCollection.empty) { | ||
userFirestoreRef.delete(); | ||
} | ||
return; | ||
}; | ||
|
||
userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { | ||
userStatusDatabaseRef.set(isOnlineForDatabase); | ||
deviceStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { | ||
deviceStatusDatabaseRef.set(isOnlineForDatabase); | ||
|
||
// We'll also add Firestore set here for when we come online. | ||
userStatusFirestoreRef.set(isOnlineForFirestore); | ||
userFirestoreRef.set({ uid }); | ||
deviceStatusFirestoreRef.set(isOnlineForFirestore); | ||
}); | ||
}); | ||
// [END rtdb_and_local_fs_presence] | ||
|
@@ -116,7 +123,7 @@ function rtdb_and_local_fs_presence() { | |
function fs_listen() { | ||
// [START fs_onsnapshot] | ||
userStatusFirestoreRef.onSnapshot(function(doc) { | ||
var isOnline = doc.data().state == 'online'; | ||
var isOnline = !doc.empty; | ||
// ... use isOnline | ||
}); | ||
// [END fs_onsnapshot] | ||
|
@@ -126,7 +133,6 @@ function fs_listen_online() { | |
var history = document.querySelector('#history'); | ||
// [START fs_onsnapshot_online] | ||
firebase.firestore().collection('status') | ||
.where('state', '==', 'online') | ||
.onSnapshot(function(snapshot) { | ||
snapshot.docChanges().forEach(function(change) { | ||
if (change.type === 'added') { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async/await
syntax targets ES2016+, which is inconsistent with the rest of the file. Looking for whether ES5 is the target, or ES2016+