1 | var _a, _b;
|
2 | import { forwardRef, memo } from "react";
|
3 | import { isUsingStaticRendering } from "./staticRendering";
|
4 | import { useObserver } from "./useObserver";
|
5 | var warnObserverOptionsDeprecated = true;
|
6 | var hasSymbol = typeof Symbol === "function" && Symbol.for;
|
7 | var isFunctionNameConfigurable = (_b = (_a = Object.getOwnPropertyDescriptor(function () { }, "name")) === null || _a === void 0 ? void 0 : _a.configurable) !== null && _b !== void 0 ? _b : false;
|
8 |
|
9 | var ReactForwardRefSymbol = hasSymbol
|
10 | ? Symbol.for("react.forward_ref")
|
11 | : typeof forwardRef === "function" && forwardRef(function (props) { return null; })["$$typeof"];
|
12 | var ReactMemoSymbol = hasSymbol
|
13 | ? Symbol.for("react.memo")
|
14 | : typeof memo === "function" && memo(function (props) { return null; })["$$typeof"];
|
15 |
|
16 | export function observer(baseComponent,
|
17 | // TODO remove in next major
|
18 | options) {
|
19 | var _a;
|
20 | if (process.env.NODE_ENV !== "production" && warnObserverOptionsDeprecated && options) {
|
21 | warnObserverOptionsDeprecated = false;
|
22 | console.warn("[mobx-react-lite] `observer(fn, { forwardRef: true })` is deprecated, use `observer(React.forwardRef(fn))`");
|
23 | }
|
24 | if (ReactMemoSymbol && baseComponent["$$typeof"] === ReactMemoSymbol) {
|
25 | throw new Error("[mobx-react-lite] You are trying to use `observer` on a function component wrapped in either another `observer` or `React.memo`. The observer already applies 'React.memo' for you.");
|
26 | }
|
27 |
|
28 | if (isUsingStaticRendering()) {
|
29 | return baseComponent;
|
30 | }
|
31 | var useForwardRef = (_a = options === null || options === void 0 ? void 0 : options.forwardRef) !== null && _a !== void 0 ? _a : false;
|
32 | var render = baseComponent;
|
33 | var baseComponentName = baseComponent.displayName || baseComponent.name;
|
34 |
|
35 |
|
36 | if (ReactForwardRefSymbol && baseComponent["$$typeof"] === ReactForwardRefSymbol) {
|
37 | useForwardRef = true;
|
38 | render = baseComponent["render"];
|
39 | if (typeof render !== "function") {
|
40 | throw new Error("[mobx-react-lite] `render` property of ForwardRef was not a function");
|
41 | }
|
42 | }
|
43 | var observerComponent = function (props, ref) {
|
44 | return useObserver(function () { return render(props, ref); }, baseComponentName);
|
45 | };
|
46 | observerComponent.displayName = baseComponent.displayName;
|
47 | if (isFunctionNameConfigurable) {
|
48 | Object.defineProperty(observerComponent, "name", {
|
49 | value: baseComponent.name,
|
50 | writable: true,
|
51 | configurable: true
|
52 | });
|
53 | }
|
54 |
|
55 | if (baseComponent.contextTypes) {
|
56 | ;
|
57 | observerComponent.contextTypes = baseComponent.contextTypes;
|
58 | }
|
59 | if (useForwardRef) {
|
60 |
|
61 |
|
62 |
|
63 | observerComponent = forwardRef(observerComponent);
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 | observerComponent = memo(observerComponent);
|
69 | copyStaticProperties(baseComponent, observerComponent);
|
70 | if ("production" !== process.env.NODE_ENV) {
|
71 | Object.defineProperty(observerComponent, "contextTypes", {
|
72 | set: function () {
|
73 | var _a, _b;
|
74 | throw new Error("[mobx-react-lite] `".concat(this.displayName || ((_a = this.type) === null || _a === void 0 ? void 0 : _a.displayName) || ((_b = this.type) === null || _b === void 0 ? void 0 : _b.name) || "Component", ".contextTypes` must be set before applying `observer`."));
|
75 | }
|
76 | });
|
77 | }
|
78 | return observerComponent;
|
79 | }
|
80 |
|
81 | var hoistBlackList = {
|
82 | $$typeof: true,
|
83 | render: true,
|
84 | compare: true,
|
85 | type: true,
|
86 |
|
87 |
|
88 | displayName: true
|
89 | };
|
90 | function copyStaticProperties(base, target) {
|
91 | Object.keys(base).forEach(function (key) {
|
92 | if (!hoistBlackList[key]) {
|
93 | Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(base, key));
|
94 | }
|
95 | });
|
96 | }
|
97 |
|
\ | No newline at end of file |