UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

144 lines (143 loc) 7.09 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target, mod )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var useLinking_native_exports = {}; __export(useLinking_native_exports, { useLinking: () => useLinking }); module.exports = __toCommonJS(useLinking_native_exports); var import_core = require("@react-navigation/core"), React = __toESM(require("react"), 1), import_react_native = require("react-native"), import_extractPathFromURL = require("./extractPathFromURL"), linkingHandlers = []; function useLinking(ref, param, onUnhandledLinking) { var { enabled = !0, prefixes, filter, config, getInitialURL = function() { return Promise.race([ import_react_native.Linking.getInitialURL(), new Promise(function(resolve) { setTimeout(resolve, 150); }) ]); }, subscribe = function(listener) { var _Linking_removeEventListener, callback = function(param2) { var { url } = param2; return listener(url); }, subscription = import_react_native.Linking.addEventListener("url", callback), removeEventListener = (_Linking_removeEventListener = import_react_native.Linking.removeEventListener) === null || _Linking_removeEventListener === void 0 ? void 0 : _Linking_removeEventListener.bind(import_react_native.Linking); return function() { subscription != null && subscription.remove ? subscription.remove() : removeEventListener == null || removeEventListener("url", callback); }; }, getStateFromPath = import_core.getStateFromPath, getActionFromState = import_core.getActionFromState } = param, independent = (0, import_core.useNavigationIndependentTree)(); React.useEffect(function() { if (process.env.NODE_ENV !== "production" && !independent) { enabled !== !1 && linkingHandlers.length && console.error([ "Looks like you have configured linking in multiple places. This is likely an error since deep links should only be handled in one place to avoid conflicts. Make sure that:", "- You don't have multiple NavigationContainers in the app each with 'linking' enabled", "- Only a single instance of the root component is rendered", import_react_native.Platform.OS === "android" ? "- You have set 'android:launchMode=singleTask' in the '<activity />' section of the 'AndroidManifest.xml' file to avoid launching multiple instances" : "" ].join(` `).trim()); var handler = Symbol(); return enabled !== !1 && linkingHandlers.push(handler), function() { var index = linkingHandlers.indexOf(handler); index > -1 && linkingHandlers.splice(index, 1); }; } }, [ enabled, independent ]); var enabledRef = React.useRef(enabled), prefixesRef = React.useRef(prefixes), filterRef = React.useRef(filter), configRef = React.useRef(config), getInitialURLRef = React.useRef(getInitialURL), getStateFromPathRef = React.useRef(getStateFromPath), getActionFromStateRef = React.useRef(getActionFromState); React.useEffect(function() { enabledRef.current = enabled, prefixesRef.current = prefixes, filterRef.current = filter, configRef.current = config, getInitialURLRef.current = getInitialURL, getStateFromPathRef.current = getStateFromPath, getActionFromStateRef.current = getActionFromState; }); var getStateFromURL = React.useCallback(function(url) { if (!(!url || filterRef.current && !filterRef.current(url))) { var path = (0, import_extractPathFromURL.extractPathFromURL)(prefixesRef.current, url); return path !== void 0 ? getStateFromPathRef.current(path, configRef.current) : void 0; } }, []), getInitialState = React.useCallback(function() { var state; if (enabledRef.current) { var url = getInitialURLRef.current(); if (url != null) { if (typeof url != "string") return url.then(function(url2) { var state2 = getStateFromURL(url2); return typeof url2 == "string" && onUnhandledLinking((0, import_extractPathFromURL.extractPathFromURL)(prefixes, url2)), state2; }); onUnhandledLinking((0, import_extractPathFromURL.extractPathFromURL)(prefixes, url)); } state = getStateFromURL(url); } var thenable = { // biome-ignore lint/suspicious/noThenProperty: don't check copied code then(onfulfilled) { return Promise.resolve(onfulfilled ? onfulfilled(state) : state); }, catch() { return thenable; } }; return thenable; }, [ getStateFromURL, onUnhandledLinking, prefixes ]); return React.useEffect(function() { var listener = function(url) { if (enabled) { var navigation = ref.current, state = navigation ? getStateFromURL(url) : void 0; if (navigation && state) { onUnhandledLinking((0, import_extractPathFromURL.extractPathFromURL)(prefixes, url)); var rootState = navigation.getRootState(); if (state.routes.some(function(r) { return !(rootState != null && rootState.routeNames.includes(r.name)); })) return; var action = getActionFromStateRef.current(state, configRef.current); if (action !== void 0) try { navigation.dispatch(action); } catch (e) { console.warn(`An error occurred when trying to handle the link '${url}': ${typeof e == "object" && e != null && "message" in e ? e.message : e}`); } else navigation.resetRoot(state); } } }; return subscribe(listener); }, [ enabled, getStateFromURL, onUnhandledLinking, prefixes, ref, subscribe ]), { getInitialState }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useLinking }); //# sourceMappingURL=useLinking.native.js.map