UNPKG

@joenoon/mobx-react-hooks

Version:
123 lines (110 loc) 4.13 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var mobx = require('mobx'); var react = require('react'); if (!react.useState) { throw new Error("@joenoon/mobx-react-hooks requires React with Hooks support"); } if (!mobx.spy) { throw new Error("@joenoon/mobx-react-hooks requires mobx at least version 4 to be available"); } var globalIsUsingStaticRendering = false; function useStaticRendering(enable) { globalIsUsingStaticRendering = enable; } function isUsingStaticRendering() { return globalIsUsingStaticRendering; } function printDebugValue(v) { if (!v.current) { return "<unknown>"; } return mobx.getDependencyTree(v.current); } /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function useForceUpdate() { var _a = __read(react.useState(0), 2), setTick = _a[1]; var update = react.useCallback(function () { setTick(function (tick) { return tick + 1; }); }, []); return update; } var EMPTY_OBJECT = {}; /** * @internal */ function useObserverInternal(fn, baseComponentName, options) { if (baseComponentName === void 0) { baseComponentName = "observed"; } if (options === void 0) { options = EMPTY_OBJECT; } if (isUsingStaticRendering()) { return fn(); } var wantedForceUpdateHook = options.useForceUpdate || useForceUpdate; var forceUpdate = wantedForceUpdateHook(); var reaction = react.useRef(null); var committed = react.useRef(false); if (!reaction.current) { // First render for this component. Not yet committed. reaction.current = new mobx.Reaction("observer(" + baseComponentName + ")", function () { // Observable has changed. Only force an update if we've definitely // been committed. if (committed.current) { forceUpdate(); } }); } react.useDebugValue(reaction, printDebugValue); react.useEffect(function () { committed.current = true; return function () { return reaction.current.dispose(); }; }, []); // render the original component, but have the // reaction track the observables, so that rendering // can be invalidated (see above) once a dependency changes var rendering; var exception; reaction.current.track(function () { try { rendering = fn(); } catch (e) { exception = e; } }); if (exception) { reaction.current.dispose(); throw exception; // re-throw any exceptions catched during rendering } return rendering; } exports.isUsingStaticRendering = isUsingStaticRendering; exports.useForceUpdate = useForceUpdate; exports.useObserverInternal = useObserverInternal; exports.useStaticRendering = useStaticRendering;