Skip to content

Commit

Permalink
feat: update firestore listener when props change (#10)
Browse files Browse the repository at this point in the history
* Adds componentWillReceiveProps lifecycle method; adds tests

* Just resets `isLoading` state; wraps unsubscribe in an if, following the unmount lifecycle pattern

* WIP: updating tests for full coverage

* Finishes updating tests

* Adds lodash.isEqual for deep equality check

* Finishes adding tests

* Removes lodash dependency in favor of custom array comparison helper
  • Loading branch information
damonbauer authored and green-arrow committed Mar 19, 2018
1 parent 78787c9 commit 48cb1bf
Show file tree
Hide file tree
Showing 6 changed files with 561 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/FirestoreCollection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component } from 'react';
import PropTypes from 'prop-types';
import deepEqual from './utils/deepEqual';

class FirestoreCollection extends Component {
static propTypes = {
Expand Down Expand Up @@ -29,6 +30,25 @@ class FirestoreCollection extends Component {
}

componentWillUnmount() {
this.handleUnsubscribe();
}

componentWillReceiveProps(nextProps) {
if (
nextProps.path !== this.props.path ||
nextProps.sort !== this.props.sort ||
nextProps.limit !== this.props.limit ||
!deepEqual(nextProps.filter, this.props.filter)
) {
this.handleUnsubscribe();

this.setState({ isLoading: true }, () =>
this.setupFirestoreListener(this.props),
);
}
}

handleUnsubscribe() {
if (this.unsubscribe) {
this.unsubscribe();
}
Expand Down
14 changes: 14 additions & 0 deletions src/FirestoreDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ class FirestoreDocument extends Component {
}

componentWillUnmount() {
this.handleUnsubscribe();
}

componentWillReceiveProps(nextProps) {
if (nextProps.path !== this.props.path) {
this.handleUnsubscribe();

this.setState({ isLoading: true }, () =>
this.setupFirestoreListener(this.props),
);
}
}

handleUnsubscribe() {
if (this.unsubscribe) {
this.unsubscribe();
}
Expand Down
Loading

0 comments on commit 48cb1bf

Please sign in to comment.