one
Version:
One is a React framework for web, iOS, and Android apps.
81 lines (78 loc) • 3.11 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useBackgroundComputation_exports = {};
__export(useBackgroundComputation_exports, { useBackgroundComputation: () => useBackgroundComputation });
module.exports = __toCommonJS(useBackgroundComputation_exports);
var import_react = require("react");
var import_createLatestComputation = require("./createLatestComputation.native.js");
function useBackgroundComputation(definition, input) {
var _submitted_current, _owner_current;
var [state, setState] = (0, import_react.useState)({
phase: "idle",
revision: 0
});
var owner = (0, import_react.useRef)(null);
var submitted = (0, import_react.useRef)(null);
var active = input !== null;
(0, import_react.useLayoutEffect)(function() {
if (!active) return;
var computation = (0, import_createLatestComputation.createLatestComputation)(definition, function(changed) {
setState(changed.phase === "ready" ? {
phase: "ready",
revision: changed.result.revision
} : changed);
});
owner.current = computation;
return function() {
owner.current = null;
submitted.current = null;
computation.dispose();
};
}, [active, definition]);
(0, import_react.useLayoutEffect)(function() {
var _owner_current2;
if (input === null) return;
submitted.current = {
input,
factory: definition
};
(_owner_current2 = owner.current) === null || _owner_current2 === void 0 ? void 0 : _owner_current2.update(input);
}, [
active,
definition,
input
]);
var getCurrent = (0, import_react.useCallback)(function() {
var _ref;
var _owner_current2;
return (_ref = (_owner_current2 = owner.current) === null || _owner_current2 === void 0 ? void 0 : _owner_current2.getCurrent()) !== null && _ref !== void 0 ? _ref : null;
}, []);
var matches = active && ((_submitted_current = submitted.current) === null || _submitted_current === void 0 ? void 0 : _submitted_current.input) === input && submitted.current.factory === definition;
if (matches && state.phase === "failed") throw state.error;
var current = (_owner_current = owner.current) === null || _owner_current === void 0 ? void 0 : _owner_current.getCurrent();
var result = matches && state.phase === "ready" && current && state.revision === current.revision ? current : null;
return {
result,
getCurrent
};
}
//# sourceMappingURL=useBackgroundComputation.native.js.map