Skip to content

Commit 181b8b5

Browse files
authored
Merge pull request #73 from devshackio/master
Allow removal of specific callbacks from query listeners
2 parents 7a8c458 + 04174af commit 181b8b5

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -592,23 +592,33 @@ storageRef.downloadUrl()
592592

593593
### Realtime Database
594594

595-
#### database attribute
596-
597595
The native Firebase JavaScript library provides a featureful realtime database that works out of the box. Firestack provides an attribute to interact with the database without needing to configure the JS library.
598596

597+
#### DatabaseRef
598+
599+
Firestack attempts to provide the same API as the JS Firebase library for both Android and iOS platforms. [Check out the firebase guide](https://firebase.google.com/docs/database/web/read-and-write) for more information on how to use the JS library.
600+
601+
#### Example
602+
599603
```javascript
600-
firestack.database
601-
.ref(LIST_KEY)
602-
.on('value', snapshot => {
603-
if (snapshot.val()) {
604-
console.log('The list was updated');
605-
}
606-
});
607-
```
608604

609-
#### DatabaseRef
605+
function handleValueChange(snapshot) {
606+
if (snapshot.val()) {
607+
console.log('The list was updated');
608+
}
609+
}
610+
611+
const LIST_KEY = 'path/to/data';
612+
firestack.database.ref(LIST_KEY).on('value', handleValueChange);
610613

611-
Firestack attempts to provide the same API as the JS Firebase library for both Android and iOS platforms.
614+
// Calling `.off` with a reference to the callback function will only remove that specific listener.
615+
// This is useful if multiple components are listening and unlistening to the same ref path.
616+
firestack.database.ref(LIST_KEY).off('value', handleValueChange);
617+
618+
// Calling `.off` without passing the callback function will remove *all* 'value' listeners for that ref
619+
firestack.database.ref(LIST_KEY).off('value');
620+
621+
```
612622

613623
// TODO: Finish documenting
614624

lib/modules/database.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,14 @@ class DatabaseRef extends ReferenceBase {
211211
})
212212
}
213213

214-
off(evt='') {
214+
off(evt='', origCB) {
215215
const path = this.dbPath();
216-
return this.db.off(path, evt)
216+
return this.db.off(path, evt, origCB)
217217
.then(({callback, subscriptions}) => {
218+
if (dbSubscriptions[path][evt].length > 0) {
219+
return subscriptions;
220+
}
221+
218222
return promisify('off', FirestackDatabase)(path, evt)
219223
.then(() => {
220224
// subscriptions.forEach(sub => sub.remove());
@@ -432,15 +436,20 @@ export class Database extends Base {
432436
return Promise.resolve({callback, subscriptions});
433437
}
434438

435-
off(path, evt) {
439+
off(path, evt, origCB) {
436440
const key = this._pathKey(path);
437441
// Remove subscription
438442
if (dbSubscriptions[key]) {
439443
if (!evt || evt === "") {
440444
dbSubscriptions[key] = {};
441445
} else if (dbSubscriptions[key][evt]) {
442-
delete dbSubscriptions[key][evt];
446+
if (origCB) {
447+
dbSubscriptions[key][evt].splice(dbSubscriptions[key][evt].indexOf(origCB), 1);
448+
} else {
449+
delete dbSubscriptions[key][evt];
450+
}
443451
}
452+
444453
if (Object.keys(dbSubscriptions[key]).length <= 0) {
445454
// there are no more subscriptions
446455
// so we can unwatch

0 commit comments

Comments
 (0)