8000 Merge pull request #45 from firebase/fix-sign-out · iambryansanders/quickstart-js@c4dba07 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4dba07

Browse files
Merge pull request firebase#45 from firebase/fix-sign-out
Cleanup UI and stop Firebase listeners on user sign-out
2 parents 328e351 + 2c8dba2 commit c4dba07

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

database/scripts/main.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var topUserPostsSection = document.getElementById('top-user-posts-list');
3030
var recentMenuButton = document.getElementById('menu-recent');
3131
var myPostsMenuButton = document.getElementById('menu-my-posts');
3232
var myTopPostsMenuButton = document.getElementById('menu-my-top-posts');
33+
var listeningFirebaseRefs = [];
3334

3435
/**
3536
* Saves a new post to the Firebase DB.
@@ -151,16 +152,23 @@ function createPostElement(postId, title, text, author, authorId, authorPic) {
151152

152153
// Listen for likes counts.
153154
// [START post_value_event_listener]
154-
firebase.database().ref('posts/' + postId + '/starCount').on('value', function(snapshot) {
155+
var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
156+
starCountRef.on('value', function(snapshot) {
155157
updateStarCount(postElement, snapshot.val());
156158
});
157159
// [END post_value_event_listener]
158160

159161
// Listen for the starred status.
160-
firebase.database().ref('posts/' + postId + '/stars/' + uid).on('value', function(snapshot) {
162+
var starredStatusRef = firebase.database().ref('posts/' + postId + '/stars/' + uid)
163+
starredStatusRef.on('value', function(snapshot) {
161164
updateStarredByCurrentUser(postElement, snapshot.val());
162165
});
163166

167+
// Keep track of all Firebase reference on which we are listening.
168+
listeningFirebaseRefs.push(commentsRef);
169+
listeningFirebaseRefs.push(starCountRef);
170+
listeningFirebaseRefs.push(starredStatusRef);
171+
164172
// Create new comment.
165173
addCommentForm.onsubmit = function(e) {
166174
e.preventDefault();
@@ -267,9 +275,15 @@ function startDatabaseQueries() {
267275
});
268276
};
269277

278+
// Fetching and displaying all posts of each sections.
270279
fetchPosts(topUserPostsRef, topUserPostsSection);
271280
fetchPosts(recentPostsRef, recentPostsSection);
272281
fetchPosts(userPostsRef, userPostsSection);
282+
283+
// Keep track of all Firebase refs we are listening to.
284+
listeningFirebaseRefs.push(topUserPostsRef);
285+
listeningFirebaseRefs.push(recentPostsRef);
286+
listeningFirebaseRefs.push(userPostsRef);
273287
}
274288

275289
/**
@@ -305,7 +319,19 @@ window.addEventListener('load', function() {
305319
writeUserData(user.uid, user.displayName, user.email, user.photoURL);
306320
startDatabaseQueries();
307321
} else {
322+
// Display the splash page where you can sign-in.
308323
splashPage.style.display = '';
324+
325+
// Remove all previously displayed posts.
326+
topUserPostsSection.getElementsByClassName('posts-container')[0].innerHTML = '';
327+
recentPostsSection.getElementsByClassName('posts-container')[0].innerHTML = '';
328+
userPostsSection.getElementsByClassName('posts-container')[0].innerHTML = '';
329+
330+
// Stop all currently listening Firebase listeners.
331+
listeningFirebaseRefs.forEach(function(ref) {
332+
ref.off();
333+
});
334+
listeningFirebaseRefs = [];
309335
}
310336
});
311337

0 commit comments

Comments
 (0)
0