|
| 1 | +/// <reference lib="dom" /> |
| 2 | +export { EventEmitter, EventEmitterEvents } from './interface'; |
| 3 | +import type { EventEmitterEvents, Timeout, Timer } from './interface'; |
| 4 | + |
| 5 | +import { EventEmitter as NodeEventEmitter } from 'events'; |
| 6 | +import 'event-target-shim'; |
| 7 | +import structuredClone from '@ungap/structured-clone'; |
| 8 | +import uuid from 'react-native-uuid'; |
| 9 | + |
| 10 | +if (!('structuredClone' in globalThis)) { |
| 11 | + // @ts-expect-error - This is the recommended approach from ungap/structured-clone |
| 12 | + globalThis.structuredClone = structuredClone; |
| 13 | +} |
| 14 | +export const randomUUID = (): string => uuid.v4(); |
| 15 | + |
| 16 | +export function loadEnv(): Record<string, string | undefined> { |
| 17 | + return {}; |
| 18 | +} |
| 19 | + |
| 20 | +export class ReactNativeEventEmitter< |
| 21 | + Events extends EventEmitterEvents = Record<string, any[]>, |
| 22 | +> extends NodeEventEmitter { |
| 23 | + override on<K extends keyof Events & (string | symbol)>( |
| 24 | + type: K, |
| 25 | + listener: (...args: Events[K]) => void, |
| 26 | + ): this { |
| 27 | + // Node's typings accept string | symbol; cast is safe. |
| 28 | + return super.on(type as string | symbol, listener); |
| 29 | + } |
| 30 | + |
| 31 | + override off<K extends keyof Events & (string | symbol)>( |
| 32 | + type: K, |
| 33 | + listener: (...args: Events[K]) => void, |
| 34 | + ): this { |
| 35 | + return super.off(type as string | symbol, listener); |
| 36 | + } |
| 37 | + |
| 38 | + override emit<K extends keyof Events & (string | symbol)>( |
| 39 | + type: K, |
| 40 | + ...args: Events[K] |
| 41 | + ): boolean { |
| 42 | + return super.emit(type as string | symbol, ...args); |
| 43 | + } |
| 44 | + |
| 45 | + override once<K extends keyof Events & (string | symbol)>( |
| 46 | + type: K, |
| 47 | + listener: (...args: Events[K]) => void, |
| 48 | + ): this { |
| 49 | + return super.once(type as string | symbol, listener); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export { ReactNativeEventEmitter as RuntimeEventEmitter }; |
| 54 | + |
| 55 | +// Streams – placeholders (unused by the SDK on RN) |
| 56 | +export const Readable = class {}; |
| 57 | +export const ReadableStream = globalThis.ReadableStream; |
| 58 | +export const ReadableStreamController = |
| 59 | + globalThis.ReadableStreamDefaultController; |
| 60 | +export const TransformStream = globalThis.TransformStream; |
| 61 | + |
| 62 | +export class AsyncLocalStorage { |
| 63 | + #ctx: unknown = null; |
| 64 | + |
| 65 | + run<T>(store: T, fn: () => unknown) { |
| 66 | + this.#ctx = store; |
| 67 | + return fn(); |
| 68 | + } |
| 69 | + getStore<T>() { |
| 70 | + return this.#ctx as T; |
| 71 | + } |
| 72 | + enterWith<T>(store: T) { |
| 73 | + this.#ctx = store; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export function isBrowserEnvironment(): boolean { |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +export function isTracingLoopRunningByDefault(): boolean { |
| 82 | + return false; |
| 83 | +} |
| 84 | + |
| 85 | +/* MCP not supported on mobile; export browser stubs */ |
| 86 | +export { MCPServerStdio, MCPServerStreamableHttp } from './mcp-server/browser'; |
| 87 | + |
| 88 | +class RNTimer implements Timer { |
| 89 | + setTimeout(cb: () => void, ms: number): Timeout { |
| 90 | + const id: any = setTimeout(cb, ms); |
| 91 | + // RN timers don’t expose ref/unref; shim them |
| 92 | + id.ref ??= () => id; |
| 93 | + id.unref ??= () => id; |
| 94 | + id.hasRef ??= () => true; |
| 95 | + id.refresh ??= () => id; |
| 96 | + return id; |
| 97 | + } |
| 98 | + |
| 99 | + clearTimeout(id: Timeout | string | number | undefined) { |
| 100 | + clearTimeout(id as number); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +const timer = new RNTimer(); |
| 105 | +export { timer }; |
0 commit comments