import type { RefComponentMountAdapter } from '../types/refComponentMount'; import type { UrlListenerAdapter } from '../types/urlListener'; import type { MergeNativeRuntimeAdapterInput, NativeRuntimeAdapterState, } from '../types/nativeRuntimeAdapter'; let nativeRuntimeState: NativeRuntimeAdapterState | null = null; /** * 读取当前 native 适配器状态。 * @returns 已注册状态;未注册时为 null */ function getNativeRuntimeAdapter(): NativeRuntimeAdapterState | null { if (!nativeRuntimeState) { return null; } return { ...nativeRuntimeState }; } /** * 是否已注册 native 运行时适配器(mergeNativeAdapter 传入非 null 对象后即为 true)。 * @returns 是否已注册 */ function isNativeRuntimeAdapterRegistered(): boolean { return nativeRuntimeState !== null; } /** * 命令式挂载是否已通过 native 适配器声明为支持(refComponentMount 为非 null 对象)。 * @returns 是否支持 */ function isNativeRefComponentMountSupported(): boolean { const mount = nativeRuntimeState?.refComponentMount; return mount != null && typeof mount === 'object'; } /** * 命令式挂载是否已通过 native 适配器声明为不支持(refComponentMount === null)。 * @returns 是否明确不支持 */ function isNativeRefComponentMountDisabled(): boolean { return nativeRuntimeState != null && nativeRuntimeState.refComponentMount === null; } /** * URL 监听是否已通过 native 适配器声明为支持。 * @returns 是否支持 */ function isNativeUrlListenerSupported(): boolean { const url = nativeRuntimeState?.urlListener; return url != null && typeof url === 'object'; } /** * URL 监听是否已通过 native 适配器声明为不支持。 * @returns 是否明确不支持 */ function isNativeUrlListenerDisabled(): boolean { return nativeRuntimeState != null && nativeRuntimeState.urlListener === null; } /** * 解析 native 适配器中的 refComponentMount 字段。 * @returns unset | none | 适配器 */ function resolveNativeRefComponentMount(): | { kind: 'unset' } | { kind: 'none' } | { kind: 'adapter', adapter: RefComponentMountAdapter } { if (!nativeRuntimeState || nativeRuntimeState.refComponentMount === undefined) { return { kind: 'unset' }; } if (nativeRuntimeState.refComponentMount === null) { return { kind: 'none' }; } return { kind: 'adapter', adapter: nativeRuntimeState.refComponentMount }; } /** * 解析 native 适配器中的 urlListener 字段。 * @returns unset | none | 适配器 */ function resolveNativeUrlListener(): | { kind: 'unset' } | { kind: 'none' } | { kind: 'adapter', adapter: UrlListenerAdapter } { if (!nativeRuntimeState || nativeRuntimeState.urlListener === undefined) { return { kind: 'unset' }; } if (nativeRuntimeState.urlListener === null) { return { kind: 'none' }; } return { kind: 'adapter', adapter: nativeRuntimeState.urlListener }; } /** * 注册 / 更新 / 清空非浏览器运行时统一适配器。 * @param input - 适配器片段;传 null 清空 * @returns 当前状态快照 */ function mergeNativeAdapter( input: MergeNativeRuntimeAdapterInput | null, ): NativeRuntimeAdapterState | null { if (input === null) { nativeRuntimeState = null; return null; } const next: NativeRuntimeAdapterState = { ...(nativeRuntimeState || {}) }; if (input.id !== undefined) { next.id = input.id; } if (input.refComponentMount !== undefined) { next.refComponentMount = input.refComponentMount; } if (input.urlListener !== undefined) { next.urlListener = input.urlListener; } nativeRuntimeState = next; if ( nativeRuntimeState.refComponentMount === undefined && nativeRuntimeState.urlListener === undefined && nativeRuntimeState.id === undefined ) { nativeRuntimeState = null; } return getNativeRuntimeAdapter(); } /** * 重置 native 运行时适配器(主要用于测试)。 */ function resetNativeAdapter(): void { mergeNativeAdapter(null); } /** * 仅清除 native 适配器中的 refComponentMount 字段。 */ function clearNativeRefComponentMount(): void { if (!nativeRuntimeState) { return; } delete nativeRuntimeState.refComponentMount; if ( nativeRuntimeState.urlListener === undefined && nativeRuntimeState.id === undefined ) { nativeRuntimeState = null; } } /** * 仅清除 native 适配器中的 urlListener 字段。 */ function clearNativeUrlListener(): void { if (!nativeRuntimeState) { return; } delete nativeRuntimeState.urlListener; if ( nativeRuntimeState.refComponentMount === undefined && nativeRuntimeState.id === undefined ) { nativeRuntimeState = null; } } export { mergeNativeAdapter, resetNativeAdapter, getNativeRuntimeAdapter, isNativeRuntimeAdapterRegistered, isNativeRefComponentMountSupported, isNativeRefComponentMountDisabled, isNativeUrlListenerSupported, isNativeUrlListenerDisabled, resolveNativeRefComponentMount, resolveNativeUrlListener, clearNativeRefComponentMount, clearNativeUrlListener, };