@joenoon/mobx-react-hooks
Version:
React Hooks for MobX
116 lines (105 loc) • 3.97 kB
JavaScript
import { spy, getDependencyTree, Reaction } from 'mobx';
import { useState, useCallback, useRef, useDebugValue, useEffect } from 'react';
if (!useState) {
throw new Error("@joenoon/mobx-react-hooks requires React with Hooks support");
}
if (!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 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(useState(0), 2), setTick = _a[1];
var update = 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 = useRef(null);
var committed = useRef(false);
if (!reaction.current) {
// First render for this component. Not yet committed.
reaction.current = new Reaction("observer(" + baseComponentName + ")", function () {
// Observable has changed. Only force an update if we've definitely
// been committed.
if (committed.current) {
forceUpdate();
}
});
}
useDebugValue(reaction, printDebugValue);
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;
}
export { isUsingStaticRendering, useForceUpdate, useObserverInternal, useStaticRendering };