redux-devshare
Version:
[![NPM version][npm-image]][npm-url] [![NPM downloads][npm-downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Code Climate][climate-image]][climate-url] [![Code Coverage][coverage-i
143 lines (112 loc) • 6.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _isEqual2 = require('lodash/isEqual');
var _isEqual3 = _interopRequireDefault(_isEqual2);
var _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; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _query = require('./actions/query');
var _utils = require('./utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* @name devshareConnect
* @extends React.Component
* @description Higher Order Component that automatically listens/unListens
* to provided devshare paths using React's Lifecycle hooks.
* @param {Array} watchArray - Array of objects or strings for paths to sync from Firebase
* @return {Function} - that accepts a component to wrap and returns the wrapped component
* @example <caption>Basic</caption>
* // this.props.devshare set on App component as devshare object with helpers
* import { devshareConnect } from 'react-redux-devshare'
* export default devshareConnect()(App)
* @example <caption>Data</caption>
* import { connect } from 'react-redux'
* import { devshareConnect, dataToJS } from 'redux-devshare'
*
* // sync /todos from devshare into redux
* const fbWrapped = devshareConnect([
* 'todos'
* ])(App)
*
* // pass todos list from redux as this.props.todosList
* export default connect(({ devshare }) => ({
* todosList: dataToJS(devshare, 'todos'),
* profile: pathToJS(devshare, 'profile'), // pass profile data as this.props.proifle
* auth: pathToJS(devshare, 'auth') // pass auth data as this.props.auth
* }))(fbWrapped)
*/
exports.default = function () {
var dataOrFn = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return function (WrappedComponent) {
var DevshareConnect = function (_Component) {
_inherits(DevshareConnect, _Component);
function DevshareConnect(props, context) {
_classCallCheck(this, DevshareConnect);
var _this = _possibleConstructorReturn(this, (DevshareConnect.__proto__ || Object.getPrototypeOf(DevshareConnect)).call(this, props, context));
_this._devshareEvents = [];
_this.devshare = null;
return _this;
}
_createClass(DevshareConnect, [{
key: 'componentWillMount',
value: function componentWillMount() {
var _context$store = this.context.store,
devshare = _context$store.devshare,
dispatch = _context$store.dispatch;
// Allow function to be passed
var inputAsFunc = (0, _utils.createCallable)(dataOrFn);
this.prevData = inputAsFunc(this.props, devshare);
this.devshare = devshare;
this._devshareEvents = (0, _utils.getEventsFromInput)(this.prevData);
(0, _query.watchEvents)(devshare, dispatch, this._devshareEvents);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var _context$store2 = this.context.store,
devshare = _context$store2.devshare,
dispatch = _context$store2.dispatch;
(0, _query.unWatchEvents)(devshare, dispatch, this._devshareEvents);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(np) {
var _context$store3 = this.context.store,
devshare = _context$store3.devshare,
dispatch = _context$store3.dispatch;
var inputAsFunc = (0, _utils.createCallable)(dataOrFn);
var data = inputAsFunc(np, devshare);
// Handle a data parameter having changed
if (!(0, _isEqual3.default)(data, this.prevData)) {
this.prevData = data;
// UnWatch all current events
(0, _query.unWatchEvents)(devshare, dispatch, this._devshareEvents);
// Get watch events from new data
this._devshareEvents = (0, _utils.getEventsFromInput)(data);
// Watch new events
(0, _query.watchEvents)(devshare, dispatch, this._devshareEvents);
}
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(WrappedComponent, _extends({}, this.props, this.state, {
devshare: this.devshare
}));
}
}]);
return DevshareConnect;
}(_react.Component);
DevshareConnect.contextTypes = {
store: _react.PropTypes.object.isRequired
};
return DevshareConnect;
};
};
module.exports = exports['default'];