How onesignal/onesignal-website-sdk works
Main Components
• SDK Client & Initialization: Represented by the code in src/entries (pageSdkInit, sdk.ts, etc.) which bootstraps the SDK on a webpage.
• Core Functionality Layer: The src/core directory encapsulates modules for model management, caching, executors, operations, and request services. This is the heart of the SDK’s business logic.
• Service Worker Layer: The src/sw directory holds the ServiceWorker code and related helpers. This is responsible for push notification handling, background tasks, and interacting with notifications.
• OneSignal Integration: The src/onesignal directory defines the primary public API, event handling, and namespace integration for user-facing interactions with OneSignal.
• Page & UI Components: Under src/page and its subdirectories (e.g., bell, errors, slidedown), the user interface and error handling aspects are implemented.
• Shared Utilities and Managers: The src/shared folder provides underlying utilities, API wrappers (in src/shared/api), shared managers (e.g., PermissionManager, SessionManager), helper functions, and error handling common to both the client SDK and service worker.
• Testing Infrastructure: The __test__ directory shows a robust test infrastructure, including unit tests for APIs, core modules, notifications, and push/subscription flows.
• DevOps/Build & Preview: Top-level items like Dockerfile, docker-compose.yml, and the preview folder indicate how the project is containerized and previewed before deployment.
Relationships and Interactions
• The Client-Side SDK (initialized via the code in src/entries) interacts with the Core and Shared layers to manage subscriptions, caching, and API requests.
• Registration of the Service Worker (indicated by OneSignalSDKWorker.js and code in src/sw) is triggered from the client, establishing communication between the webpage and the background processes (service worker).
• The OneSignal integration and public API in src/onesignal interface with the developer’s page code and call down into the lower layers (core, shared, page managers) to coordinate behavior (subscriptions, event handling, push processing).
• API communication flows through modules in src/shared/api and src/core/requestService, which handle the routing and management of data both internally (e.g., caching or model updates) and externally to OneSignal’s servers.
Architectural Patterns and Design Principles
• Modularity: The separation into clear directories (core, onesignal, page, shared, sw) suggests well-defined modules with single responsibilities.
• Layered Architecture: There is a clear distinction between the client initialization layer, the core business logic, and the service worker background processes.
• Test-Driven Design: Given the extensive suite in the __test__ directory, unit tests are central, suggesting that components can be individually mapped, tested, and maintained.
• Separation of Concerns: UI components, network/API services, business logic, and service worker code are kept separate, promoting maintainability and clarity.
Relevant Technologies, Frameworks, and Tools
• TypeScript: Evident by extensive .ts and .d.ts files throughout the src folders.
• Vite: The presence of vite.config.ts and vitest.config.ts indicates a modern build and testing environment.
• Docker: Dockerfile and docker-compose.yml support containerized development/testing.
• Testing Libraries: The __test__ structure along with scripts for unit tests hint at a robust testing framework.
• ESLint and Prettier: Code quality and formatting are enforced via configuration files (.eslintrc.cjs, .prettierrc.cjs).