1 | import { Reaction, _allowStateChanges, _allowStateReadsStart, _allowStateReadsEnd, $mobx, createAtom, untracked, isObservableMap, isObservableObject, isObservableArray, observable, configure } from 'mobx';
|
2 | import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
|
3 | import { unstable_batchedUpdates } from 'react-native';
|
4 | import { isUsingStaticRendering, Observer, observer as observer$1 } from 'mobx-react-lite';
|
5 | export { Observer, isUsingStaticRendering, useAsObservableSource, useLocalStore, useObserver, useStaticRendering } from 'mobx-react-lite';
|
6 |
|
7 | let symbolId = 0;
|
8 |
|
9 | function createSymbol(name) {
|
10 | if (typeof Symbol === "function") {
|
11 | return Symbol(name);
|
12 | }
|
13 |
|
14 | const symbol = `__$mobx-react ${name} (${symbolId})`;
|
15 | symbolId++;
|
16 | return symbol;
|
17 | }
|
18 |
|
19 | const createdSymbols = {};
|
20 | function newSymbol(name) {
|
21 | if (!createdSymbols[name]) {
|
22 | createdSymbols[name] = createSymbol(name);
|
23 | }
|
24 |
|
25 | return createdSymbols[name];
|
26 | }
|
27 | function shallowEqual(objA, objB) {
|
28 |
|
29 | if (is(objA, objB)) return true;
|
30 |
|
31 | if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
|
32 | return false;
|
33 | }
|
34 |
|
35 | const keysA = Object.keys(objA);
|
36 | const keysB = Object.keys(objB);
|
37 | if (keysA.length !== keysB.length) return false;
|
38 |
|
39 | for (let i = 0; i < keysA.length; i++) {
|
40 | if (!Object.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
|
41 | return false;
|
42 | }
|
43 | }
|
44 |
|
45 | return true;
|
46 | }
|
47 |
|
48 | function is(x, y) {
|
49 |
|
50 | if (x === y) {
|
51 | return x !== 0 || 1 / x === 1 / y;
|
52 | } else {
|
53 | return x !== x && y !== y;
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 | const hoistBlackList = {
|
59 | $$typeof: 1,
|
60 | render: 1,
|
61 | compare: 1,
|
62 | type: 1,
|
63 | childContextTypes: 1,
|
64 | contextType: 1,
|
65 | contextTypes: 1,
|
66 | defaultProps: 1,
|
67 | getDefaultProps: 1,
|
68 | getDerivedStateFromError: 1,
|
69 | getDerivedStateFromProps: 1,
|
70 | mixins: 1,
|
71 | propTypes: 1
|
72 | };
|
73 | function copyStaticProperties(base, target) {
|
74 | const protoProps = Object.getOwnPropertyNames(Object.getPrototypeOf(base));
|
75 | Object.getOwnPropertyNames(base).forEach(key => {
|
76 | if (!hoistBlackList[key] && protoProps.indexOf(key) === -1) {
|
77 | Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(base, key));
|
78 | }
|
79 | });
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function setHiddenProp(target, prop, value) {
|
89 | if (!Object.hasOwnProperty.call(target, prop)) {
|
90 | Object.defineProperty(target, prop, {
|
91 | enumerable: false,
|
92 | configurable: true,
|
93 | writable: true,
|
94 | value
|
95 | });
|
96 | } else {
|
97 | target[prop] = value;
|
98 | }
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | const mobxMixins =
|
106 |
|
107 | newSymbol("patchMixins");
|
108 | const mobxPatchedDefinition =
|
109 |
|
110 | newSymbol("patchedDefinition");
|
111 |
|
112 | function getMixins(target, methodName) {
|
113 | const mixins = target[mobxMixins] = target[mobxMixins] || {};
|
114 | const methodMixins = mixins[methodName] = mixins[methodName] || {};
|
115 | methodMixins.locks = methodMixins.locks || 0;
|
116 | methodMixins.methods = methodMixins.methods || [];
|
117 | return methodMixins;
|
118 | }
|
119 |
|
120 | function wrapper(realMethod, mixins, ...args) {
|
121 |
|
122 | mixins.locks++;
|
123 |
|
124 | try {
|
125 | let retVal;
|
126 |
|
127 | if (realMethod !== undefined && realMethod !== null) {
|
128 | retVal = realMethod.apply(this, args);
|
129 | }
|
130 |
|
131 | return retVal;
|
132 | } finally {
|
133 | mixins.locks--;
|
134 |
|
135 | if (mixins.locks === 0) {
|
136 | mixins.methods.forEach(mx => {
|
137 | mx.apply(this, args);
|
138 | });
|
139 | }
|
140 | }
|
141 | }
|
142 |
|
143 | function wrapFunction(realMethod, mixins) {
|
144 | const fn = function (...args) {
|
145 | wrapper.call(this, realMethod, mixins, ...args);
|
146 | };
|
147 |
|
148 | return fn;
|
149 | }
|
150 |
|
151 | function patch(target, methodName, mixinMethod) {
|
152 | const mixins = getMixins(target, methodName);
|
153 |
|
154 | if (mixins.methods.indexOf(mixinMethod) < 0) {
|
155 | mixins.methods.push(mixinMethod);
|
156 | }
|
157 |
|
158 | const oldDefinition = Object.getOwnPropertyDescriptor(target, methodName);
|
159 |
|
160 | if (oldDefinition && oldDefinition[mobxPatchedDefinition]) {
|
161 |
|
162 | return;
|
163 | }
|
164 |
|
165 | const originalMethod = target[methodName];
|
166 | const newDefinition = createDefinition(target, methodName, oldDefinition ? oldDefinition.enumerable : undefined, mixins, originalMethod);
|
167 | Object.defineProperty(target, methodName, newDefinition);
|
168 | }
|
169 |
|
170 | function createDefinition(target, methodName, enumerable, mixins, originalMethod) {
|
171 | let wrappedFunc = wrapFunction(originalMethod, mixins);
|
172 | return {
|
173 | [mobxPatchedDefinition]: true,
|
174 | get: function () {
|
175 | return wrappedFunc;
|
176 | },
|
177 | set: function (value) {
|
178 | if (this === target) {
|
179 | wrappedFunc = wrapFunction(value, mixins);
|
180 | } else {
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | const newDefinition = createDefinition(this, methodName, enumerable, mixins, value);
|
186 | Object.defineProperty(this, methodName, newDefinition);
|
187 | }
|
188 | },
|
189 | configurable: true,
|
190 | enumerable: enumerable
|
191 | };
|
192 | }
|
193 |
|
194 | const mobxAdminProperty = $mobx || "$mobx";
|
195 | const mobxIsUnmounted =
|
196 |
|
197 | newSymbol("isUnmounted");
|
198 | const skipRenderKey =
|
199 |
|
200 | newSymbol("skipRender");
|
201 | const isForcingUpdateKey =
|
202 |
|
203 | newSymbol("isForcingUpdate");
|
204 | function makeClassComponentObserver(componentClass) {
|
205 | const target = componentClass.prototype;
|
206 | if (target.componentWillReact) throw new Error("The componentWillReact life-cycle event is no longer supported");
|
207 |
|
208 | if (componentClass["__proto__"] !== PureComponent) {
|
209 | if (!target.shouldComponentUpdate) target.shouldComponentUpdate = observerSCU;else if (target.shouldComponentUpdate !== observerSCU)
|
210 | throw new Error("It is not allowed to use shouldComponentUpdate in observer based components.");
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | makeObservableProp(target, "props");
|
218 | makeObservableProp(target, "state");
|
219 | const baseRender = target.render;
|
220 |
|
221 | target.render = function () {
|
222 | return makeComponentReactive.call(this, baseRender);
|
223 | };
|
224 |
|
225 | patch(target, "componentWillUnmount", function () {
|
226 | if (isUsingStaticRendering() === true) return;
|
227 |
|
228 | if (this.render[mobxAdminProperty]) {
|
229 | this.render[mobxAdminProperty].dispose();
|
230 | } else if (process.env.NODE_ENV !== "production") {
|
231 | const displayName = getDisplayName(this);
|
232 | console.warn(`The render function for an observer component (${displayName}) was modified after MobX attached. This is not supported, since the new function can't be triggered by MobX.`);
|
233 | }
|
234 |
|
235 | this[mobxIsUnmounted] = true;
|
236 | });
|
237 | return componentClass;
|
238 | }
|
239 |
|
240 | function getDisplayName(comp) {
|
241 | return comp.displayName || comp.name || comp.constructor && (comp.constructor.displayName || comp.constructor.name) || "<component>";
|
242 | }
|
243 |
|
244 | function makeComponentReactive(render) {
|
245 | if (isUsingStaticRendering() === true) return render.call(this);
|
246 | |
247 |
|
248 |
|
249 |
|
250 |
|
251 | setHiddenProp(this, skipRenderKey, false);
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 | setHiddenProp(this, isForcingUpdateKey, false);
|
258 | const initialName = getDisplayName(this);
|
259 | const baseRender = render.bind(this);
|
260 | let isRenderingPending = false;
|
261 | const reaction = new Reaction(`${initialName}.render()`, () => {
|
262 | if (!isRenderingPending) {
|
263 |
|
264 |
|
265 |
|
266 | isRenderingPending = true;
|
267 |
|
268 | if (this[mobxIsUnmounted] !== true) {
|
269 | let hasError = true;
|
270 |
|
271 | try {
|
272 | setHiddenProp(this, isForcingUpdateKey, true);
|
273 | if (!this[skipRenderKey]) Component.prototype.forceUpdate.call(this);
|
274 | hasError = false;
|
275 | } finally {
|
276 | setHiddenProp(this, isForcingUpdateKey, false);
|
277 | if (hasError) reaction.dispose();
|
278 | }
|
279 | }
|
280 | }
|
281 | });
|
282 | reaction["reactComponent"] = this;
|
283 | reactiveRender[mobxAdminProperty] = reaction;
|
284 | this.render = reactiveRender;
|
285 |
|
286 | function reactiveRender() {
|
287 | isRenderingPending = false;
|
288 | let exception = undefined;
|
289 | let rendering = undefined;
|
290 | reaction.track(() => {
|
291 | try {
|
292 | rendering = _allowStateChanges(false, baseRender);
|
293 | } catch (e) {
|
294 | exception = e;
|
295 | }
|
296 | });
|
297 |
|
298 | if (exception) {
|
299 | throw exception;
|
300 | }
|
301 |
|
302 | return rendering;
|
303 | }
|
304 |
|
305 | return reactiveRender.call(this);
|
306 | }
|
307 |
|
308 | function observerSCU(nextProps, nextState) {
|
309 | if (isUsingStaticRendering()) {
|
310 | 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.");
|
311 | }
|
312 |
|
313 |
|
314 | if (this.state !== nextState) {
|
315 | return true;
|
316 | }
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | return !shallowEqual(this.props, nextProps);
|
323 | }
|
324 |
|
325 | function makeObservableProp(target, propName) {
|
326 | const valueHolderKey = newSymbol(`reactProp_${propName}_valueHolder`);
|
327 | const atomHolderKey = newSymbol(`reactProp_${propName}_atomHolder`);
|
328 |
|
329 | function getAtom() {
|
330 | if (!this[atomHolderKey]) {
|
331 | setHiddenProp(this, atomHolderKey, createAtom("reactive " + propName));
|
332 | }
|
333 |
|
334 | return this[atomHolderKey];
|
335 | }
|
336 |
|
337 | Object.defineProperty(target, propName, {
|
338 | configurable: true,
|
339 | enumerable: true,
|
340 | get: function () {
|
341 | let prevReadState = false;
|
342 |
|
343 | if (_allowStateReadsStart && _allowStateReadsEnd) {
|
344 | prevReadState = _allowStateReadsStart(true);
|
345 | }
|
346 |
|
347 | getAtom.call(this).reportObserved();
|
348 |
|
349 | if (_allowStateReadsStart && _allowStateReadsEnd) {
|
350 | _allowStateReadsEnd(prevReadState);
|
351 | }
|
352 |
|
353 | return this[valueHolderKey];
|
354 | },
|
355 | set: function set(v) {
|
356 | if (!this[isForcingUpdateKey] && !shallowEqual(this[valueHolderKey], v)) {
|
357 | setHiddenProp(this, valueHolderKey, v);
|
358 | setHiddenProp(this, skipRenderKey, true);
|
359 | getAtom.call(this).reportChanged();
|
360 | setHiddenProp(this, skipRenderKey, false);
|
361 | } else {
|
362 | setHiddenProp(this, valueHolderKey, v);
|
363 | }
|
364 | }
|
365 | });
|
366 | }
|
367 |
|
368 | const hasSymbol = typeof Symbol === "function" && Symbol.for;
|
369 |
|
370 | const ReactForwardRefSymbol = hasSymbol ?
|
371 |
|
372 | Symbol.for("react.forward_ref") : typeof forwardRef === "function" &&
|
373 |
|
374 | forwardRef(props => null)["$$typeof"];
|
375 | const ReactMemoSymbol = hasSymbol ?
|
376 |
|
377 | Symbol.for("react.memo") : typeof memo === "function" &&
|
378 |
|
379 | memo(props => null)["$$typeof"];
|
380 |
|
381 |
|
382 |
|
383 |
|
384 | function observer(component) {
|
385 | if (component["isMobxInjector"] === true) {
|
386 | console.warn("Mobx observer: You are trying to use 'observer' on a component that already has 'inject'. Please apply 'observer' before applying 'inject'");
|
387 | }
|
388 |
|
389 | if (ReactMemoSymbol && component["$$typeof"] === ReactMemoSymbol) {
|
390 | throw new Error("Mobx observer: You are trying to use 'observer' on function component wrapped to either another observer or 'React.memo'. The observer already applies 'React.memo' for you.");
|
391 | }
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | if (ReactForwardRefSymbol && component["$$typeof"] === ReactForwardRefSymbol) {
|
397 | const baseRender = component["render"];
|
398 | if (typeof baseRender !== "function") throw new Error("render property of ForwardRef was not a function");
|
399 | return forwardRef(function ObserverForwardRef() {
|
400 | return createElement(Observer, null, () => baseRender.apply(undefined, arguments));
|
401 | });
|
402 | }
|
403 |
|
404 |
|
405 | if (typeof component === "function" && (!component.prototype || !component.prototype.render) && !component["isReactClass"] && !Object.prototype.isPrototypeOf.call(Component, component)) {
|
406 | return observer$1(component);
|
407 | }
|
408 |
|
409 | return makeClassComponentObserver(component);
|
410 | }
|
411 |
|
412 | const MobXProviderContext =
|
413 |
|
414 | React__default.createContext({});
|
415 | function Provider(props) {
|
416 | const {
|
417 | children,
|
418 | ...stores
|
419 | } = props;
|
420 | const parentValue = React__default.useContext(MobXProviderContext);
|
421 | const mutableProviderRef = React__default.useRef({ ...parentValue,
|
422 | ...stores
|
423 | });
|
424 | const value = mutableProviderRef.current;
|
425 |
|
426 | if (process.env.NODE_ENV !== "production") {
|
427 | const newValue = { ...value,
|
428 | ...stores
|
429 | };
|
430 |
|
431 | if (!shallowEqual(value, newValue)) {
|
432 | 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.");
|
433 | }
|
434 | }
|
435 |
|
436 | return React__default.createElement(MobXProviderContext.Provider, {
|
437 | value: value
|
438 | }, children);
|
439 | }
|
440 | Provider.displayName = "MobXProvider";
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 | function createStoreInjector(grabStoresFn, component, injectNames, makeReactive) {
|
447 |
|
448 | let Injector = React__default.forwardRef((props, ref) => {
|
449 | const newProps = { ...props
|
450 | };
|
451 | const context = React__default.useContext(MobXProviderContext);
|
452 | Object.assign(newProps, grabStoresFn(context || {}, newProps) || {});
|
453 |
|
454 | if (ref) {
|
455 | newProps.ref = ref;
|
456 | }
|
457 |
|
458 | return React__default.createElement(component, newProps);
|
459 | });
|
460 | if (makeReactive) Injector = observer(Injector);
|
461 | Injector["isMobxInjector"] = true;
|
462 |
|
463 |
|
464 | copyStaticProperties(component, Injector);
|
465 | Injector["wrappedComponent"] = component;
|
466 | Injector.displayName = getInjectName(component, injectNames);
|
467 | return Injector;
|
468 | }
|
469 |
|
470 | function getInjectName(component, injectNames) {
|
471 | let displayName;
|
472 | const componentName = component.displayName || component.name || component.constructor && component.constructor.name || "Component";
|
473 | if (injectNames) displayName = "inject-with-" + injectNames + "(" + componentName + ")";else displayName = "inject(" + componentName + ")";
|
474 | return displayName;
|
475 | }
|
476 |
|
477 | function grabStoresByName(storeNames) {
|
478 | return function (baseStores, nextProps) {
|
479 | storeNames.forEach(function (storeName) {
|
480 | if (storeName in nextProps
|
481 | ) return;
|
482 | if (!(storeName in baseStores)) throw new Error("MobX injector: Store '" + storeName + "' is not available! Make sure it is provided by some Provider");
|
483 | nextProps[storeName] = baseStores[storeName];
|
484 | });
|
485 | return nextProps;
|
486 | };
|
487 | }
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | function inject(
|
497 | /* fn(stores, nextProps) or ...storeNames */
|
498 | ...storeNames) {
|
499 | if (typeof arguments[0] === "function") {
|
500 | let grabStoresFn = arguments[0];
|
501 | return componentClass => createStoreInjector(grabStoresFn, componentClass, grabStoresFn.name, true);
|
502 | } else {
|
503 | return componentClass => createStoreInjector(grabStoresByName(storeNames), componentClass, storeNames.join("-"), false);
|
504 | }
|
505 | }
|
506 |
|
507 | const protoStoreKey =
|
508 |
|
509 | newSymbol("disposeOnUnmountProto");
|
510 | const instStoreKey =
|
511 |
|
512 | newSymbol("disposeOnUnmountInst");
|
513 |
|
514 | function runDisposersOnWillUnmount() {
|
515 | [...(this[protoStoreKey] || []), ...(this[instStoreKey] || [])].forEach(propKeyOrFunction => {
|
516 | const prop = typeof propKeyOrFunction === "string" ? this[propKeyOrFunction] : propKeyOrFunction;
|
517 |
|
518 | if (prop !== undefined && prop !== null) {
|
519 | if (Array.isArray(prop)) prop.map(f => f());else prop();
|
520 | }
|
521 | });
|
522 | }
|
523 |
|
524 | function disposeOnUnmount(target, propertyKeyOrFunction) {
|
525 | if (Array.isArray(propertyKeyOrFunction)) {
|
526 | return propertyKeyOrFunction.map(fn => disposeOnUnmount(target, fn));
|
527 | }
|
528 |
|
529 | const c = Object.getPrototypeOf(target).constructor || Object.getPrototypeOf(target.constructor);
|
530 | const c2 = Object.getPrototypeOf(target.constructor);
|
531 |
|
532 | if (!(c === React__default.Component || c === React__default.PureComponent || c2 === React__default.Component || c2 === React__default.PureComponent)) {
|
533 | throw new Error("[mobx-react] disposeOnUnmount only supports direct subclasses of React.Component or React.PureComponent.");
|
534 | }
|
535 |
|
536 | if (typeof propertyKeyOrFunction !== "string" && typeof propertyKeyOrFunction !== "function" && !Array.isArray(propertyKeyOrFunction)) {
|
537 | throw new Error("[mobx-react] disposeOnUnmount only works if the parameter is either a property key or a function.");
|
538 | }
|
539 |
|
540 |
|
541 | const isDecorator = typeof propertyKeyOrFunction === "string";
|
542 |
|
543 | const componentWasAlreadyModified = !!target[protoStoreKey] || !!target[instStoreKey];
|
544 | const store = isDecorator ?
|
545 | target[protoStoreKey] || (target[protoStoreKey] = []) :
|
546 | target[instStoreKey] || (target[instStoreKey] = []);
|
547 | store.push(propertyKeyOrFunction);
|
548 |
|
549 | if (!componentWasAlreadyModified) {
|
550 | patch(target, "componentWillUnmount", runDisposersOnWillUnmount);
|
551 | }
|
552 |
|
553 |
|
554 | if (typeof propertyKeyOrFunction !== "string") {
|
555 | return propertyKeyOrFunction;
|
556 | }
|
557 | }
|
558 |
|
559 | function createChainableTypeChecker(validator) {
|
560 | function checkType(isRequired, props, propName, componentName, location, propFullName, ...rest) {
|
561 | return untracked(() => {
|
562 | componentName = componentName || "<<anonymous>>";
|
563 | propFullName = propFullName || propName;
|
564 |
|
565 | if (props[propName] == null) {
|
566 | if (isRequired) {
|
567 | const actual = props[propName] === null ? "null" : "undefined";
|
568 | return new Error("The " + location + " `" + propFullName + "` is marked as required " + "in `" + componentName + "`, but its value is `" + actual + "`.");
|
569 | }
|
570 |
|
571 | return null;
|
572 | } else {
|
573 |
|
574 | return validator(props, propName, componentName, location, propFullName, ...rest);
|
575 | }
|
576 | });
|
577 | }
|
578 |
|
579 | const chainedCheckType = checkType.bind(null, false);
|
580 |
|
581 | chainedCheckType.isRequired = checkType.bind(null, true);
|
582 | return chainedCheckType;
|
583 | }
|
584 |
|
585 |
|
586 | function isSymbol(propType, propValue) {
|
587 |
|
588 | if (propType === "symbol") {
|
589 | return true;
|
590 | }
|
591 |
|
592 |
|
593 | if (propValue["@@toStringTag"] === "Symbol") {
|
594 | return true;
|
595 | }
|
596 |
|
597 |
|
598 | if (typeof Symbol === "function" && propValue instanceof Symbol) {
|
599 | return true;
|
600 | }
|
601 |
|
602 | return false;
|
603 | }
|
604 |
|
605 |
|
606 | function getPropType(propValue) {
|
607 | const propType = typeof propValue;
|
608 |
|
609 | if (Array.isArray(propValue)) {
|
610 | return "array";
|
611 | }
|
612 |
|
613 | if (propValue instanceof RegExp) {
|
614 |
|
615 |
|
616 |
|
617 | return "object";
|
618 | }
|
619 |
|
620 | if (isSymbol(propType, propValue)) {
|
621 | return "symbol";
|
622 | }
|
623 |
|
624 | return propType;
|
625 | }
|
626 |
|
627 |
|
628 |
|
629 | function getPreciseType(propValue) {
|
630 | const propType = getPropType(propValue);
|
631 |
|
632 | if (propType === "object") {
|
633 | if (propValue instanceof Date) {
|
634 | return "date";
|
635 | } else if (propValue instanceof RegExp) {
|
636 | return "regexp";
|
637 | }
|
638 | }
|
639 |
|
640 | return propType;
|
641 | }
|
642 |
|
643 | function createObservableTypeCheckerCreator(allowNativeType, mobxType) {
|
644 | return createChainableTypeChecker((props, propName, componentName, location, propFullName) => {
|
645 | return untracked(() => {
|
646 | if (allowNativeType) {
|
647 | if (getPropType(props[propName]) === mobxType.toLowerCase()) return null;
|
648 | }
|
649 |
|
650 | let mobxChecker;
|
651 |
|
652 | switch (mobxType) {
|
653 | case "Array":
|
654 | mobxChecker = isObservableArray;
|
655 | break;
|
656 |
|
657 | case "Object":
|
658 | mobxChecker = isObservableObject;
|
659 | break;
|
660 |
|
661 | case "Map":
|
662 | mobxChecker = isObservableMap;
|
663 | break;
|
664 |
|
665 | default:
|
666 | throw new Error(`Unexpected mobxType: ${mobxType}`);
|
667 | }
|
668 |
|
669 | const propValue = props[propName];
|
670 |
|
671 | if (!mobxChecker(propValue)) {
|
672 | const preciseType = getPreciseType(propValue);
|
673 | const nativeTypeExpectationMessage = allowNativeType ? " or javascript `" + mobxType.toLowerCase() + "`" : "";
|
674 | return new Error("Invalid prop `" + propFullName + "` of type `" + preciseType + "` supplied to" + " `" + componentName + "`, expected `mobx.Observable" + mobxType + "`" + nativeTypeExpectationMessage + ".");
|
675 | }
|
676 |
|
677 | return null;
|
678 | });
|
679 | });
|
680 | }
|
681 |
|
682 | function createObservableArrayOfTypeChecker(allowNativeType, typeChecker) {
|
683 | return createChainableTypeChecker((props, propName, componentName, location, propFullName, ...rest) => {
|
684 | return untracked(() => {
|
685 | if (typeof typeChecker !== "function") {
|
686 | return new Error("Property `" + propFullName + "` of component `" + componentName + "` has " + "invalid PropType notation.");
|
687 | } else {
|
688 | let error = createObservableTypeCheckerCreator(allowNativeType, "Array")(props, propName, componentName, location, propFullName);
|
689 | if (error instanceof Error) return error;
|
690 | const propValue = props[propName];
|
691 |
|
692 | for (let i = 0; i < propValue.length; i++) {
|
693 | error = typeChecker(propValue, i, componentName, location, propFullName + "[" + i + "]", ...rest);
|
694 | if (error instanceof Error) return error;
|
695 | }
|
696 |
|
697 | return null;
|
698 | }
|
699 | });
|
700 | });
|
701 | }
|
702 |
|
703 | const observableArray =
|
704 |
|
705 | createObservableTypeCheckerCreator(false, "Array");
|
706 | const observableArrayOf =
|
707 |
|
708 | createObservableArrayOfTypeChecker.bind(null, false);
|
709 | const observableMap =
|
710 |
|
711 | createObservableTypeCheckerCreator(false, "Map");
|
712 | const observableObject =
|
713 |
|
714 | createObservableTypeCheckerCreator(false, "Object");
|
715 | const arrayOrObservableArray =
|
716 |
|
717 | createObservableTypeCheckerCreator(true, "Array");
|
718 | const arrayOrObservableArrayOf =
|
719 |
|
720 | createObservableArrayOfTypeChecker.bind(null, true);
|
721 | const objectOrObservableObject =
|
722 |
|
723 | createObservableTypeCheckerCreator(true, "Object");
|
724 | const PropTypes = {
|
725 | observableArray,
|
726 | observableArrayOf,
|
727 | observableMap,
|
728 | observableObject,
|
729 | arrayOrObservableArray,
|
730 | arrayOrObservableArrayOf,
|
731 | objectOrObservableObject
|
732 | };
|
733 |
|
734 | if (!Component) throw new Error("mobx-react requires React to be available");
|
735 | if (!observable) throw new Error("mobx-react requires mobx to be available");
|
736 | if (typeof unstable_batchedUpdates === "function") configure({
|
737 | reactionScheduler: unstable_batchedUpdates
|
738 | });
|
739 |
|
740 | export { MobXProviderContext, PropTypes, Provider, disposeOnUnmount, inject, observer };
|
741 |
|