How binaryminds/react-native-sse works
Main Components
• Library API / Interface: Represented by the index.js and index.d.ts files, which serve as the public entry point to the library. This is where users import and construct their EventSource instances.
• Core EventSource Module: Located in src/EventSource.js. This module contains the logic to create a connection (via XMLHttpRequest), handle event listeners, and manage options such as headers, HTTP methods, timeouts, etc.
• Event Handling & Listeners: Represent the internal observer mechanism that listens to events (“open”, “message”, “error”, “close”, and custom events) and dispatches these to user-supplied callbacks.
Detail the Relationships and Interactions
• Flow from the consuming application (the client code or React Native app) importing the library (via index.js/index.d.ts).
• From the public API, illustrate how a new EventSource object is instantiated with a URL and options.
• Demonstrate the internal connection lifecycle: the EventSource module uses XMLHttpRequest under the hood to establish and maintain an SSE connection.
• Registration of event listeners on the EventSource instance and how events are handled and forwarded to client code.
• How configuration options are passed through and applied to the connection behavior.
Architectural Patterns and Principles
• Observer/Listener Pattern: Emphasize the event-driven nature where subscribers register listeners for different event types.
• Modular Design: The separation of the public interface (index.js / index.d.ts) from the implementation inside the src directory.
• Abstraction over Native Networking: The library abstracts away the complexities of using XMLHttpRequest to provide a simpler API similar to the native EventSource interface.
Incorporate Relevant Technologies and Frameworks
• React Native: Since this is aimed at React Native, note that the underlying environment is a mobile JavaScript runtime.
• XMLHttpRequest: Make clear that this API is used for establishing and handling SSE connections.
• TypeScript: Use of index.d.ts shows that type definitions are available, enhancing development experience.
• Any usage of additional polyfills or middleware (e.g. “react-native-url-polyfill”) can be noted as external dependencies for URL handling.
Essential Elements
• Clear labels for all components (e.g., “EventSource API”, “XHR Connection”, “Event Listener Module”, “Configuration Handler”).