forked from enviPath/enviPy
1118 lines
37 KiB
JavaScript
1118 lines
37 KiB
JavaScript
import { Component, h } from 'preact';
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
var Children = {
|
|
only: function only(children) {
|
|
return children && children[0] || null;
|
|
}
|
|
};
|
|
|
|
function proptype() {}
|
|
proptype.isRequired = proptype;
|
|
|
|
var PropTypes = {
|
|
element: proptype,
|
|
func: proptype,
|
|
shape: function shape() {
|
|
return proptype;
|
|
},
|
|
instanceOf: function instanceOf() {
|
|
return proptype;
|
|
}
|
|
};
|
|
|
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
|
|
return typeof obj;
|
|
} : function (obj) {
|
|
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var classCallCheck = function (instance, Constructor) {
|
|
if (!(instance instanceof Constructor)) {
|
|
throw new TypeError("Cannot call a class as a function");
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 get = function get(object, property, receiver) {
|
|
if (object === null) object = Function.prototype;
|
|
var desc = Object.getOwnPropertyDescriptor(object, property);
|
|
|
|
if (desc === undefined) {
|
|
var parent = Object.getPrototypeOf(object);
|
|
|
|
if (parent === null) {
|
|
return undefined;
|
|
} else {
|
|
return get(parent, property, receiver);
|
|
}
|
|
} else if ("value" in desc) {
|
|
return desc.value;
|
|
} else {
|
|
var getter = desc.get;
|
|
|
|
if (getter === undefined) {
|
|
return undefined;
|
|
}
|
|
|
|
return getter.call(receiver);
|
|
}
|
|
};
|
|
|
|
var inherits = function (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;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var objectWithoutProperties = function (obj, keys) {
|
|
var target = {};
|
|
|
|
for (var i in obj) {
|
|
if (keys.indexOf(i) >= 0) continue;
|
|
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
|
|
target[i] = obj[i];
|
|
}
|
|
|
|
return target;
|
|
};
|
|
|
|
var possibleConstructorReturn = function (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;
|
|
};
|
|
|
|
|
|
|
|
var set = function set(object, property, value, receiver) {
|
|
var desc = Object.getOwnPropertyDescriptor(object, property);
|
|
|
|
if (desc === undefined) {
|
|
var parent = Object.getPrototypeOf(object);
|
|
|
|
if (parent !== null) {
|
|
set(parent, property, value, receiver);
|
|
}
|
|
} else if ("value" in desc && desc.writable) {
|
|
desc.value = value;
|
|
} else {
|
|
var setter = desc.set;
|
|
|
|
if (setter !== undefined) {
|
|
setter.call(receiver, value);
|
|
}
|
|
}
|
|
|
|
return value;
|
|
};
|
|
|
|
// encapsulates the subscription logic for connecting a component to the redux store, as
|
|
// well as nesting subscriptions of descendant components, so that we can ensure the
|
|
// ancestor components re-render before descendants
|
|
|
|
var CLEARED = null;
|
|
var nullListeners = {
|
|
notify: function notify() {}
|
|
};
|
|
|
|
function createListenerCollection() {
|
|
// the current/next pattern is copied from redux's createStore code.
|
|
// TODO: refactor+expose that code to be reusable here?
|
|
var current = [];
|
|
var next = [];
|
|
|
|
return {
|
|
clear: function clear() {
|
|
next = CLEARED;
|
|
current = CLEARED;
|
|
},
|
|
notify: function notify() {
|
|
var listeners = current = next;
|
|
for (var i = 0; i < listeners.length; i++) {
|
|
listeners[i]();
|
|
}
|
|
},
|
|
subscribe: function subscribe(listener) {
|
|
var isSubscribed = true;
|
|
if (next === current) next = current.slice();
|
|
next.push(listener);
|
|
|
|
return function unsubscribe() {
|
|
if (!isSubscribed || current === CLEARED) return;
|
|
isSubscribed = false;
|
|
|
|
if (next === current) next = current.slice();
|
|
next.splice(next.indexOf(listener), 1);
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
var Subscription = function () {
|
|
function Subscription(store, parentSub) {
|
|
classCallCheck(this, Subscription);
|
|
|
|
this.store = store;
|
|
this.parentSub = parentSub;
|
|
this.unsubscribe = null;
|
|
this.listeners = nullListeners;
|
|
}
|
|
|
|
Subscription.prototype.addNestedSub = function addNestedSub(listener) {
|
|
this.trySubscribe();
|
|
return this.listeners.subscribe(listener);
|
|
};
|
|
|
|
Subscription.prototype.notifyNestedSubs = function notifyNestedSubs() {
|
|
this.listeners.notify();
|
|
};
|
|
|
|
Subscription.prototype.isSubscribed = function isSubscribed() {
|
|
return Boolean(this.unsubscribe);
|
|
};
|
|
|
|
Subscription.prototype.trySubscribe = function trySubscribe() {
|
|
if (!this.unsubscribe) {
|
|
// this.onStateChange is set by connectAdvanced.initSubscription()
|
|
this.unsubscribe = this.parentSub ? this.parentSub.addNestedSub(this.onStateChange) : this.store.subscribe(this.onStateChange);
|
|
|
|
this.listeners = createListenerCollection();
|
|
}
|
|
};
|
|
|
|
Subscription.prototype.tryUnsubscribe = function tryUnsubscribe() {
|
|
if (this.unsubscribe) {
|
|
this.unsubscribe();
|
|
this.unsubscribe = null;
|
|
this.listeners.clear();
|
|
this.listeners = nullListeners;
|
|
}
|
|
};
|
|
|
|
return Subscription;
|
|
}();
|
|
|
|
var storeShape = PropTypes.shape({
|
|
subscribe: PropTypes.func.isRequired,
|
|
dispatch: PropTypes.func.isRequired,
|
|
getState: PropTypes.func.isRequired
|
|
});
|
|
|
|
/**
|
|
* Prints a warning in the console if it exists.
|
|
*
|
|
* @param {String} message The warning message.
|
|
* @returns {void}
|
|
*/
|
|
function warning(message) {
|
|
/* eslint-disable no-console */
|
|
if (typeof console !== 'undefined' && typeof console.error === 'function') {
|
|
console.error(message);
|
|
}
|
|
/* eslint-enable no-console */
|
|
try {
|
|
// This error was thrown as a convenience so that if you enable
|
|
// "break on all exceptions" in your console,
|
|
// it would pause the execution at this line.
|
|
throw new Error(message);
|
|
/* eslint-disable no-empty */
|
|
} catch (e) {}
|
|
/* eslint-enable no-empty */
|
|
}
|
|
|
|
var didWarnAboutReceivingStore = false;
|
|
function warnAboutReceivingStore() {
|
|
if (didWarnAboutReceivingStore) {
|
|
return;
|
|
}
|
|
didWarnAboutReceivingStore = true;
|
|
|
|
warning('<Provider> does not support changing `store` on the fly. ' + 'It is most likely that you see this error because you updated to ' + 'Redux 2.x and React Redux 2.x which no longer hot reload reducers ' + 'automatically. See https://github.com/reactjs/react-redux/releases/' + 'tag/v2.0.0 for the migration instructions.');
|
|
}
|
|
|
|
var Provider = function (_Component) {
|
|
inherits(Provider, _Component);
|
|
|
|
Provider.prototype.getChildContext = function getChildContext() {
|
|
return { store: this.store, storeSubscription: null };
|
|
};
|
|
|
|
function Provider(props, context) {
|
|
classCallCheck(this, Provider);
|
|
|
|
var _this = possibleConstructorReturn(this, _Component.call(this, props, context));
|
|
|
|
_this.store = props.store;
|
|
return _this;
|
|
}
|
|
|
|
Provider.prototype.render = function render() {
|
|
return Children.only(this.props.children);
|
|
};
|
|
|
|
return Provider;
|
|
}(Component);
|
|
|
|
{
|
|
Provider.prototype.componentWillReceiveProps = function (nextProps) {
|
|
var store = this.store;
|
|
var nextStore = nextProps.store;
|
|
|
|
|
|
if (store !== nextStore) {
|
|
warnAboutReceivingStore();
|
|
}
|
|
};
|
|
}
|
|
|
|
Provider.childContextTypes = {
|
|
store: storeShape.isRequired,
|
|
storeSubscription: PropTypes.instanceOf(Subscription)
|
|
};
|
|
Provider.displayName = 'Provider';
|
|
|
|
/**
|
|
* Copyright 2015, Yahoo! Inc.
|
|
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
|
*/
|
|
var REACT_STATICS = {
|
|
childContextTypes: true,
|
|
contextTypes: true,
|
|
defaultProps: true,
|
|
displayName: true,
|
|
getDefaultProps: true,
|
|
mixins: true,
|
|
propTypes: true,
|
|
type: true
|
|
};
|
|
|
|
var KNOWN_STATICS = {
|
|
name: true,
|
|
length: true,
|
|
prototype: true,
|
|
caller: true,
|
|
arguments: true,
|
|
arity: true
|
|
};
|
|
|
|
var isGetOwnPropertySymbolsAvailable = typeof Object.getOwnPropertySymbols === 'function';
|
|
|
|
var index = function hoistNonReactStatics(targetComponent, sourceComponent, customStatics) {
|
|
if (typeof sourceComponent !== 'string') {
|
|
// don't hoist over string (html) components
|
|
var keys = Object.getOwnPropertyNames(sourceComponent);
|
|
|
|
/* istanbul ignore else */
|
|
if (isGetOwnPropertySymbolsAvailable) {
|
|
keys = keys.concat(Object.getOwnPropertySymbols(sourceComponent));
|
|
}
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
if (!REACT_STATICS[keys[i]] && !KNOWN_STATICS[keys[i]] && (!customStatics || !customStatics[keys[i]])) {
|
|
try {
|
|
targetComponent[keys[i]] = sourceComponent[keys[i]];
|
|
} catch (error) {}
|
|
}
|
|
}
|
|
}
|
|
|
|
return targetComponent;
|
|
};
|
|
|
|
var invariant = function () {}
|
|
|
|
var hotReloadingVersion = 0;
|
|
function connectAdvanced(
|
|
/*
|
|
selectorFactory is a func that is responsible for returning the selector function used to
|
|
compute new props from state, props, and dispatch. For example:
|
|
export default connectAdvanced((dispatch, options) => (state, props) => ({
|
|
thing: state.things[props.thingId],
|
|
saveThing: fields => dispatch(actionCreators.saveThing(props.thingId, fields)),
|
|
}))(YourComponent)
|
|
Access to dispatch is provided to the factory so selectorFactories can bind actionCreators
|
|
outside of their selector as an optimization. Options passed to connectAdvanced are passed to
|
|
the selectorFactory, along with displayName and WrappedComponent, as the second argument.
|
|
Note that selectorFactory is responsible for all caching/memoization of inbound and outbound
|
|
props. Do not use connectAdvanced directly without memoizing results between calls to your
|
|
selector, otherwise the Connect component will re-render on every state or props change.
|
|
*/
|
|
selectorFactory) {
|
|
var _contextTypes, _childContextTypes;
|
|
|
|
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
|
|
|
var _ref$getDisplayName = _ref.getDisplayName;
|
|
var getDisplayName = _ref$getDisplayName === undefined ? function (name) {
|
|
return 'ConnectAdvanced(' + name + ')';
|
|
} : _ref$getDisplayName;
|
|
var _ref$methodName = _ref.methodName;
|
|
var methodName = _ref$methodName === undefined ? 'connectAdvanced' : _ref$methodName;
|
|
var _ref$renderCountProp = _ref.renderCountProp;
|
|
var renderCountProp = _ref$renderCountProp === undefined ? undefined : _ref$renderCountProp;
|
|
var _ref$shouldHandleStat = _ref.shouldHandleStateChanges;
|
|
var shouldHandleStateChanges = _ref$shouldHandleStat === undefined ? true : _ref$shouldHandleStat;
|
|
var _ref$storeKey = _ref.storeKey;
|
|
var storeKey = _ref$storeKey === undefined ? 'store' : _ref$storeKey;
|
|
var _ref$withRef = _ref.withRef;
|
|
var withRef = _ref$withRef === undefined ? false : _ref$withRef;
|
|
var connectOptions = objectWithoutProperties(_ref, ['getDisplayName', 'methodName', 'renderCountProp', 'shouldHandleStateChanges', 'storeKey', 'withRef']);
|
|
|
|
var subscriptionKey = storeKey + 'Subscription';
|
|
var version = hotReloadingVersion++;
|
|
|
|
var contextTypes = (_contextTypes = {}, _contextTypes[storeKey] = storeShape, _contextTypes[subscriptionKey] = PropTypes.instanceOf(Subscription), _contextTypes);
|
|
var childContextTypes = (_childContextTypes = {}, _childContextTypes[subscriptionKey] = PropTypes.instanceOf(Subscription), _childContextTypes);
|
|
|
|
return function wrapWithConnect(WrappedComponent) {
|
|
invariant(typeof WrappedComponent == 'function', 'You must pass a component to the function returned by ' + ('connect. Instead received ' + WrappedComponent));
|
|
|
|
var wrappedComponentName = WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
|
|
|
var displayName = getDisplayName(wrappedComponentName);
|
|
|
|
var selectorFactoryOptions = _extends({}, connectOptions, {
|
|
getDisplayName: getDisplayName,
|
|
methodName: methodName,
|
|
renderCountProp: renderCountProp,
|
|
shouldHandleStateChanges: shouldHandleStateChanges,
|
|
storeKey: storeKey,
|
|
withRef: withRef,
|
|
displayName: displayName,
|
|
wrappedComponentName: wrappedComponentName,
|
|
WrappedComponent: WrappedComponent
|
|
});
|
|
|
|
var Connect = function (_Component) {
|
|
inherits(Connect, _Component);
|
|
|
|
function Connect(props, context) {
|
|
classCallCheck(this, Connect);
|
|
|
|
var _this = possibleConstructorReturn(this, _Component.call(this, props, context));
|
|
|
|
_this.version = version;
|
|
_this.state = {};
|
|
_this.renderCount = 0;
|
|
_this.store = _this.props[storeKey] || _this.context[storeKey];
|
|
_this.parentSub = props[subscriptionKey] || context[subscriptionKey];
|
|
|
|
_this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
|
|
|
|
invariant(_this.store, 'Could not find "' + storeKey + '" in either the context or ' + ('props of "' + displayName + '". ') + 'Either wrap the root component in a <Provider>, ' + ('or explicitly pass "' + storeKey + '" as a prop to "' + displayName + '".'));
|
|
|
|
// make sure `getState` is properly bound in order to avoid breaking
|
|
// custom store implementations that rely on the store's context
|
|
_this.getState = _this.store.getState.bind(_this.store);
|
|
|
|
_this.initSelector();
|
|
_this.initSubscription();
|
|
return _this;
|
|
}
|
|
|
|
Connect.prototype.getChildContext = function getChildContext() {
|
|
var _ref2;
|
|
|
|
return _ref2 = {}, _ref2[subscriptionKey] = this.subscription || this.parentSub, _ref2;
|
|
};
|
|
|
|
Connect.prototype.componentDidMount = function componentDidMount() {
|
|
if (!shouldHandleStateChanges) return;
|
|
|
|
// componentWillMount fires during server side rendering, but componentDidMount and
|
|
// componentWillUnmount do not. Because of this, trySubscribe happens during ...didMount.
|
|
// Otherwise, unsubscription would never take place during SSR, causing a memory leak.
|
|
// To handle the case where a child component may have triggered a state change by
|
|
// dispatching an action in its componentWillMount, we have to re-run the select and maybe
|
|
// re-render.
|
|
this.subscription.trySubscribe();
|
|
this.selector.run(this.props);
|
|
if (this.selector.shouldComponentUpdate) this.forceUpdate();
|
|
};
|
|
|
|
Connect.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
|
this.selector.run(nextProps);
|
|
};
|
|
|
|
Connect.prototype.shouldComponentUpdate = function shouldComponentUpdate() {
|
|
return this.selector.shouldComponentUpdate;
|
|
};
|
|
|
|
Connect.prototype.componentWillUnmount = function componentWillUnmount() {
|
|
if (this.subscription) this.subscription.tryUnsubscribe();
|
|
// these are just to guard against extra memory leakage if a parent element doesn't
|
|
// dereference this instance properly, such as an async callback that never finishes
|
|
this.subscription = null;
|
|
this.store = null;
|
|
this.parentSub = null;
|
|
this.selector.run = function () {};
|
|
};
|
|
|
|
Connect.prototype.getWrappedInstance = function getWrappedInstance() {
|
|
invariant(withRef, 'To access the wrapped instance, you need to specify ' + ('{ withRef: true } in the options argument of the ' + methodName + '() call.'));
|
|
return this.wrappedInstance;
|
|
};
|
|
|
|
Connect.prototype.setWrappedInstance = function setWrappedInstance(ref) {
|
|
this.wrappedInstance = ref;
|
|
};
|
|
|
|
Connect.prototype.initSelector = function initSelector() {
|
|
var dispatch = this.store.dispatch;
|
|
var getState = this.getState;
|
|
|
|
var sourceSelector = selectorFactory(dispatch, selectorFactoryOptions);
|
|
|
|
// wrap the selector in an object that tracks its results between runs
|
|
var selector = this.selector = {
|
|
shouldComponentUpdate: true,
|
|
props: sourceSelector(getState(), this.props),
|
|
run: function runComponentSelector(props) {
|
|
try {
|
|
var nextProps = sourceSelector(getState(), props);
|
|
if (selector.error || nextProps !== selector.props) {
|
|
selector.shouldComponentUpdate = true;
|
|
selector.props = nextProps;
|
|
selector.error = null;
|
|
}
|
|
} catch (error) {
|
|
selector.shouldComponentUpdate = true;
|
|
selector.error = error;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
Connect.prototype.initSubscription = function initSubscription() {
|
|
var _this2 = this;
|
|
|
|
if (shouldHandleStateChanges) {
|
|
(function () {
|
|
var subscription = _this2.subscription = new Subscription(_this2.store, _this2.parentSub);
|
|
var dummyState = {};
|
|
|
|
subscription.onStateChange = function onStateChange() {
|
|
this.selector.run(this.props);
|
|
|
|
if (!this.selector.shouldComponentUpdate) {
|
|
subscription.notifyNestedSubs();
|
|
} else {
|
|
this.componentDidUpdate = function componentDidUpdate() {
|
|
this.componentDidUpdate = undefined;
|
|
subscription.notifyNestedSubs();
|
|
};
|
|
|
|
this.setState(dummyState);
|
|
}
|
|
}.bind(_this2);
|
|
})();
|
|
}
|
|
};
|
|
|
|
Connect.prototype.isSubscribed = function isSubscribed() {
|
|
return Boolean(this.subscription) && this.subscription.isSubscribed();
|
|
};
|
|
|
|
Connect.prototype.addExtraProps = function addExtraProps(props) {
|
|
if (!withRef && !renderCountProp) return props;
|
|
// make a shallow copy so that fields added don't leak to the original selector.
|
|
// this is especially important for 'ref' since that's a reference back to the component
|
|
// instance. a singleton memoized selector would then be holding a reference to the
|
|
// instance, preventing the instance from being garbage collected, and that would be bad
|
|
var withExtras = _extends({}, props);
|
|
if (withRef) withExtras.ref = this.setWrappedInstance;
|
|
if (renderCountProp) withExtras[renderCountProp] = this.renderCount++;
|
|
return withExtras;
|
|
};
|
|
|
|
Connect.prototype.render = function render() {
|
|
var selector = this.selector;
|
|
selector.shouldComponentUpdate = false;
|
|
|
|
if (selector.error) {
|
|
throw selector.error;
|
|
} else {
|
|
return h(WrappedComponent, this.addExtraProps(selector.props));
|
|
}
|
|
};
|
|
|
|
return Connect;
|
|
}(Component);
|
|
|
|
Connect.WrappedComponent = WrappedComponent;
|
|
Connect.displayName = displayName;
|
|
Connect.childContextTypes = childContextTypes;
|
|
Connect.contextTypes = contextTypes;
|
|
|
|
|
|
{
|
|
Connect.prototype.componentWillUpdate = function componentWillUpdate() {
|
|
// We are hot reloading!
|
|
if (this.version !== version) {
|
|
this.version = version;
|
|
this.initSelector();
|
|
|
|
if (this.subscription) this.subscription.tryUnsubscribe();
|
|
this.initSubscription();
|
|
if (shouldHandleStateChanges) this.subscription.trySubscribe();
|
|
}
|
|
};
|
|
}
|
|
|
|
return index(Connect, WrappedComponent);
|
|
};
|
|
}
|
|
|
|
var hasOwn = Object.prototype.hasOwnProperty;
|
|
|
|
function shallowEqual(a, b) {
|
|
if (a === b) return true;
|
|
|
|
var countA = 0;
|
|
var countB = 0;
|
|
|
|
for (var key in a) {
|
|
if (hasOwn.call(a, key) && a[key] !== b[key]) return false;
|
|
countA++;
|
|
}
|
|
|
|
for (var _key in b) {
|
|
if (hasOwn.call(b, _key)) countB++;
|
|
}
|
|
|
|
return countA === countB;
|
|
}
|
|
|
|
/* Built-in method references for those with the same name as other `lodash` methods. */
|
|
var nativeGetPrototype = Object.getPrototypeOf;
|
|
|
|
/**
|
|
* Gets the `[[Prototype]]` of `value`.
|
|
*
|
|
* @private
|
|
* @param {*} value The value to query.
|
|
* @returns {null|Object} Returns the `[[Prototype]]`.
|
|
*/
|
|
function getPrototype(value) {
|
|
return nativeGetPrototype(Object(value));
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is a host object in IE < 9.
|
|
*
|
|
* @private
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is a host object, else `false`.
|
|
*/
|
|
function isHostObject(value) {
|
|
// Many host objects are `Object` objects that can coerce to strings
|
|
// despite having improperly defined `toString` methods.
|
|
var result = false;
|
|
if (value != null && typeof value.toString != 'function') {
|
|
try {
|
|
result = !!(value + '');
|
|
} catch (e) {}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Checks if `value` is object-like. A value is object-like if it's not `null`
|
|
* and has a `typeof` result of "object".
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @since 4.0.0
|
|
* @category Lang
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
|
* @example
|
|
*
|
|
* _.isObjectLike({});
|
|
* // => true
|
|
*
|
|
* _.isObjectLike([1, 2, 3]);
|
|
* // => true
|
|
*
|
|
* _.isObjectLike(_.noop);
|
|
* // => false
|
|
*
|
|
* _.isObjectLike(null);
|
|
* // => false
|
|
*/
|
|
function isObjectLike(value) {
|
|
return !!value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) == 'object';
|
|
}
|
|
|
|
/** `Object#toString` result references. */
|
|
var objectTag = '[object Object]';
|
|
|
|
/** Used for built-in method references. */
|
|
var objectProto = Object.prototype;
|
|
|
|
/** Used to resolve the decompiled source of functions. */
|
|
var funcToString = Function.prototype.toString;
|
|
|
|
/** Used to check objects for own properties. */
|
|
var hasOwnProperty = objectProto.hasOwnProperty;
|
|
|
|
/** Used to infer the `Object` constructor. */
|
|
var objectCtorString = funcToString.call(Object);
|
|
|
|
/**
|
|
* Used to resolve the
|
|
* [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
|
* of values.
|
|
*/
|
|
var objectToString = objectProto.toString;
|
|
|
|
/**
|
|
* Checks if `value` is a plain object, that is, an object created by the
|
|
* `Object` constructor or one with a `[[Prototype]]` of `null`.
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @since 0.8.0
|
|
* @category Lang
|
|
* @param {*} value The value to check.
|
|
* @returns {boolean} Returns `true` if `value` is a plain object,
|
|
* else `false`.
|
|
* @example
|
|
*
|
|
* function Foo() {
|
|
* this.a = 1;
|
|
* }
|
|
*
|
|
* _.isPlainObject(new Foo);
|
|
* // => false
|
|
*
|
|
* _.isPlainObject([1, 2, 3]);
|
|
* // => false
|
|
*
|
|
* _.isPlainObject({ 'x': 0, 'y': 0 });
|
|
* // => true
|
|
*
|
|
* _.isPlainObject(Object.create(null));
|
|
* // => true
|
|
*/
|
|
function isPlainObject(value) {
|
|
if (!isObjectLike(value) || objectToString.call(value) != objectTag || isHostObject(value)) {
|
|
return false;
|
|
}
|
|
var proto = getPrototype(value);
|
|
if (proto === null) {
|
|
return true;
|
|
}
|
|
var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor;
|
|
return typeof Ctor == 'function' && Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString;
|
|
}
|
|
|
|
function verifyPlainObject(value, displayName, methodName) {
|
|
if (!isPlainObject(value)) {
|
|
warning(methodName + '() in ' + displayName + ' must return a plain object. Instead received ' + value + '.');
|
|
}
|
|
}
|
|
|
|
function wrapMapToPropsConstant(getConstant) {
|
|
return function initConstantSelector(dispatch, options) {
|
|
var constant = getConstant(dispatch, options);
|
|
|
|
function constantSelector() {
|
|
return constant;
|
|
}
|
|
constantSelector.dependsOnOwnProps = false;
|
|
return constantSelector;
|
|
};
|
|
}
|
|
|
|
// dependsOnOwnProps is used by createMapToPropsProxy to determine whether to pass props as args
|
|
// to the mapToProps function being wrapped. It is also used by makePurePropsSelector to determine
|
|
// whether mapToProps needs to be invoked when props have changed.
|
|
//
|
|
// A length of one signals that mapToProps does not depend on props from the parent component.
|
|
// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
|
|
// therefore not reporting its length accurately..
|
|
function getDependsOnOwnProps(mapToProps) {
|
|
return mapToProps.dependsOnOwnProps !== null && mapToProps.dependsOnOwnProps !== undefined ? Boolean(mapToProps.dependsOnOwnProps) : mapToProps.length !== 1;
|
|
}
|
|
|
|
// Used by whenMapStateToPropsIsFunction and whenMapDispatchToPropsIsFunction,
|
|
// this function wraps mapToProps in a proxy function which does several things:
|
|
//
|
|
// * Detects whether the mapToProps function being called depends on props, which
|
|
// is used by selectorFactory to decide if it should reinvoke on props changes.
|
|
//
|
|
// * On first call, handles mapToProps if returns another function, and treats that
|
|
// new function as the true mapToProps for subsequent calls.
|
|
//
|
|
// * On first call, verifies the first result is a plain object, in order to warn
|
|
// the developer that their mapToProps function is not returning a valid result.
|
|
//
|
|
function wrapMapToPropsFunc(mapToProps, methodName) {
|
|
return function initProxySelector(dispatch, _ref) {
|
|
var displayName = _ref.displayName;
|
|
|
|
var proxy = function mapToPropsProxy(stateOrDispatch, ownProps) {
|
|
return proxy.dependsOnOwnProps ? proxy.mapToProps(stateOrDispatch, ownProps) : proxy.mapToProps(stateOrDispatch);
|
|
};
|
|
|
|
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps);
|
|
|
|
proxy.mapToProps = function detectFactoryAndVerify(stateOrDispatch, ownProps) {
|
|
proxy.mapToProps = mapToProps;
|
|
var props = proxy(stateOrDispatch, ownProps);
|
|
|
|
if (typeof props === 'function') {
|
|
proxy.mapToProps = props;
|
|
proxy.dependsOnOwnProps = getDependsOnOwnProps(props);
|
|
props = proxy(stateOrDispatch, ownProps);
|
|
}
|
|
|
|
verifyPlainObject(props, displayName, methodName);
|
|
|
|
return props;
|
|
};
|
|
|
|
return proxy;
|
|
};
|
|
}
|
|
|
|
function whenMapDispatchToPropsIsFunction(mapDispatchToProps) {
|
|
return typeof mapDispatchToProps === 'function' ? wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps') : undefined;
|
|
}
|
|
|
|
function whenMapDispatchToPropsIsMissing(mapDispatchToProps) {
|
|
return !mapDispatchToProps ? wrapMapToPropsConstant(function (dispatch) {
|
|
return { dispatch: dispatch };
|
|
}) : undefined;
|
|
}
|
|
|
|
function whenMapDispatchToPropsIsObject(mapDispatchToProps) {
|
|
return mapDispatchToProps && (typeof mapDispatchToProps === 'undefined' ? 'undefined' : _typeof(mapDispatchToProps)) === 'object' ? wrapMapToPropsConstant(function (dispatch) {
|
|
return bindActionCreators(mapDispatchToProps, dispatch);
|
|
}) : undefined;
|
|
}
|
|
|
|
var defaultMapDispatchToPropsFactories = [whenMapDispatchToPropsIsFunction, whenMapDispatchToPropsIsMissing, whenMapDispatchToPropsIsObject];
|
|
|
|
function whenMapStateToPropsIsFunction(mapStateToProps) {
|
|
return typeof mapStateToProps === 'function' ? wrapMapToPropsFunc(mapStateToProps, 'mapStateToProps') : undefined;
|
|
}
|
|
|
|
function whenMapStateToPropsIsMissing(mapStateToProps) {
|
|
return !mapStateToProps ? wrapMapToPropsConstant(function () {
|
|
return {};
|
|
}) : undefined;
|
|
}
|
|
|
|
var defaultMapStateToPropsFactories = [whenMapStateToPropsIsFunction, whenMapStateToPropsIsMissing];
|
|
|
|
function defaultMergeProps(stateProps, dispatchProps, ownProps) {
|
|
return _extends({}, ownProps, stateProps, dispatchProps);
|
|
}
|
|
|
|
function wrapMergePropsFunc(mergeProps) {
|
|
return function initMergePropsProxy(dispatch, _ref) {
|
|
var displayName = _ref.displayName;
|
|
var pure = _ref.pure;
|
|
var areMergedPropsEqual = _ref.areMergedPropsEqual;
|
|
|
|
var hasRunOnce = false;
|
|
var mergedProps = void 0;
|
|
|
|
return function mergePropsProxy(stateProps, dispatchProps, ownProps) {
|
|
var nextMergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
|
|
if (hasRunOnce) {
|
|
if (!pure || !areMergedPropsEqual(nextMergedProps, mergedProps)) mergedProps = nextMergedProps;
|
|
} else {
|
|
hasRunOnce = true;
|
|
mergedProps = nextMergedProps;
|
|
|
|
verifyPlainObject(mergedProps, displayName, 'mergeProps');
|
|
}
|
|
|
|
return mergedProps;
|
|
};
|
|
};
|
|
}
|
|
|
|
function whenMergePropsIsFunction(mergeProps) {
|
|
return typeof mergeProps === 'function' ? wrapMergePropsFunc(mergeProps) : undefined;
|
|
}
|
|
|
|
function whenMergePropsIsOmitted(mergeProps) {
|
|
return !mergeProps ? function () {
|
|
return defaultMergeProps;
|
|
} : undefined;
|
|
}
|
|
|
|
var defaultMergePropsFactories = [whenMergePropsIsFunction, whenMergePropsIsOmitted];
|
|
|
|
function verify(selector, methodName, displayName) {
|
|
if (!selector) {
|
|
throw new Error('Unexpected value for ' + methodName + ' in ' + displayName + '.');
|
|
} else if (methodName === 'mapStateToProps' || methodName === 'mapDispatchToProps') {
|
|
if (!selector.hasOwnProperty('dependsOnOwnProps')) {
|
|
warning('The selector for ' + methodName + ' of ' + displayName + ' did not specify a value for dependsOnOwnProps.');
|
|
}
|
|
}
|
|
}
|
|
|
|
function verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps, displayName) {
|
|
verify(mapStateToProps, 'mapStateToProps', displayName);
|
|
verify(mapDispatchToProps, 'mapDispatchToProps', displayName);
|
|
verify(mergeProps, 'mergeProps', displayName);
|
|
}
|
|
|
|
function impureFinalPropsSelectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch) {
|
|
return function impureFinalPropsSelector(state, ownProps) {
|
|
return mergeProps(mapStateToProps(state, ownProps), mapDispatchToProps(dispatch, ownProps), ownProps);
|
|
};
|
|
}
|
|
|
|
function pureFinalPropsSelectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, _ref) {
|
|
var areStatesEqual = _ref.areStatesEqual;
|
|
var areOwnPropsEqual = _ref.areOwnPropsEqual;
|
|
var areStatePropsEqual = _ref.areStatePropsEqual;
|
|
|
|
var hasRunAtLeastOnce = false;
|
|
var state = void 0;
|
|
var ownProps = void 0;
|
|
var stateProps = void 0;
|
|
var dispatchProps = void 0;
|
|
var mergedProps = void 0;
|
|
|
|
function handleFirstCall(firstState, firstOwnProps) {
|
|
state = firstState;
|
|
ownProps = firstOwnProps;
|
|
stateProps = mapStateToProps(state, ownProps);
|
|
dispatchProps = mapDispatchToProps(dispatch, ownProps);
|
|
mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
hasRunAtLeastOnce = true;
|
|
return mergedProps;
|
|
}
|
|
|
|
function handleNewPropsAndNewState() {
|
|
stateProps = mapStateToProps(state, ownProps);
|
|
|
|
if (mapDispatchToProps.dependsOnOwnProps) dispatchProps = mapDispatchToProps(dispatch, ownProps);
|
|
|
|
mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
return mergedProps;
|
|
}
|
|
|
|
function handleNewProps() {
|
|
if (mapStateToProps.dependsOnOwnProps) stateProps = mapStateToProps(state, ownProps);
|
|
|
|
if (mapDispatchToProps.dependsOnOwnProps) dispatchProps = mapDispatchToProps(dispatch, ownProps);
|
|
|
|
mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
return mergedProps;
|
|
}
|
|
|
|
function handleNewState() {
|
|
var nextStateProps = mapStateToProps(state, ownProps);
|
|
var statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps);
|
|
stateProps = nextStateProps;
|
|
|
|
if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
|
|
return mergedProps;
|
|
}
|
|
|
|
function handleSubsequentCalls(nextState, nextOwnProps) {
|
|
var propsChanged = !areOwnPropsEqual(nextOwnProps, ownProps);
|
|
var stateChanged = !areStatesEqual(nextState, state);
|
|
state = nextState;
|
|
ownProps = nextOwnProps;
|
|
|
|
if (propsChanged && stateChanged) return handleNewPropsAndNewState();
|
|
if (propsChanged) return handleNewProps();
|
|
if (stateChanged) return handleNewState();
|
|
return mergedProps;
|
|
}
|
|
|
|
return function pureFinalPropsSelector(nextState, nextOwnProps) {
|
|
return hasRunAtLeastOnce ? handleSubsequentCalls(nextState, nextOwnProps) : handleFirstCall(nextState, nextOwnProps);
|
|
};
|
|
}
|
|
|
|
// TODO: Add more comments
|
|
|
|
// If pure is true, the selector returned by selectorFactory will memoize its results,
|
|
// allowing connectAdvanced's shouldComponentUpdate to return false if final
|
|
// props have not changed. If false, the selector will always return a new
|
|
// object and shouldComponentUpdate will always return true.
|
|
|
|
function finalPropsSelectorFactory(dispatch, _ref2) {
|
|
var initMapStateToProps = _ref2.initMapStateToProps;
|
|
var initMapDispatchToProps = _ref2.initMapDispatchToProps;
|
|
var initMergeProps = _ref2.initMergeProps;
|
|
var options = objectWithoutProperties(_ref2, ['initMapStateToProps', 'initMapDispatchToProps', 'initMergeProps']);
|
|
|
|
var mapStateToProps = initMapStateToProps(dispatch, options);
|
|
var mapDispatchToProps = initMapDispatchToProps(dispatch, options);
|
|
var mergeProps = initMergeProps(dispatch, options);
|
|
|
|
{
|
|
verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps, options.displayName);
|
|
}
|
|
|
|
var selectorFactory = options.pure ? pureFinalPropsSelectorFactory : impureFinalPropsSelectorFactory;
|
|
|
|
return selectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, options);
|
|
}
|
|
|
|
/*
|
|
connect is a facade over connectAdvanced. It turns its args into a compatible
|
|
selectorFactory, which has the signature:
|
|
|
|
(dispatch, options) => (nextState, nextOwnProps) => nextFinalProps
|
|
|
|
connect passes its args to connectAdvanced as options, which will in turn pass them to
|
|
selectorFactory each time a Connect component instance is instantiated or hot reloaded.
|
|
|
|
selectorFactory returns a final props selector from its mapStateToProps,
|
|
mapStateToPropsFactories, mapDispatchToProps, mapDispatchToPropsFactories, mergeProps,
|
|
mergePropsFactories, and pure args.
|
|
|
|
The resulting final props selector is called by the Connect component instance whenever
|
|
it receives new props or store state.
|
|
*/
|
|
|
|
function match(arg, factories, name) {
|
|
for (var i = factories.length - 1; i >= 0; i--) {
|
|
var result = factories[i](arg);
|
|
if (result) return result;
|
|
}
|
|
|
|
return function (dispatch, options) {
|
|
throw new Error('Invalid value of type ' + (typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) + ' for ' + name + ' argument when connecting component ' + options.wrappedComponentName + '.');
|
|
};
|
|
}
|
|
|
|
function strictEqual(a, b) {
|
|
return a === b;
|
|
}
|
|
|
|
// createConnect with default args builds the 'official' connect behavior. Calling it with
|
|
// different options opens up some testing and extensibility scenarios
|
|
function createConnect() {
|
|
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
|
|
|
var _ref$connectHOC = _ref.connectHOC;
|
|
var connectHOC = _ref$connectHOC === undefined ? connectAdvanced : _ref$connectHOC;
|
|
var _ref$mapStateToPropsF = _ref.mapStateToPropsFactories;
|
|
var mapStateToPropsFactories = _ref$mapStateToPropsF === undefined ? defaultMapStateToPropsFactories : _ref$mapStateToPropsF;
|
|
var _ref$mapDispatchToPro = _ref.mapDispatchToPropsFactories;
|
|
var mapDispatchToPropsFactories = _ref$mapDispatchToPro === undefined ? defaultMapDispatchToPropsFactories : _ref$mapDispatchToPro;
|
|
var _ref$mergePropsFactor = _ref.mergePropsFactories;
|
|
var mergePropsFactories = _ref$mergePropsFactor === undefined ? defaultMergePropsFactories : _ref$mergePropsFactor;
|
|
var _ref$selectorFactory = _ref.selectorFactory;
|
|
var selectorFactory = _ref$selectorFactory === undefined ? finalPropsSelectorFactory : _ref$selectorFactory;
|
|
|
|
return function connect(mapStateToProps, mapDispatchToProps, mergeProps) {
|
|
var _ref2 = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
|
|
|
|
var _ref2$pure = _ref2.pure;
|
|
var pure = _ref2$pure === undefined ? true : _ref2$pure;
|
|
var _ref2$areStatesEqual = _ref2.areStatesEqual;
|
|
var areStatesEqual = _ref2$areStatesEqual === undefined ? strictEqual : _ref2$areStatesEqual;
|
|
var _ref2$areOwnPropsEqua = _ref2.areOwnPropsEqual;
|
|
var areOwnPropsEqual = _ref2$areOwnPropsEqua === undefined ? shallowEqual : _ref2$areOwnPropsEqua;
|
|
var _ref2$areStatePropsEq = _ref2.areStatePropsEqual;
|
|
var areStatePropsEqual = _ref2$areStatePropsEq === undefined ? shallowEqual : _ref2$areStatePropsEq;
|
|
var _ref2$areMergedPropsE = _ref2.areMergedPropsEqual;
|
|
var areMergedPropsEqual = _ref2$areMergedPropsE === undefined ? shallowEqual : _ref2$areMergedPropsE;
|
|
var extraOptions = objectWithoutProperties(_ref2, ['pure', 'areStatesEqual', 'areOwnPropsEqual', 'areStatePropsEqual', 'areMergedPropsEqual']);
|
|
|
|
var initMapStateToProps = match(mapStateToProps, mapStateToPropsFactories, 'mapStateToProps');
|
|
var initMapDispatchToProps = match(mapDispatchToProps, mapDispatchToPropsFactories, 'mapDispatchToProps');
|
|
var initMergeProps = match(mergeProps, mergePropsFactories, 'mergeProps');
|
|
|
|
return connectHOC(selectorFactory, _extends({
|
|
// used in error messages
|
|
methodName: 'connect',
|
|
|
|
// used to compute Connect's displayName from the wrapped component's displayName.
|
|
getDisplayName: function getDisplayName(name) {
|
|
return 'Connect(' + name + ')';
|
|
},
|
|
|
|
// if mapStateToProps is falsy, the Connect component doesn't subscribe to store state changes
|
|
shouldHandleStateChanges: Boolean(mapStateToProps),
|
|
|
|
// passed through to selectorFactory
|
|
initMapStateToProps: initMapStateToProps,
|
|
initMapDispatchToProps: initMapDispatchToProps,
|
|
initMergeProps: initMergeProps,
|
|
pure: pure,
|
|
areStatesEqual: areStatesEqual,
|
|
areOwnPropsEqual: areOwnPropsEqual,
|
|
areStatePropsEqual: areStatePropsEqual,
|
|
areMergedPropsEqual: areMergedPropsEqual
|
|
|
|
}, extraOptions));
|
|
};
|
|
}
|
|
|
|
var connect$1 = createConnect();
|
|
|
|
|
|
|
|
var lib$1 = {
|
|
Provider: Provider,
|
|
connect: connect$1,
|
|
connectAdvanced: connectAdvanced
|
|
};
|
|
|
|
export default lib$1;
|
|
//# sourceMappingURL=preact-redux.esm.js.map
|