1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
4 |
|
---|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
---|
6 |
|
---|
7 | var React = _interopDefault(require('react'));
|
---|
8 | var immutable = require('immutable');
|
---|
9 |
|
---|
10 | function _typeof(obj) {
|
---|
11 | if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
---|
12 | _typeof = function (obj) {
|
---|
13 | return typeof obj;
|
---|
14 | };
|
---|
15 | } else {
|
---|
16 | _typeof = function (obj) {
|
---|
17 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
---|
18 | };
|
---|
19 | }
|
---|
20 |
|
---|
21 | return _typeof(obj);
|
---|
22 | }
|
---|
23 |
|
---|
24 | function _classCallCheck(instance, Constructor) {
|
---|
25 | if (!(instance instanceof Constructor)) {
|
---|
26 | throw new TypeError("Cannot call a class as a function");
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | function _defineProperties(target, props) {
|
---|
31 | for (var i = 0; i < props.length; i++) {
|
---|
32 | var descriptor = props[i];
|
---|
33 | descriptor.enumerable = descriptor.enumerable || false;
|
---|
34 | descriptor.configurable = true;
|
---|
35 | if ("value" in descriptor) descriptor.writable = true;
|
---|
36 | Object.defineProperty(target, descriptor.key, descriptor);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | function _createClass(Constructor, protoProps, staticProps) {
|
---|
41 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
---|
42 | if (staticProps) _defineProperties(Constructor, staticProps);
|
---|
43 | return Constructor;
|
---|
44 | }
|
---|
45 |
|
---|
46 | function _defineProperty(obj, key, value) {
|
---|
47 | if (key in obj) {
|
---|
48 | Object.defineProperty(obj, key, {
|
---|
49 | value: value,
|
---|
50 | enumerable: true,
|
---|
51 | configurable: true,
|
---|
52 | writable: true
|
---|
53 | });
|
---|
54 | } else {
|
---|
55 | obj[key] = value;
|
---|
56 | }
|
---|
57 |
|
---|
58 | return obj;
|
---|
59 | }
|
---|
60 |
|
---|
61 | function ownKeys(object, enumerableOnly) {
|
---|
62 | var keys = Object.keys(object);
|
---|
63 |
|
---|
64 | if (Object.getOwnPropertySymbols) {
|
---|
65 | var symbols = Object.getOwnPropertySymbols(object);
|
---|
66 | if (enumerableOnly) symbols = symbols.filter(function (sym) {
|
---|
67 | return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
---|
68 | });
|
---|
69 | keys.push.apply(keys, symbols);
|
---|
70 | }
|
---|
71 |
|
---|
72 | return keys;
|
---|
73 | }
|
---|
74 |
|
---|
75 | function _objectSpread2(target) {
|
---|
76 | for (var i = 1; i < arguments.length; i++) {
|
---|
77 | var source = arguments[i] != null ? arguments[i] : {};
|
---|
78 |
|
---|
79 | if (i % 2) {
|
---|
80 | ownKeys(source, true).forEach(function (key) {
|
---|
81 | _defineProperty(target, key, source[key]);
|
---|
82 | });
|
---|
83 | } else if (Object.getOwnPropertyDescriptors) {
|
---|
84 | Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
---|
85 | } else {
|
---|
86 | ownKeys(source).forEach(function (key) {
|
---|
87 | Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
---|
88 | });
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | return target;
|
---|
93 | }
|
---|
94 |
|
---|
95 | function _inherits(subClass, superClass) {
|
---|
96 | if (typeof superClass !== "function" && superClass !== null) {
|
---|
97 | throw new TypeError("Super expression must either be null or a function");
|
---|
98 | }
|
---|
99 |
|
---|
100 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
---|
101 | constructor: {
|
---|
102 | value: subClass,
|
---|
103 | writable: true,
|
---|
104 | configurable: true
|
---|
105 | }
|
---|
106 | });
|
---|
107 | if (superClass) _setPrototypeOf(subClass, superClass);
|
---|
108 | }
|
---|
109 |
|
---|
110 | function _getPrototypeOf(o) {
|
---|
111 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
---|
112 | return o.__proto__ || Object.getPrototypeOf(o);
|
---|
113 | };
|
---|
114 | return _getPrototypeOf(o);
|
---|
115 | }
|
---|
116 |
|
---|
117 | function _setPrototypeOf(o, p) {
|
---|
118 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
---|
119 | o.__proto__ = p;
|
---|
120 | return o;
|
---|
121 | };
|
---|
122 |
|
---|
123 | return _setPrototypeOf(o, p);
|
---|
124 | }
|
---|
125 |
|
---|
126 | function _assertThisInitialized(self) {
|
---|
127 | if (self === void 0) {
|
---|
128 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
---|
129 | }
|
---|
130 |
|
---|
131 | return self;
|
---|
132 | }
|
---|
133 |
|
---|
134 | function _possibleConstructorReturn(self, call) {
|
---|
135 | if (call && (typeof call === "object" || typeof call === "function")) {
|
---|
136 | return call;
|
---|
137 | }
|
---|
138 |
|
---|
139 | return _assertThisInitialized(self);
|
---|
140 | }
|
---|
141 |
|
---|
142 | var NOT_SET = {};
|
---|
143 | function isMapLike(collection) {
|
---|
144 | return collection !== null && _typeof(collection) === 'object' && typeof collection.get === 'function' && typeof collection.has === 'function';
|
---|
145 | }
|
---|
146 |
|
---|
147 | function isInvalid(collection) {
|
---|
148 | return collection === null || collection === undefined;
|
---|
149 | }
|
---|
150 |
|
---|
151 | function get(collection, key, notSetValue) {
|
---|
152 | if (isInvalid(collection)) {
|
---|
153 | return notSetValue;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (isMapLike(collection)) {
|
---|
157 | return collection.has(key) ? collection.get(key) : notSetValue;
|
---|
158 | }
|
---|
159 |
|
---|
160 | return hasOwnProperty.call(collection, key) ? collection[key] : notSetValue;
|
---|
161 | }
|
---|
162 | function getIn(collection, keyPath, notSetValue) {
|
---|
163 | var i = 0;
|
---|
164 |
|
---|
165 | while (i !== keyPath.length) {
|
---|
166 | collection = get(collection, keyPath[i++], NOT_SET);
|
---|
167 |
|
---|
168 | if (collection === NOT_SET) {
|
---|
169 | return notSetValue;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | return collection;
|
---|
174 | }
|
---|
175 |
|
---|
176 | function check(config) {
|
---|
177 | var prev = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
---|
178 | var next = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
---|
179 | var checkItem = createChecker(prev, next);
|
---|
180 | var checklist = config || Object.keys(_objectSpread2({}, next, {}, prev));
|
---|
181 | return checklist.every(checkItem);
|
---|
182 | }
|
---|
183 |
|
---|
184 | function createChecker(prev, next) {
|
---|
185 | return function (name) {
|
---|
186 | if (typeof name === 'string') {
|
---|
187 | return immutable.is(next[name], prev[name]);
|
---|
188 | } else if (Array.isArray(name)) {
|
---|
189 | return immutable.is(getIn(next, name), getIn(prev, name));
|
---|
190 | }
|
---|
191 |
|
---|
192 | throw new TypeError('Invalid key: expected Array or string: ' + name);
|
---|
193 | };
|
---|
194 | }
|
---|
195 |
|
---|
196 | var ImmutablePureComponent =
|
---|
197 | /*#__PURE__*/
|
---|
198 | function (_React$Component) {
|
---|
199 | _inherits(ImmutablePureComponent, _React$Component);
|
---|
200 |
|
---|
201 | function ImmutablePureComponent() {
|
---|
202 | _classCallCheck(this, ImmutablePureComponent);
|
---|
203 |
|
---|
204 | return _possibleConstructorReturn(this, _getPrototypeOf(ImmutablePureComponent).apply(this, arguments));
|
---|
205 | }
|
---|
206 |
|
---|
207 | _createClass(ImmutablePureComponent, [{
|
---|
208 | key: "shouldComponentUpdate",
|
---|
209 | value: function shouldComponentUpdate(nextProps) {
|
---|
210 | var nextState = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
---|
211 | return !check(this.updateOnProps, this.props, nextProps, 'updateOnProps') || !check(this.updateOnStates, this.state, nextState, 'updateOnStates');
|
---|
212 | }
|
---|
213 | }]);
|
---|
214 |
|
---|
215 | return ImmutablePureComponent;
|
---|
216 | }(React.Component);
|
---|
217 |
|
---|
218 | function immutableMemo(Component, updateOnProps) {
|
---|
219 | return React.memo(Component, function (prev, next) {
|
---|
220 | return check(updateOnProps, prev, next, 'immutableMemo');
|
---|
221 | });
|
---|
222 | }
|
---|
223 |
|
---|
224 | exports.ImmutablePureComponent = ImmutablePureComponent;
|
---|
225 | exports.default = ImmutablePureComponent;
|
---|
226 | exports.immutableMemo = immutableMemo;
|
---|