mobx-react 7.5.3 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,52 +6,26 @@
6
6
 
7
7
  var React__default = 'default' in React ? React['default'] : React;
8
8
 
9
- var symbolId = 0;
10
-
11
- function createSymbol(name) {
12
- if (typeof Symbol === "function") {
13
- return Symbol(name);
14
- }
15
-
16
- var symbol = "__$mobx-react " + name + " (" + symbolId + ")";
17
- symbolId++;
18
- return symbol;
19
- }
20
-
21
- var createdSymbols = {};
22
- function newSymbol(name) {
23
- if (!createdSymbols[name]) {
24
- createdSymbols[name] = createSymbol(name);
25
- }
26
-
27
- return createdSymbols[name];
28
- }
29
9
  function shallowEqual(objA, objB) {
30
10
  //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
31
11
  if (is(objA, objB)) {
32
12
  return true;
33
13
  }
34
-
35
14
  if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
36
15
  return false;
37
16
  }
38
-
39
17
  var keysA = Object.keys(objA);
40
18
  var keysB = Object.keys(objB);
41
-
42
19
  if (keysA.length !== keysB.length) {
43
20
  return false;
44
21
  }
45
-
46
22
  for (var i = 0; i < keysA.length; i++) {
47
23
  if (!Object.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
48
24
  return false;
49
25
  }
50
26
  }
51
-
52
27
  return true;
53
28
  }
54
-
55
29
  function is(x, y) {
56
30
  // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
57
31
  if (x === y) {
@@ -59,9 +33,8 @@
59
33
  } else {
60
34
  return x !== x && y !== y;
61
35
  }
62
- } // based on https://github.com/mridgway/hoist-non-react-statics/blob/master/src/index.js
63
-
64
-
36
+ }
37
+ // based on https://github.com/mridgway/hoist-non-react-statics/blob/master/src/index.js
65
38
  var hoistBlackList = {
66
39
  $$typeof: 1,
67
40
  render: 1,
@@ -86,33 +59,12 @@
86
59
  }
87
60
  });
88
61
  }
89
- /**
90
- * Helper to set `prop` to `this` as non-enumerable (hidden prop)
91
- * @param target
92
- * @param prop
93
- * @param value
94
- */
95
-
96
- function setHiddenProp(target, prop, value) {
97
- if (!Object.hasOwnProperty.call(target, prop)) {
98
- Object.defineProperty(target, prop, {
99
- enumerable: false,
100
- configurable: true,
101
- writable: true,
102
- value: value
103
- });
104
- } else {
105
- target[prop] = value;
106
- }
107
- }
108
62
  /**
109
63
  * Utilities for patching componentWillUnmount, to make sure @disposeOnUnmount works correctly icm with user defined hooks
110
64
  * and the handler provided by mobx-react
111
65
  */
112
-
113
- var mobxMixins = /*#__PURE__*/newSymbol("patchMixins");
114
- var mobxPatchedDefinition = /*#__PURE__*/newSymbol("patchedDefinition");
115
-
66
+ var mobxMixins = /*#__PURE__*/Symbol("patchMixins");
67
+ var mobxPatchedDefinition = /*#__PURE__*/Symbol("patchedDefinition");
116
68
  function getMixins(target, methodName) {
117
69
  var mixins = target[mobxMixins] = target[mobxMixins] || {};
118
70
  var methodMixins = mixins[methodName] = mixins[methodName] || {};
@@ -120,28 +72,21 @@
120
72
  methodMixins.methods = methodMixins.methods || [];
121
73
  return methodMixins;
122
74
  }
123
-
124
75
  function wrapper(realMethod, mixins) {
125
76
  var _this = this;
126
-
127
77
  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
128
78
  args[_key - 2] = arguments[_key];
129
79
  }
130
-
131
80
  // locks are used to ensure that mixins are invoked only once per invocation, even on recursive calls
132
81
  mixins.locks++;
133
-
134
82
  try {
135
83
  var retVal;
136
-
137
84
  if (realMethod !== undefined && realMethod !== null) {
138
85
  retVal = realMethod.apply(this, args);
139
86
  }
140
-
141
87
  return retVal;
142
88
  } finally {
143
89
  mixins.locks--;
144
-
145
90
  if (mixins.locks === 0) {
146
91
  mixins.methods.forEach(function (mx) {
147
92
  mx.apply(_this, args);
@@ -149,41 +94,31 @@
149
94
  }
150
95
  }
151
96
  }
152
-
153
97
  function wrapFunction(realMethod, mixins) {
154
98
  var fn = function fn() {
155
99
  for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
156
100
  args[_key2] = arguments[_key2];
157
101
  }
158
-
159
102
  wrapper.call.apply(wrapper, [this, realMethod, mixins].concat(args));
160
103
  };
161
-
162
104
  return fn;
163
105
  }
164
-
165
106
  function patch(target, methodName, mixinMethod) {
166
107
  var mixins = getMixins(target, methodName);
167
-
168
108
  if (mixins.methods.indexOf(mixinMethod) < 0) {
169
109
  mixins.methods.push(mixinMethod);
170
110
  }
171
-
172
111
  var oldDefinition = Object.getOwnPropertyDescriptor(target, methodName);
173
-
174
112
  if (oldDefinition && oldDefinition[mobxPatchedDefinition]) {
175
113
  // already patched definition, do not repatch
176
114
  return;
177
115
  }
178
-
179
116
  var originalMethod = target[methodName];
180
117
  var newDefinition = createDefinition(target, methodName, oldDefinition ? oldDefinition.enumerable : undefined, mixins, originalMethod);
181
118
  Object.defineProperty(target, methodName, newDefinition);
182
119
  }
183
-
184
120
  function createDefinition(target, methodName, enumerable, mixins, originalMethod) {
185
121
  var _ref;
186
-
187
122
  var wrappedFunc = wrapFunction(originalMethod, mixins);
188
123
  return _ref = {}, _ref[mobxPatchedDefinition] = true, _ref.get = function get() {
189
124
  return wrappedFunc;
@@ -201,247 +136,231 @@
201
136
  }, _ref.configurable = true, _ref.enumerable = enumerable, _ref;
202
137
  }
203
138
 
204
- var mobxAdminProperty = mobx.$mobx || "$mobx"; // BC
205
-
206
- var mobxObserverProperty = /*#__PURE__*/newSymbol("isMobXReactObserver");
207
- var mobxIsUnmounted = /*#__PURE__*/newSymbol("isUnmounted");
208
- var skipRenderKey = /*#__PURE__*/newSymbol("skipRender");
209
- var isForcingUpdateKey = /*#__PURE__*/newSymbol("isForcingUpdate");
139
+ var administrationSymbol = /*#__PURE__*/Symbol("ObserverAdministration");
140
+ var isMobXReactObserverSymbol = /*#__PURE__*/Symbol("isMobXReactObserver");
141
+ function getAdministration(component) {
142
+ var _component$administra;
143
+ // We create administration lazily, because we can't patch constructor
144
+ // and the exact moment of initialization partially depends on React internals.
145
+ // At the time of writing this, the first thing invoked is one of the observable getter/setter (state/props/context).
146
+ return (_component$administra = component[administrationSymbol]) != null ? _component$administra : component[administrationSymbol] = {
147
+ reaction: null,
148
+ mounted: false,
149
+ forceUpdate: null,
150
+ name: getDisplayName(component.constructor),
151
+ state: undefined,
152
+ props: undefined,
153
+ context: undefined,
154
+ propsAtom: mobx.createAtom("props"),
155
+ stateAtom: mobx.createAtom("state"),
156
+ contextAtom: mobx.createAtom("context"),
157
+ isUpdating: false
158
+ };
159
+ }
210
160
  function makeClassComponentObserver(componentClass) {
211
- var target = componentClass.prototype;
212
-
213
- if (componentClass[mobxObserverProperty]) {
214
- var displayName = getDisplayName(target);
161
+ var prototype = componentClass.prototype;
162
+ if (componentClass[isMobXReactObserverSymbol]) {
163
+ var displayName = getDisplayName(componentClass);
215
164
  console.warn("The provided component class (" + displayName + ")\n has already been declared as an observer component.");
216
165
  } else {
217
- componentClass[mobxObserverProperty] = true;
166
+ componentClass[isMobXReactObserverSymbol] = true;
218
167
  }
219
-
220
- if (target.componentWillReact) {
168
+ if (prototype.componentWillReact) {
221
169
  throw new Error("The componentWillReact life-cycle event is no longer supported");
222
170
  }
223
-
224
171
  if (componentClass["__proto__"] !== React.PureComponent) {
225
- if (!target.shouldComponentUpdate) {
226
- target.shouldComponentUpdate = observerSCU;
227
- } else if (target.shouldComponentUpdate !== observerSCU) {
172
+ if (!prototype.shouldComponentUpdate) {
173
+ prototype.shouldComponentUpdate = observerSCU;
174
+ } else if (prototype.shouldComponentUpdate !== observerSCU) {
228
175
  // n.b. unequal check, instead of existence check, as @observer might be on superclass as well
229
176
  throw new Error("It is not allowed to use shouldComponentUpdate in observer based components.");
230
177
  }
231
- } // this.props and this.state are made observable, just to make sure @computed fields that
178
+ }
179
+ // this.props and this.state are made observable, just to make sure @computed fields that
232
180
  // are defined inside the component, and which rely on state or props, re-compute if state or props change
233
181
  // (otherwise the computed wouldn't update and become stale on props change, since props are not observable)
234
182
  // However, this solution is not without it's own problems: https://github.com/mobxjs/mobx-react/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3Aobservable-props-or-not+
235
-
236
-
237
- makeObservableProp(target, "props");
238
- makeObservableProp(target, "state");
239
-
240
- if (componentClass.contextType) {
241
- makeObservableProp(target, "context");
242
- }
243
-
244
- var originalRender = target.render;
245
-
183
+ Object.defineProperties(prototype, {
184
+ props: observablePropsDescriptor,
185
+ state: observableStateDescriptor,
186
+ context: observableContextDescriptor
187
+ });
188
+ var originalRender = prototype.render;
246
189
  if (typeof originalRender !== "function") {
247
- var _displayName = getDisplayName(target);
248
-
190
+ var _displayName = getDisplayName(componentClass);
249
191
  throw new Error("[mobx-react] class component (" + _displayName + ") is missing `render` method." + "\n`observer` requires `render` being a function defined on prototype." + "\n`render = () => {}` or `render = function() {}` is not supported.");
250
192
  }
251
-
252
- target.render = function () {
253
- this.render = mobxReactLite.isUsingStaticRendering() ? originalRender : createReactiveRender.call(this, originalRender);
193
+ prototype.render = function () {
194
+ Object.defineProperty(this, "render", {
195
+ // There is no safe way to replace render, therefore it's forbidden.
196
+ configurable: false,
197
+ writable: false,
198
+ value: mobxReactLite.isUsingStaticRendering() ? originalRender : createReactiveRender.call(this, originalRender)
199
+ });
254
200
  return this.render();
255
201
  };
256
-
257
- patch(target, "componentDidMount", function () {
258
- this[mobxIsUnmounted] = false;
259
-
260
- if (!this.render[mobxAdminProperty]) {
261
- // Reaction is re-created automatically during render, but a component can re-mount and skip render #3395.
262
- // To re-create the reaction and re-subscribe to relevant observables we have to force an update.
263
- React.Component.prototype.forceUpdate.call(this);
202
+ patch(prototype, "componentDidMount", function () {
203
+ var _this = this;
204
+ // `componentDidMount` may not be called at all. React can abandon the instance after `render`.
205
+ // That's why we use finalization registry to dispose reaction created during render.
206
+ // Happens with `<Suspend>` see #3492
207
+ //
208
+ // `componentDidMount` can be called immediately after `componentWillUnmount` without calling `render` in between.
209
+ // Happens with `<StrictMode>`see #3395.
210
+ //
211
+ // If `componentDidMount` is called, it's guaranteed to run synchronously with render (similary to `useLayoutEffect`).
212
+ // Therefore we don't have to worry about external (observable) state being updated before mount (no state version checking).
213
+ //
214
+ // Things may change: "In the future, React will provide a feature that lets components preserve state between unmounts"
215
+ var admin = getAdministration(this);
216
+ admin.mounted = true;
217
+ // Component instance committed, prevent reaction disposal.
218
+ mobxReactLite._observerFinalizationRegistry.unregister(admin);
219
+ // We don't set forceUpdate before mount because it requires a reference to `this`,
220
+ // therefore `this` could NOT be garbage collected before mount,
221
+ // preventing reaction disposal by FinalizationRegistry and leading to memory leak.
222
+ // As an alternative we could have `admin.instanceRef = new WeakRef(this)`, but lets avoid it if possible.
223
+ admin.forceUpdate = function () {
224
+ return _this.forceUpdate();
225
+ };
226
+ if (!admin.reaction) {
227
+ // 1. Instance was unmounted (reaction disposed) and immediately remounted without running render #3395.
228
+ // 2. Reaction was disposed by finalization registry before mount. Shouldn't ever happen for class components:
229
+ // `componentDidMount` runs synchronously after render, but our registry are deferred (can't run in between).
230
+ // In any case we lost subscriptions to observables, so we have to create new reaction and re-render to resubscribe.
231
+ // The reaction will be created lazily by following render.
232
+ admin.forceUpdate();
264
233
  }
265
234
  });
266
- patch(target, "componentWillUnmount", function () {
235
+ patch(prototype, "componentWillUnmount", function () {
236
+ var _admin$reaction;
267
237
  if (mobxReactLite.isUsingStaticRendering()) {
268
238
  return;
269
239
  }
270
-
271
- var reaction = this.render[mobxAdminProperty];
272
-
273
- if (reaction) {
274
- reaction.dispose(); // Forces reaction to be re-created on next render
275
-
276
- this.render[mobxAdminProperty] = null;
277
- } else {
278
- // Render may have been hot-swapped and/or overridden by a subclass.
279
- var _displayName2 = getDisplayName(this);
280
-
281
- console.warn("The reactive render of an observer class component (" + _displayName2 + ")\n was overridden after MobX attached. This may result in a memory leak if the\n overridden reactive render was not properly disposed.");
282
- }
283
-
284
- this[mobxIsUnmounted] = true;
240
+ var admin = getAdministration(this);
241
+ (_admin$reaction = admin.reaction) == null ? void 0 : _admin$reaction.dispose();
242
+ admin.reaction = null;
243
+ admin.forceUpdate = null;
244
+ admin.mounted = false;
285
245
  });
286
246
  return componentClass;
287
- } // Generates a friendly name for debugging
288
-
289
- function getDisplayName(comp) {
290
- return comp.displayName || comp.name || comp.constructor && (comp.constructor.displayName || comp.constructor.name) || "<component>";
291
247
  }
292
-
248
+ // Generates a friendly name for debugging
249
+ function getDisplayName(componentClass) {
250
+ return componentClass.displayName || componentClass.name || "<component>";
251
+ }
293
252
  function createReactiveRender(originalRender) {
294
- var _this = this;
295
-
296
- /**
297
- * If props are shallowly modified, react will render anyway,
298
- * so atom.reportChanged() should not result in yet another re-render
299
- */
300
- setHiddenProp(this, skipRenderKey, false);
301
- /**
302
- * forceUpdate will re-assign this.props. We don't want that to cause a loop,
303
- * so detect these changes
304
- */
305
-
306
- setHiddenProp(this, isForcingUpdateKey, false);
307
- var initialName = getDisplayName(this);
308
253
  var boundOriginalRender = originalRender.bind(this);
309
- var isRenderingPending = false;
310
-
311
- var createReaction = function createReaction() {
312
- var reaction = new mobx.Reaction(initialName + ".render()", function () {
313
- if (!isRenderingPending) {
314
- // N.B. Getting here *before mounting* means that a component constructor has side effects (see the relevant test in misc.test.tsx)
315
- // This unidiomatic React usage but React will correctly warn about this so we continue as usual
316
- // See #85 / Pull #44
317
- isRenderingPending = true;
318
-
319
- if (_this[mobxIsUnmounted] !== true) {
320
- var hasError = true;
321
-
322
- try {
323
- setHiddenProp(_this, isForcingUpdateKey, true);
324
-
325
- if (!_this[skipRenderKey]) {
326
- React.Component.prototype.forceUpdate.call(_this);
327
- }
328
-
329
- hasError = false;
330
- } finally {
331
- setHiddenProp(_this, isForcingUpdateKey, false);
332
-
333
- if (hasError) {
334
- reaction.dispose(); // Forces reaction to be re-created on next render
335
-
336
- _this.render[mobxAdminProperty] = null;
337
- }
338
- }
339
- }
340
- }
341
- });
342
- reaction["reactComponent"] = _this;
343
- return reaction;
344
- };
345
-
254
+ var admin = getAdministration(this);
346
255
  function reactiveRender() {
347
- var _reactiveRender$mobxA;
348
-
349
- isRenderingPending = false; // Create reaction lazily to support re-mounting #3395
350
-
351
- var reaction = (_reactiveRender$mobxA = reactiveRender[mobxAdminProperty]) != null ? _reactiveRender$mobxA : reactiveRender[mobxAdminProperty] = createReaction();
352
- var exception = undefined;
353
- var rendering = undefined;
354
- reaction.track(function () {
256
+ if (!admin.reaction) {
257
+ // Create reaction lazily to support re-mounting #3395
258
+ admin.reaction = createReaction(admin);
259
+ if (!admin.mounted) {
260
+ // React can abandon this instance and never call `componentDidMount`/`componentWillUnmount`,
261
+ // we have to make sure reaction will be disposed.
262
+ mobxReactLite._observerFinalizationRegistry.register(this, admin, this);
263
+ }
264
+ }
265
+ var error = undefined;
266
+ var renderResult = undefined;
267
+ admin.reaction.track(function () {
355
268
  try {
356
269
  // TODO@major
357
270
  // Optimization: replace with _allowStateChangesStart/End (not available in mobx@6.0.0)
358
- rendering = mobx._allowStateChanges(false, boundOriginalRender);
271
+ renderResult = mobx._allowStateChanges(false, boundOriginalRender);
359
272
  } catch (e) {
360
- exception = e;
273
+ error = e;
361
274
  }
362
275
  });
363
-
364
- if (exception) {
365
- throw exception;
276
+ if (error) {
277
+ throw error;
366
278
  }
367
-
368
- return rendering;
279
+ return renderResult;
369
280
  }
370
-
371
281
  return reactiveRender;
372
282
  }
373
-
283
+ function createReaction(admin) {
284
+ return new mobx.Reaction(admin.name + ".render()", function () {
285
+ if (admin.isUpdating) {
286
+ // Reaction is suppressed when setting new state/props/context,
287
+ // this is when component is already being updated.
288
+ return;
289
+ }
290
+ if (!admin.mounted) {
291
+ // This is neccessary to avoid react warning about calling forceUpdate on component that isn't mounted yet.
292
+ // This happens when component is abandoned after render - our reaction is already created and reacts to changes.
293
+ // Due to the synchronous nature of `componenDidMount`, we don't have to worry that component could eventually mount and require update.
294
+ return;
295
+ }
296
+ try {
297
+ // forceUpdate sets new `props`, since we made it observable, it would `reportChanged`, causing a loop.
298
+ admin.isUpdating = true;
299
+ admin.forceUpdate == null ? void 0 : admin.forceUpdate();
300
+ } catch (error) {
301
+ var _admin$reaction2;
302
+ (_admin$reaction2 = admin.reaction) == null ? void 0 : _admin$reaction2.dispose();
303
+ admin.reaction = null;
304
+ } finally {
305
+ admin.isUpdating = false;
306
+ }
307
+ });
308
+ }
374
309
  function observerSCU(nextProps, nextState) {
375
310
  if (mobxReactLite.isUsingStaticRendering()) {
376
311
  console.warn("[mobx-react] It seems that a re-rendering of a React component is triggered while in static (server-side) mode. Please make sure components are rendered only once server-side.");
377
- } // update on any state changes (as is the default)
378
-
379
-
312
+ }
313
+ // update on any state changes (as is the default)
380
314
  if (this.state !== nextState) {
381
315
  return true;
382
- } // update if props are shallowly not equal, inspired by PureRenderMixin
316
+ }
317
+ // update if props are shallowly not equal, inspired by PureRenderMixin
383
318
  // we could return just 'false' here, and avoid the `skipRender` checks etc
384
319
  // however, it is nicer if lifecycle events are triggered like usually,
385
320
  // so we return true here if props are shallowly modified.
386
-
387
-
388
321
  return !shallowEqual(this.props, nextProps);
389
322
  }
390
-
391
- function makeObservableProp(target, propName) {
392
- var valueHolderKey = newSymbol("reactProp_" + propName + "_valueHolder");
393
- var atomHolderKey = newSymbol("reactProp_" + propName + "_atomHolder");
394
-
395
- function getAtom() {
396
- if (!this[atomHolderKey]) {
397
- setHiddenProp(this, atomHolderKey, mobx.createAtom("reactive " + propName));
398
- }
399
-
400
- return this[atomHolderKey];
401
- }
402
-
403
- Object.defineProperty(target, propName, {
323
+ function createObservablePropDescriptor(key) {
324
+ var atomKey = key + "Atom";
325
+ return {
404
326
  configurable: true,
405
327
  enumerable: true,
406
328
  get: function get() {
407
- var prevReadState = false; // Why this check? BC?
408
- // @ts-expect-error
409
-
410
- if (mobx._allowStateReadsStart && mobx._allowStateReadsEnd) {
411
- prevReadState = mobx._allowStateReadsStart(true);
412
- }
413
-
414
- getAtom.call(this).reportObserved(); // Why this check? BC?
415
- // @ts-expect-error
416
-
417
- if (mobx._allowStateReadsStart && mobx._allowStateReadsEnd) {
418
- mobx._allowStateReadsEnd(prevReadState);
419
- }
420
-
421
- return this[valueHolderKey];
329
+ var admin = getAdministration(this);
330
+ var prevReadState = mobx._allowStateReadsStart(true);
331
+ admin[atomKey].reportObserved();
332
+ mobx._allowStateReadsEnd(prevReadState);
333
+ return admin[key];
422
334
  },
423
- set: function set(v) {
424
- if (!this[isForcingUpdateKey] && !shallowEqual(this[valueHolderKey], v)) {
425
- setHiddenProp(this, valueHolderKey, v);
426
- setHiddenProp(this, skipRenderKey, true);
427
- getAtom.call(this).reportChanged();
428
- setHiddenProp(this, skipRenderKey, false);
335
+ set: function set(value) {
336
+ var admin = getAdministration(this);
337
+ // forceUpdate issued by reaction sets new props.
338
+ // It sets isUpdating to true to prevent loop.
339
+ if (!admin.isUpdating && !shallowEqual(admin[key], value)) {
340
+ admin[key] = value;
341
+ // This notifies all observers including our component,
342
+ // but we don't want to cause `forceUpdate`, because component is already updating,
343
+ // therefore supress component reaction.
344
+ admin.isUpdating = true;
345
+ admin[atomKey].reportChanged();
346
+ admin.isUpdating = false;
429
347
  } else {
430
- setHiddenProp(this, valueHolderKey, v);
348
+ admin[key] = value;
431
349
  }
432
350
  }
433
- });
351
+ };
434
352
  }
353
+ var observablePropsDescriptor = /*#__PURE__*/createObservablePropDescriptor("props");
354
+ var observableStateDescriptor = /*#__PURE__*/createObservablePropDescriptor("state");
355
+ var observableContextDescriptor = /*#__PURE__*/createObservablePropDescriptor("context");
435
356
 
436
357
  /**
437
358
  * Observer function / decorator
438
359
  */
439
-
440
360
  function observer(component) {
441
361
  if (component["isMobxInjector"] === true) {
442
362
  console.warn("Mobx observer: You are trying to use `observer` on a component that already has `inject`. Please apply `observer` before applying `inject`");
443
363
  }
444
-
445
364
  if (Object.prototype.isPrototypeOf.call(React.Component, component) || Object.prototype.isPrototypeOf.call(React.PureComponent, component)) {
446
365
  // Class component
447
366
  return makeClassComponentObserver(component);
@@ -452,35 +371,29 @@
452
371
  }
453
372
 
454
373
  function _extends() {
455
- _extends = Object.assign || function (target) {
374
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
456
375
  for (var i = 1; i < arguments.length; i++) {
457
376
  var source = arguments[i];
458
-
459
377
  for (var key in source) {
460
378
  if (Object.prototype.hasOwnProperty.call(source, key)) {
461
379
  target[key] = source[key];
462
380
  }
463
381
  }
464
382
  }
465
-
466
383
  return target;
467
384
  };
468
-
469
385
  return _extends.apply(this, arguments);
470
386
  }
471
-
472
387
  function _objectWithoutPropertiesLoose(source, excluded) {
473
388
  if (source == null) return {};
474
389
  var target = {};
475
390
  var sourceKeys = Object.keys(source);
476
391
  var key, i;
477
-
478
392
  for (i = 0; i < sourceKeys.length; i++) {
479
393
  key = sourceKeys[i];
480
394
  if (excluded.indexOf(key) >= 0) continue;
481
395
  target[key] = source[key];
482
396
  }
483
-
484
397
  return target;
485
398
  }
486
399
 
@@ -488,21 +401,16 @@
488
401
  var MobXProviderContext = /*#__PURE__*/React__default.createContext({});
489
402
  function Provider(props) {
490
403
  var children = props.children,
491
- stores = _objectWithoutPropertiesLoose(props, _excluded);
492
-
404
+ stores = _objectWithoutPropertiesLoose(props, _excluded);
493
405
  var parentValue = React__default.useContext(MobXProviderContext);
494
406
  var mutableProviderRef = React__default.useRef(_extends({}, parentValue, stores));
495
407
  var value = mutableProviderRef.current;
496
-
497
408
  {
498
409
  var newValue = _extends({}, value, stores); // spread in previous state for the context based stores
499
-
500
-
501
410
  if (!shallowEqual(value, newValue)) {
502
411
  throw new Error("MobX Provider: The set of provided stores has changed. See: https://github.com/mobxjs/mobx-react#the-set-of-provided-stores-has-changed-error.");
503
412
  }
504
413
  }
505
-
506
414
  return React__default.createElement(MobXProviderContext.Provider, {
507
415
  value: value
508
416
  }, children);
@@ -512,38 +420,31 @@
512
420
  /**
513
421
  * Store Injection
514
422
  */
515
-
516
423
  function createStoreInjector(grabStoresFn, component, injectNames, makeReactive) {
517
424
  // Support forward refs
518
425
  var Injector = React__default.forwardRef(function (props, ref) {
519
426
  var newProps = _extends({}, props);
520
-
521
427
  var context = React__default.useContext(MobXProviderContext);
522
428
  Object.assign(newProps, grabStoresFn(context || {}, newProps) || {});
523
-
524
429
  if (ref) {
525
430
  newProps.ref = ref;
526
431
  }
527
-
528
432
  return React__default.createElement(component, newProps);
529
433
  });
530
434
  if (makeReactive) Injector = observer(Injector);
531
435
  Injector["isMobxInjector"] = true; // assigned late to suppress observer warning
532
436
  // Static fields from component should be visible on the generated Injector
533
-
534
437
  copyStaticProperties(component, Injector);
535
438
  Injector["wrappedComponent"] = component;
536
439
  Injector.displayName = getInjectName(component, injectNames);
537
440
  return Injector;
538
441
  }
539
-
540
442
  function getInjectName(component, injectNames) {
541
443
  var displayName;
542
444
  var componentName = component.displayName || component.name || component.constructor && component.constructor.name || "Component";
543
445
  if (injectNames) displayName = "inject-with-" + injectNames + "(" + componentName + ")";else displayName = "inject(" + componentName + ")";
544
446
  return displayName;
545
447
  }
546
-
547
448
  function grabStoresByName(storeNames) {
548
449
  return function (baseStores, nextProps) {
549
450
  storeNames.forEach(function (storeName) {
@@ -561,13 +462,10 @@
561
462
  * or a function that manually maps the available stores from the context to props:
562
463
  * storesToProps(mobxStores, props, context) => newProps
563
464
  */
564
-
565
-
566
465
  function inject() {
567
466
  for (var _len = arguments.length, storeNames = new Array(_len), _key = 0; _key < _len; _key++) {
568
467
  storeNames[_key] = arguments[_key];
569
468
  }
570
-
571
469
  if (typeof arguments[0] === "function") {
572
470
  var grabStoresFn = arguments[0];
573
471
  return function (componentClass) {
@@ -580,14 +478,14 @@
580
478
  }
581
479
  }
582
480
 
583
- var protoStoreKey = /*#__PURE__*/newSymbol("disposeOnUnmountProto");
584
- var instStoreKey = /*#__PURE__*/newSymbol("disposeOnUnmountInst");
585
-
481
+ var reactMajorVersion = /*#__PURE__*/Number.parseInt( /*#__PURE__*/React__default.version.split(".")[0]);
482
+ var warnedAboutDisposeOnUnmountDeprecated = false;
483
+ var protoStoreKey = /*#__PURE__*/Symbol("disposeOnUnmountProto");
484
+ var instStoreKey = /*#__PURE__*/Symbol("disposeOnUnmountInst");
586
485
  function runDisposersOnWillUnmount() {
587
486
  var _this = this;
588
487
  [].concat(this[protoStoreKey] || [], this[instStoreKey] || []).forEach(function (propKeyOrFunction) {
589
488
  var prop = typeof propKeyOrFunction === "string" ? _this[propKeyOrFunction] : propKeyOrFunction;
590
-
591
489
  if (prop !== undefined && prop !== null) {
592
490
  if (Array.isArray(prop)) prop.map(function (f) {
593
491
  return f();
@@ -595,62 +493,67 @@
595
493
  }
596
494
  });
597
495
  }
598
-
496
+ /**
497
+ * @deprecated `disposeOnUnmount` is not compatible with React 18 and higher.
498
+ */
599
499
  function disposeOnUnmount(target, propertyKeyOrFunction) {
600
500
  if (Array.isArray(propertyKeyOrFunction)) {
601
501
  return propertyKeyOrFunction.map(function (fn) {
602
502
  return disposeOnUnmount(target, fn);
603
503
  });
604
504
  }
605
-
505
+ if (!warnedAboutDisposeOnUnmountDeprecated) {
506
+ if (reactMajorVersion >= 18) {
507
+ console.error("[mobx-react] disposeOnUnmount is not compatible with React 18 and higher. Don't use it.");
508
+ } else {
509
+ console.warn("[mobx-react] disposeOnUnmount is deprecated. It won't work correctly with React 18 and higher.");
510
+ }
511
+ warnedAboutDisposeOnUnmountDeprecated = true;
512
+ }
606
513
  var c = Object.getPrototypeOf(target).constructor;
607
- var c2 = Object.getPrototypeOf(target.constructor); // Special case for react-hot-loader
608
-
514
+ var c2 = Object.getPrototypeOf(target.constructor);
515
+ // Special case for react-hot-loader
609
516
  var c3 = Object.getPrototypeOf(Object.getPrototypeOf(target));
610
-
611
517
  if (!(c === React__default.Component || c === React__default.PureComponent || c2 === React__default.Component || c2 === React__default.PureComponent || c3 === React__default.Component || c3 === React__default.PureComponent)) {
612
518
  throw new Error("[mobx-react] disposeOnUnmount only supports direct subclasses of React.Component or React.PureComponent.");
613
519
  }
614
-
615
520
  if (typeof propertyKeyOrFunction !== "string" && typeof propertyKeyOrFunction !== "function" && !Array.isArray(propertyKeyOrFunction)) {
616
521
  throw new Error("[mobx-react] disposeOnUnmount only works if the parameter is either a property key or a function.");
617
- } // decorator's target is the prototype, so it doesn't have any instance properties like props
618
-
619
-
620
- var isDecorator = typeof propertyKeyOrFunction === "string"; // add property key / function we want run (disposed) to the store
621
-
522
+ }
523
+ // decorator's target is the prototype, so it doesn't have any instance properties like props
524
+ var isDecorator = typeof propertyKeyOrFunction === "string";
525
+ // add property key / function we want run (disposed) to the store
622
526
  var componentWasAlreadyModified = !!target[protoStoreKey] || !!target[instStoreKey];
623
- var store = isDecorator ? // decorators are added to the prototype store
624
- target[protoStoreKey] || (target[protoStoreKey] = []) : // functions are added to the instance store
527
+ var store = isDecorator ?
528
+ // decorators are added to the prototype store
529
+ target[protoStoreKey] || (target[protoStoreKey] = []) :
530
+ // functions are added to the instance store
625
531
  target[instStoreKey] || (target[instStoreKey] = []);
626
- store.push(propertyKeyOrFunction); // tweak the component class componentWillUnmount if not done already
627
-
532
+ store.push(propertyKeyOrFunction);
533
+ // tweak the component class componentWillUnmount if not done already
628
534
  if (!componentWasAlreadyModified) {
629
535
  patch(target, "componentWillUnmount", runDisposersOnWillUnmount);
630
- } // return the disposer as is if invoked as a non decorator
631
-
632
-
536
+ }
537
+ // return the disposer as is if invoked as a non decorator
633
538
  if (typeof propertyKeyOrFunction !== "string") {
634
539
  return propertyKeyOrFunction;
635
540
  }
636
541
  }
637
542
 
543
+ // Copied from React.PropTypes
638
544
  function createChainableTypeChecker(validator) {
639
545
  function checkType(isRequired, props, propName, componentName, location, propFullName) {
640
546
  for (var _len = arguments.length, rest = new Array(_len > 6 ? _len - 6 : 0), _key = 6; _key < _len; _key++) {
641
547
  rest[_key - 6] = arguments[_key];
642
548
  }
643
-
644
549
  return mobx.untracked(function () {
645
550
  componentName = componentName || "<<anonymous>>";
646
551
  propFullName = propFullName || propName;
647
-
648
552
  if (props[propName] == null) {
649
553
  if (isRequired) {
650
554
  var actual = props[propName] === null ? "null" : "undefined";
651
555
  return new Error("The " + location + " `" + propFullName + "` is marked as required " + "in `" + componentName + "`, but its value is `" + actual + "`.");
652
556
  }
653
-
654
557
  return null;
655
558
  } else {
656
559
  // @ts-ignore rest arg is necessary for some React internals - fails tests otherwise
@@ -658,60 +561,48 @@
658
561
  }
659
562
  });
660
563
  }
661
-
662
- var chainedCheckType = checkType.bind(null, false); // Add isRequired to satisfy Requirable
663
-
564
+ var chainedCheckType = checkType.bind(null, false);
565
+ // Add isRequired to satisfy Requirable
664
566
  chainedCheckType.isRequired = checkType.bind(null, true);
665
567
  return chainedCheckType;
666
- } // Copied from React.PropTypes
667
-
668
-
568
+ }
569
+ // Copied from React.PropTypes
669
570
  function isSymbol(propType, propValue) {
670
571
  // Native Symbol.
671
572
  if (propType === "symbol") {
672
573
  return true;
673
- } // 19.4.3.5 Symbol.prototype[@@toStringTag] === 'Symbol'
674
-
675
-
574
+ }
575
+ // 19.4.3.5 Symbol.prototype[@@toStringTag] === 'Symbol'
676
576
  if (propValue["@@toStringTag"] === "Symbol") {
677
577
  return true;
678
- } // Fallback for non-spec compliant Symbols which are polyfilled.
679
-
680
-
578
+ }
579
+ // Fallback for non-spec compliant Symbols which are polyfilled.
681
580
  if (typeof Symbol === "function" && propValue instanceof Symbol) {
682
581
  return true;
683
582
  }
684
-
685
583
  return false;
686
- } // Copied from React.PropTypes
687
-
688
-
584
+ }
585
+ // Copied from React.PropTypes
689
586
  function getPropType(propValue) {
690
587
  var propType = typeof propValue;
691
-
692
588
  if (Array.isArray(propValue)) {
693
589
  return "array";
694
590
  }
695
-
696
591
  if (propValue instanceof RegExp) {
697
592
  // Old webkits (at least until Android 4.0) return 'function' rather than
698
593
  // 'object' for typeof a RegExp. We'll normalize this here so that /bla/
699
594
  // passes PropTypes.object.
700
595
  return "object";
701
596
  }
702
-
703
597
  if (isSymbol(propType, propValue)) {
704
598
  return "symbol";
705
599
  }
706
-
707
600
  return propType;
708
- } // This handles more types than `getPropType`. Only used for error messages.
601
+ }
602
+ // This handles more types than `getPropType`. Only used for error messages.
709
603
  // Copied from React.PropTypes
710
-
711
-
712
604
  function getPreciseType(propValue) {
713
605
  var propType = getPropType(propValue);
714
-
715
606
  if (propType === "object") {
716
607
  if (propValue instanceof Date) {
717
608
  return "date";
@@ -719,55 +610,43 @@
719
610
  return "regexp";
720
611
  }
721
612
  }
722
-
723
613
  return propType;
724
614
  }
725
-
726
615
  function createObservableTypeCheckerCreator(allowNativeType, mobxType) {
727
616
  return createChainableTypeChecker(function (props, propName, componentName, location, propFullName) {
728
617
  return mobx.untracked(function () {
729
618
  if (allowNativeType) {
730
619
  if (getPropType(props[propName]) === mobxType.toLowerCase()) return null;
731
620
  }
732
-
733
621
  var mobxChecker;
734
-
735
622
  switch (mobxType) {
736
623
  case "Array":
737
624
  mobxChecker = mobx.isObservableArray;
738
625
  break;
739
-
740
626
  case "Object":
741
627
  mobxChecker = mobx.isObservableObject;
742
628
  break;
743
-
744
629
  case "Map":
745
630
  mobxChecker = mobx.isObservableMap;
746
631
  break;
747
-
748
632
  default:
749
633
  throw new Error("Unexpected mobxType: " + mobxType);
750
634
  }
751
-
752
635
  var propValue = props[propName];
753
-
754
636
  if (!mobxChecker(propValue)) {
755
637
  var preciseType = getPreciseType(propValue);
756
638
  var nativeTypeExpectationMessage = allowNativeType ? " or javascript `" + mobxType.toLowerCase() + "`" : "";
757
639
  return new Error("Invalid prop `" + propFullName + "` of type `" + preciseType + "` supplied to" + " `" + componentName + "`, expected `mobx.Observable" + mobxType + "`" + nativeTypeExpectationMessage + ".");
758
640
  }
759
-
760
641
  return null;
761
642
  });
762
643
  });
763
644
  }
764
-
765
645
  function createObservableArrayOfTypeChecker(allowNativeType, typeChecker) {
766
646
  return createChainableTypeChecker(function (props, propName, componentName, location, propFullName) {
767
647
  for (var _len2 = arguments.length, rest = new Array(_len2 > 5 ? _len2 - 5 : 0), _key2 = 5; _key2 < _len2; _key2++) {
768
648
  rest[_key2 - 5] = arguments[_key2];
769
649
  }
770
-
771
650
  return mobx.untracked(function () {
772
651
  if (typeof typeChecker !== "function") {
773
652
  return new Error("Property `" + propFullName + "` of component `" + componentName + "` has " + "invalid PropType notation.");
@@ -775,18 +654,15 @@
775
654
  var error = createObservableTypeCheckerCreator(allowNativeType, "Array")(props, propName, componentName, location, propFullName);
776
655
  if (error instanceof Error) return error;
777
656
  var propValue = props[propName];
778
-
779
657
  for (var i = 0; i < propValue.length; i++) {
780
658
  error = typeChecker.apply(void 0, [propValue, i, componentName, location, propFullName + "[" + i + "]"].concat(rest));
781
659
  if (error instanceof Error) return error;
782
660
  }
783
-
784
661
  return null;
785
662
  }
786
663
  });
787
664
  });
788
665
  }
789
-
790
666
  var observableArray = /*#__PURE__*/createObservableTypeCheckerCreator(false, "Array");
791
667
  var observableArrayOf = /*#__PURE__*/createObservableArrayOfTypeChecker.bind(null, false);
792
668
  var observableMap = /*#__PURE__*/createObservableTypeCheckerCreator(false, "Map");
@@ -804,8 +680,12 @@
804
680
  objectOrObservableObject: objectOrObservableObject
805
681
  };
806
682
 
807
- if (!React.Component) throw new Error("mobx-react requires React to be available");
808
- if (!mobx.observable) throw new Error("mobx-react requires mobx to be available");
683
+ if (!React.Component) {
684
+ throw new Error("mobx-react requires React to be available");
685
+ }
686
+ if (!mobx.observable) {
687
+ throw new Error("mobx-react requires mobx to be available");
688
+ }
809
689
 
810
690
  Object.defineProperty(exports, 'Observer', {
811
691
  enumerable: true,