react-firestore
Version:
React components to fetch data from firestore using render props
909 lines (749 loc) • 29.7 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('preact')) :
typeof define === 'function' && define.amd ? define(['exports', 'preact'], factory) :
(global = global || self, factory(global.ReactFirestore = {}, global.preact));
}(this, function (exports, React) { 'use strict';
var React__default = 'default' in React ? React['default'] : React;
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
subClass.__proto__ = superClass;
}
var FirestoreCache = function () {};
var FirestoreContext = React__default.createContext(null);
var FirestoreProvider =
/*#__PURE__*/
function (_Component) {
_inheritsLoose(FirestoreProvider, _Component);
function FirestoreProvider(props) {
var _this = _Component.call(this, props) || this;
var firebase = props.firebase,
useTimestampsInSnapshots = props.useTimestampsInSnapshots;
var firestore = firebase.firestore();
if (typeof useTimestampsInSnapshots !== 'undefined') {
firestore.settings({
timestampsInSnapshots: useTimestampsInSnapshots
});
}
_this.state = {
firestoreDatabase: firestore,
firestoreCache: new FirestoreCache()
};
return _this;
}
var _proto = FirestoreProvider.prototype;
_proto.render = function render() {
return React__default.h(FirestoreContext.Provider, {
value: this.state
}, this.props.children);
};
return FirestoreProvider;
}(React.Component);
FirestoreProvider.defaultProps = {};
var Firestore = function (_ref) {
var render = _ref.render;
return React__default.h(FirestoreContext.Consumer, null, function (_ref2) {
var firestoreDatabase = _ref2.firestoreDatabase;
return render({
firestore: firestoreDatabase
});
});
};
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x.default : x;
}
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var reactIs_production_min = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", {
value: !0
});
var b = "function" === typeof Symbol && Symbol.for,
c = b ? Symbol.for("react.element") : 60103,
d = b ? Symbol.for("react.portal") : 60106,
e = b ? Symbol.for("react.fragment") : 60107,
f = b ? Symbol.for("react.strict_mode") : 60108,
g = b ? Symbol.for("react.profiler") : 60114,
h = b ? Symbol.for("react.provider") : 60109,
k = b ? Symbol.for("react.context") : 60110,
l = b ? Symbol.for("react.async_mode") : 60111,
m = b ? Symbol.for("react.concurrent_mode") : 60111,
n = b ? Symbol.for("react.forward_ref") : 60112,
p = b ? Symbol.for("react.suspense") : 60113,
q = b ? Symbol.for("react.memo") : 60115,
r = b ? Symbol.for("react.lazy") : 60116;
function t(a) {
if ("object" === typeof a && null !== a) {
var u = a.$$typeof;
switch (u) {
case c:
switch (a = a.type, a) {
case l:
case m:
case e:
case g:
case f:
case p:
return a;
default:
switch (a = a && a.$$typeof, a) {
case k:
case n:
case h:
return a;
default:
return u;
}
}
case r:
case q:
case d:
return u;
}
}
}
function v(a) {
return t(a) === m;
}
exports.typeOf = t;
exports.AsyncMode = l;
exports.ConcurrentMode = m;
exports.ContextConsumer = k;
exports.ContextProvider = h;
exports.Element = c;
exports.ForwardRef = n;
exports.Fragment = e;
exports.Lazy = r;
exports.Memo = q;
exports.Portal = d;
exports.Profiler = g;
exports.StrictMode = f;
exports.Suspense = p;
exports.isValidElementType = function (a) {
return "string" === typeof a || "function" === typeof a || a === e || a === m || a === g || a === f || a === p || "object" === typeof a && null !== a && (a.$$typeof === r || a.$$typeof === q || a.$$typeof === h || a.$$typeof === k || a.$$typeof === n);
};
exports.isAsyncMode = function (a) {
return v(a) || t(a) === l;
};
exports.isConcurrentMode = v;
exports.isContextConsumer = function (a) {
return t(a) === k;
};
exports.isContextProvider = function (a) {
return t(a) === h;
};
exports.isElement = function (a) {
return "object" === typeof a && null !== a && a.$$typeof === c;
};
exports.isForwardRef = function (a) {
return t(a) === n;
};
exports.isFragment = function (a) {
return t(a) === e;
};
exports.isLazy = function (a) {
return t(a) === r;
};
exports.isMemo = function (a) {
return t(a) === q;
};
exports.isPortal = function (a) {
return t(a) === d;
};
exports.isProfiler = function (a) {
return t(a) === g;
};
exports.isStrictMode = function (a) {
return t(a) === f;
};
exports.isSuspense = function (a) {
return t(a) === p;
};
});
unwrapExports(reactIs_production_min);
var reactIs_production_min_1 = reactIs_production_min.typeOf;
var reactIs_production_min_2 = reactIs_production_min.AsyncMode;
var reactIs_production_min_3 = reactIs_production_min.ConcurrentMode;
var reactIs_production_min_4 = reactIs_production_min.ContextConsumer;
var reactIs_production_min_5 = reactIs_production_min.ContextProvider;
var reactIs_production_min_6 = reactIs_production_min.Element;
var reactIs_production_min_7 = reactIs_production_min.ForwardRef;
var reactIs_production_min_8 = reactIs_production_min.Fragment;
var reactIs_production_min_9 = reactIs_production_min.Lazy;
var reactIs_production_min_10 = reactIs_production_min.Memo;
var reactIs_production_min_11 = reactIs_production_min.Portal;
var reactIs_production_min_12 = reactIs_production_min.Profiler;
var reactIs_production_min_13 = reactIs_production_min.StrictMode;
var reactIs_production_min_14 = reactIs_production_min.Suspense;
var reactIs_production_min_15 = reactIs_production_min.isValidElementType;
var reactIs_production_min_16 = reactIs_production_min.isAsyncMode;
var reactIs_production_min_17 = reactIs_production_min.isConcurrentMode;
var reactIs_production_min_18 = reactIs_production_min.isContextConsumer;
var reactIs_production_min_19 = reactIs_production_min.isContextProvider;
var reactIs_production_min_20 = reactIs_production_min.isElement;
var reactIs_production_min_21 = reactIs_production_min.isForwardRef;
var reactIs_production_min_22 = reactIs_production_min.isFragment;
var reactIs_production_min_23 = reactIs_production_min.isLazy;
var reactIs_production_min_24 = reactIs_production_min.isMemo;
var reactIs_production_min_25 = reactIs_production_min.isPortal;
var reactIs_production_min_26 = reactIs_production_min.isProfiler;
var reactIs_production_min_27 = reactIs_production_min.isStrictMode;
var reactIs_production_min_28 = reactIs_production_min.isSuspense;
var reactIs_development = createCommonjsModule(function (module, exports) {
(function () {
Object.defineProperty(exports, '__esModule', {
value: true
}); // The Symbol used to tag the ReactElement-like types. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
var hasSymbol = typeof Symbol === 'function' && Symbol.for;
var REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7;
var REACT_PORTAL_TYPE = hasSymbol ? Symbol.for('react.portal') : 0xeaca;
var REACT_FRAGMENT_TYPE = hasSymbol ? Symbol.for('react.fragment') : 0xeacb;
var REACT_STRICT_MODE_TYPE = hasSymbol ? Symbol.for('react.strict_mode') : 0xeacc;
var REACT_PROFILER_TYPE = hasSymbol ? Symbol.for('react.profiler') : 0xead2;
var REACT_PROVIDER_TYPE = hasSymbol ? Symbol.for('react.provider') : 0xeacd;
var REACT_CONTEXT_TYPE = hasSymbol ? Symbol.for('react.context') : 0xeace;
var REACT_ASYNC_MODE_TYPE = hasSymbol ? Symbol.for('react.async_mode') : 0xeacf;
var REACT_CONCURRENT_MODE_TYPE = hasSymbol ? Symbol.for('react.concurrent_mode') : 0xeacf;
var REACT_FORWARD_REF_TYPE = hasSymbol ? Symbol.for('react.forward_ref') : 0xead0;
var REACT_SUSPENSE_TYPE = hasSymbol ? Symbol.for('react.suspense') : 0xead1;
var REACT_MEMO_TYPE = hasSymbol ? Symbol.for('react.memo') : 0xead3;
var REACT_LAZY_TYPE = hasSymbol ? Symbol.for('react.lazy') : 0xead4;
/**
* Forked from fbjs/warning:
* https://github.com/facebook/fbjs/blob/e66ba20ad5be433eb54423f2b097d829324d9de6/packages/fbjs/src/__forks__/warning.js
*
* Only change is we use console.warn instead of console.error,
* and do nothing when 'console' is not supported.
* This really simplifies the code.
* ---
* Similar to invariant but only logs a warning if the condition is not met.
* This can be used to log issues in development environments in critical
* paths. Removing the logging code for production environments will keep the
* same logic and follow the same code paths.
*/
var lowPriorityWarning = function () {};
{
var printWarning = function (format) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var argIndex = 0;
var message = 'Warning: ' + format.replace(/%s/g, function () {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.warn(message);
}
try {
// --- Welcome to debugging React ---
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch (x) {}
};
lowPriorityWarning = function (condition, format) {
if (format === undefined) {
throw new Error('`lowPriorityWarning(condition, format, ...args)` requires a warning ' + 'message argument');
}
if (!condition) {
for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
args[_key2 - 2] = arguments[_key2];
}
printWarning.apply(undefined, [format].concat(args));
}
};
}
var lowPriorityWarning$1 = lowPriorityWarning;
function typeOf(object) {
if (typeof object === 'object' && object !== null) {
var $$typeof = object.$$typeof;
switch ($$typeof) {
case REACT_ELEMENT_TYPE:
var type = object.type;
switch (type) {
case REACT_ASYNC_MODE_TYPE:
case REACT_CONCURRENT_MODE_TYPE:
case REACT_FRAGMENT_TYPE:
case REACT_PROFILER_TYPE:
case REACT_STRICT_MODE_TYPE:
case REACT_SUSPENSE_TYPE:
return type;
default:
var $$typeofType = type && type.$$typeof;
switch ($$typeofType) {
case REACT_CONTEXT_TYPE:
case REACT_FORWARD_REF_TYPE:
case REACT_PROVIDER_TYPE:
return $$typeofType;
default:
return $$typeof;
}
}
case REACT_LAZY_TYPE:
case REACT_MEMO_TYPE:
case REACT_PORTAL_TYPE:
return $$typeof;
}
}
return undefined;
} // AsyncMode is deprecated along with isAsyncMode
var hasWarnedAboutDeprecatedIsAsyncMode = false; // AsyncMode should be deprecated
function isAsyncMode(object) {
{
if (!hasWarnedAboutDeprecatedIsAsyncMode) {
hasWarnedAboutDeprecatedIsAsyncMode = true;
lowPriorityWarning$1(false, 'The ReactIs.isAsyncMode() alias has been deprecated, ' + 'and will be removed in React 17+. Update your code to use ' + 'ReactIs.isConcurrentMode() instead. It has the exact same API.');
}
}
return isConcurrentMode(object) || typeOf(object) === REACT_ASYNC_MODE_TYPE;
}
function isConcurrentMode(object) {
return typeOf(object) === REACT_CONCURRENT_MODE_TYPE;
}
exports.typeOf = typeOf;
exports.AsyncMode = REACT_ASYNC_MODE_TYPE;
exports.ConcurrentMode = REACT_CONCURRENT_MODE_TYPE;
exports.ContextConsumer = REACT_CONTEXT_TYPE;
exports.ContextProvider = REACT_PROVIDER_TYPE;
exports.Element = REACT_ELEMENT_TYPE;
exports.ForwardRef = REACT_FORWARD_REF_TYPE;
exports.Fragment = REACT_FRAGMENT_TYPE;
exports.Lazy = REACT_LAZY_TYPE;
exports.Memo = REACT_MEMO_TYPE;
exports.Portal = REACT_PORTAL_TYPE;
exports.Profiler = REACT_PROFILER_TYPE;
exports.StrictMode = REACT_STRICT_MODE_TYPE;
exports.Suspense = REACT_SUSPENSE_TYPE;
exports.isValidElementType = function (type) {
return typeof type === 'string' || typeof type === 'function' || // Note: its typeof might be other than 'symbol' or 'number' if it's a polyfill.
type === REACT_FRAGMENT_TYPE || type === REACT_CONCURRENT_MODE_TYPE || type === REACT_PROFILER_TYPE || type === REACT_STRICT_MODE_TYPE || type === REACT_SUSPENSE_TYPE || typeof type === 'object' && type !== null && (type.$$typeof === REACT_LAZY_TYPE || type.$$typeof === REACT_MEMO_TYPE || type.$$typeof === REACT_PROVIDER_TYPE || type.$$typeof === REACT_CONTEXT_TYPE || type.$$typeof === REACT_FORWARD_REF_TYPE);
};
exports.isAsyncMode = isAsyncMode;
exports.isConcurrentMode = isConcurrentMode;
exports.isContextConsumer = function (object) {
return typeOf(object) === REACT_CONTEXT_TYPE;
};
exports.isContextProvider = function (object) {
return typeOf(object) === REACT_PROVIDER_TYPE;
};
exports.isElement = function (object) {
return typeof object === 'object' && object !== null && object.$$typeof === REACT_ELEMENT_TYPE;
};
exports.isForwardRef = function (object) {
return typeOf(object) === REACT_FORWARD_REF_TYPE;
};
exports.isFragment = function (object) {
return typeOf(object) === REACT_FRAGMENT_TYPE;
};
exports.isLazy = function (object) {
return typeOf(object) === REACT_LAZY_TYPE;
};
exports.isMemo = function (object) {
return typeOf(object) === REACT_MEMO_TYPE;
};
exports.isPortal = function (object) {
return typeOf(object) === REACT_PORTAL_TYPE;
};
exports.isProfiler = function (object) {
return typeOf(object) === REACT_PROFILER_TYPE;
};
exports.isStrictMode = function (object) {
return typeOf(object) === REACT_STRICT_MODE_TYPE;
};
exports.isSuspense = function (object) {
return typeOf(object) === REACT_SUSPENSE_TYPE;
};
})();
});
unwrapExports(reactIs_development);
var reactIs_development_1 = reactIs_development.typeOf;
var reactIs_development_2 = reactIs_development.AsyncMode;
var reactIs_development_3 = reactIs_development.ConcurrentMode;
var reactIs_development_4 = reactIs_development.ContextConsumer;
var reactIs_development_5 = reactIs_development.ContextProvider;
var reactIs_development_6 = reactIs_development.Element;
var reactIs_development_7 = reactIs_development.ForwardRef;
var reactIs_development_8 = reactIs_development.Fragment;
var reactIs_development_9 = reactIs_development.Lazy;
var reactIs_development_10 = reactIs_development.Memo;
var reactIs_development_11 = reactIs_development.Portal;
var reactIs_development_12 = reactIs_development.Profiler;
var reactIs_development_13 = reactIs_development.StrictMode;
var reactIs_development_14 = reactIs_development.Suspense;
var reactIs_development_15 = reactIs_development.isValidElementType;
var reactIs_development_16 = reactIs_development.isAsyncMode;
var reactIs_development_17 = reactIs_development.isConcurrentMode;
var reactIs_development_18 = reactIs_development.isContextConsumer;
var reactIs_development_19 = reactIs_development.isContextProvider;
var reactIs_development_20 = reactIs_development.isElement;
var reactIs_development_21 = reactIs_development.isForwardRef;
var reactIs_development_22 = reactIs_development.isFragment;
var reactIs_development_23 = reactIs_development.isLazy;
var reactIs_development_24 = reactIs_development.isMemo;
var reactIs_development_25 = reactIs_development.isPortal;
var reactIs_development_26 = reactIs_development.isProfiler;
var reactIs_development_27 = reactIs_development.isStrictMode;
var reactIs_development_28 = reactIs_development.isSuspense;
var reactIs = createCommonjsModule(function (module) {
module.exports = reactIs_development;
});
/**
* Copyright 2015, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
var REACT_STATICS = {
childContextTypes: true,
contextType: true,
contextTypes: true,
defaultProps: true,
displayName: true,
getDefaultProps: true,
getDerivedStateFromError: true,
getDerivedStateFromProps: true,
mixins: true,
propTypes: true,
type: true
};
var KNOWN_STATICS = {
name: true,
length: true,
prototype: true,
caller: true,
callee: true,
arguments: true,
arity: true
};
var FORWARD_REF_STATICS = {
'$$typeof': true,
render: true,
defaultProps: true,
displayName: true,
propTypes: true
};
var MEMO_STATICS = {
'$$typeof': true,
compare: true,
defaultProps: true,
displayName: true,
propTypes: true,
type: true
};
var TYPE_STATICS = {};
TYPE_STATICS[reactIs.ForwardRef] = FORWARD_REF_STATICS;
function getStatics(component) {
if (reactIs.isMemo(component)) {
return MEMO_STATICS;
}
return TYPE_STATICS[component['$$typeof']] || REACT_STATICS;
}
var defineProperty = Object.defineProperty;
var getOwnPropertyNames = Object.getOwnPropertyNames;
var getOwnPropertySymbols = Object.getOwnPropertySymbols;
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var getPrototypeOf = Object.getPrototypeOf;
var objectPrototype = Object.prototype;
function hoistNonReactStatics(targetComponent, sourceComponent, blacklist) {
if (typeof sourceComponent !== 'string') {
// don't hoist over string (html) components
if (objectPrototype) {
var inheritedComponent = getPrototypeOf(sourceComponent);
if (inheritedComponent && inheritedComponent !== objectPrototype) {
hoistNonReactStatics(targetComponent, inheritedComponent, blacklist);
}
}
var keys = getOwnPropertyNames(sourceComponent);
if (getOwnPropertySymbols) {
keys = keys.concat(getOwnPropertySymbols(sourceComponent));
}
var targetStatics = getStatics(targetComponent);
var sourceStatics = getStatics(sourceComponent);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (!KNOWN_STATICS[key] && !(blacklist && blacklist[key]) && !(sourceStatics && sourceStatics[key]) && !(targetStatics && targetStatics[key])) {
var descriptor = getOwnPropertyDescriptor(sourceComponent, key);
try {
// Avoid failures from read-only properties
defineProperty(targetComponent, key, descriptor);
} catch (e) {}
}
}
return targetComponent;
}
return targetComponent;
}
var hoistNonReactStatics_cjs = hoistNonReactStatics;
var withFirestore = function (Component) {
var C = function (_ref) {
var wrappedComponentRef = _ref.wrappedComponentRef,
remainingProps = _objectWithoutPropertiesLoose(_ref, ["wrappedComponentRef"]);
return React__default.h(FirestoreContext.Consumer, null, function (value) {
if (!value) {
throw new Error('FirestoreProvider is missing');
}
var firestoreDatabase = value.firestoreDatabase;
return React__default.h(Component, _extends({}, remainingProps, {
firestore: firestoreDatabase,
ref: wrappedComponentRef
}));
});
};
C.displayName = "withFirestore(" + (Component.displayName || Component.name) + ")";
C.WrappedComponent = Component;
return hoistNonReactStatics_cjs(C, Component);
};
/**
* Deep equality comparison for Arrays
* @param {Array} a The array to compare against
* @param {Array} b The array to compare with
* @returns {boolean} If the two arrays are equal
*/
function deepEqual(a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) {
return false;
}
}
return true;
} else {
return a === b;
}
}
var FirestoreCollection =
/*#__PURE__*/
function (_Component) {
_inheritsLoose(FirestoreCollection, _Component);
function FirestoreCollection() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _Component.call.apply(_Component, [this].concat(args)) || this;
_this.state = {
isLoading: true,
data: [],
error: null,
snapshot: null
};
_this.setupFirestoreListener = function () {
var _this$props = _this.props,
firestore = _this$props.firestore,
path = _this$props.path,
queryProps = _objectWithoutPropertiesLoose(_this$props, ["firestore", "path"]);
var collectionRef = firestore.collection(path);
var query = _this.buildQuery(collectionRef, queryProps);
_this.unsubscribe = query.onSnapshot(_this.handleOnSnapshotSuccess, _this.handleOnSnapshotError);
};
_this.handleOnSnapshotSuccess = function (snapshot) {
if (snapshot) {
_this.setState({
isLoading: false,
data: snapshot.docs.map(function (doc) {
return _extends({
id: doc.id
}, doc.data());
}),
error: null,
snapshot: snapshot
});
}
};
_this.handleOnSnapshotError = function (error) {
_this.setState({
isLoading: false,
data: [],
error: error,
snapshot: null
});
};
_this.buildQuery = function (collectionRef, queryProps) {
var sort = queryProps.sort,
limit = queryProps.limit,
filter = queryProps.filter;
var query = collectionRef;
if (sort) {
sort.split(',').forEach(function (sortItem) {
var _sortItem$split = sortItem.split(':'),
field = _sortItem$split[0],
order = _sortItem$split[1];
query = query.orderBy(field, order);
});
}
if (limit) {
query = query.limit(limit);
}
if (filter) {
//if filter is array of array, build the compound query
if (Array.isArray(filter[0])) {
filter.forEach(function (clause) {
var _query;
query = (_query = query).where.apply(_query, clause);
});
} else {
var _query2;
//build the simple query
query = (_query2 = query).where.apply(_query2, filter);
}
}
return query;
};
return _this;
}
var _proto = FirestoreCollection.prototype;
_proto.componentDidMount = function componentDidMount() {
this.setupFirestoreListener();
};
_proto.componentWillUnmount = function componentWillUnmount() {
this.handleUnsubscribe();
};
_proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
var _this2 = this;
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
}, function () {
return _this2.setupFirestoreListener();
});
}
};
_proto.handleUnsubscribe = function handleUnsubscribe() {
if (this.unsubscribe) {
this.unsubscribe();
}
};
_proto.render = function () {
var _this$props2 = this.props,
children = _this$props2.children,
render = _this$props2.render;
if (render) return render(this.state);
if (typeof children === 'function') return children(this.state);
return null;
};
return FirestoreCollection;
}(React.Component);
var FirestoreCollection$1 = withFirestore(FirestoreCollection);
var FirestoreDocument =
/*#__PURE__*/
function (_Component) {
_inheritsLoose(FirestoreDocument, _Component);
function FirestoreDocument() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _Component.call.apply(_Component, [this].concat(args)) || this;
_this.state = {
isLoading: true,
data: null,
error: null,
snapshot: null
};
_this.setupFirestoreListener = function () {
var _this$props = _this.props,
firestore = _this$props.firestore,
path = _this$props.path;
var documentRef = firestore.doc(path);
_this.unsubscribe = documentRef.onSnapshot(_this.handleOnSnapshotSuccess, _this.handleOnSnapshotError);
};
_this.handleOnSnapshotError = function (error) {
_this.setState({
isLoading: false,
error: error,
data: null,
snapshot: null
});
};
_this.handleOnSnapshotSuccess = function (snapshot) {
if (snapshot) {
var newState = {
isLoading: false,
error: null,
snapshot: snapshot
};
try {
var documentData = snapshot.data();
newState.data = _extends({
id: snapshot.id
}, documentData);
} catch (error) {
newState.error = error;
}
_this.setState(newState);
}
};
return _this;
}
var _proto = FirestoreDocument.prototype;
_proto.componentDidMount = function componentDidMount() {
this.setupFirestoreListener();
};
_proto.componentWillUnmount = function componentWillUnmount() {
this.handleUnsubscribe();
};
_proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
var _this2 = this;
if (nextProps.path !== this.props.path) {
this.handleUnsubscribe();
this.setState({
isLoading: true
}, function () {
return _this2.setupFirestoreListener();
});
}
};
_proto.handleUnsubscribe = function handleUnsubscribe() {
if (this.unsubscribe) {
this.unsubscribe();
}
};
_proto.render = function () {
var _this$props2 = this.props,
children = _this$props2.children,
render = _this$props2.render;
if (render) return render(this.state);
if (typeof children === 'function') return children(this.state);
return null;
};
return FirestoreDocument;
}(React.Component);
var FirestoreDocument$1 = withFirestore(FirestoreDocument);
exports.Firestore = Firestore;
exports.FirestoreProvider = FirestoreProvider;
exports.FirestoreCollection = FirestoreCollection$1;
exports.FirestoreDocument = FirestoreDocument$1;
exports.withFirestore = withFirestore;
Object.defineProperty(exports, '__esModule', { value: true });
}));
//# sourceMappingURL=react-firestore.umd.js.map