UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

75 lines (72 loc) • 2.7 kB
import { Platform } from "react-native"; import { NitroModules } from "react-native-nitro-modules"; import { rethrowNativeError } from "../nativeError.native.js"; import { assertActionHandler } from "./validate.native.js"; import { AppIntents as AppIntents$1 } from "./unavailable.native.js"; var hybrid; var handlers = /* @__PURE__ */ new Map(); function native() { if (Platform.OS !== "ios") throw new Error("AppIntents requires an iOS native build"); hybrid !== null && hybrid !== void 0 ? hybrid : hybrid = NitroModules.createHybridObject("OneAppIntents"); return hybrid; } function errorMessage(error) { return error instanceof Error ? error.message : String(error); } function defineAction(identifier, handler) { assertActionHandler(identifier, handler); if (handlers.has(identifier)) { throw new Error(`AppIntents.defineAction: ${identifier} is already defined`); } handlers.set(identifier, handler); var dispatch = function dispatch2(invocation2) { Promise.resolve().then(function() { var _invocation_text; return handler((_invocation_text = invocation2.text) !== null && _invocation_text !== void 0 ? _invocation_text : null); }).then(function(result) { if (typeof result !== "string") { throw new TypeError("AppIntents action handler must return a string"); } native().complete(invocation2.executionId, true, result); }).catch(function(error) { native().complete(invocation2.executionId, false, errorMessage(error)); }); }; var remove; try { remove = native().addInvocationListener(identifier, dispatch); var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { for (var _iterator = native().claimPending(identifier)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var invocation = _step.value; dispatch(invocation); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } catch (error) { remove === null || remove === void 0 ? void 0 : remove(); handlers.delete(identifier); rethrowNativeError(error); } return function() { if (handlers.get(identifier) !== handler) return; handlers.delete(identifier); remove === null || remove === void 0 ? void 0 : remove(); }; } var nativeAppIntents = Object.freeze({ defineAction }); var AppIntents = Platform.OS === "android" ? AppIntents$1 : nativeAppIntents; export { AppIntents }; //# sourceMappingURL=index.native.js.map