|
| 1 | +import { AppState, type AppStateStatus } from 'react-native'; |
| 2 | +import { ClixLogger } from '../utils/logging/ClixLogger'; |
| 3 | +import type { EventService } from './EventService'; |
| 4 | +import type { StorageService } from './StorageService'; |
| 5 | + |
| 6 | +enum SessionEvent { |
| 7 | + SESSION_START = 'SESSION_START', |
| 8 | +} |
| 9 | + |
| 10 | +export class SessionService { |
| 11 | + private static readonly LAST_ACTIVITY_KEY = 'clix_session_last_activity'; |
| 12 | + |
| 13 | + private pendingMessageId?: string; |
| 14 | + private readonly effectiveTimeoutMs: number; |
| 15 | + private appStateSubscription?: { remove: () => void }; |
| 16 | + private lastAppState: AppStateStatus; |
| 17 | + |
| 18 | + constructor( |
| 19 | + private readonly storageService: StorageService, |
| 20 | + private readonly eventService: EventService, |
| 21 | + sessionTimeoutMs: number |
| 22 | + ) { |
| 23 | + this.effectiveTimeoutMs = Math.max(sessionTimeoutMs, 5000); |
| 24 | + this.lastAppState = AppState.currentState; |
| 25 | + } |
| 26 | + |
| 27 | + async start(): Promise<void> { |
| 28 | + this.appStateSubscription = AppState.addEventListener( |
| 29 | + 'change', |
| 30 | + this.handleAppStateChange.bind(this) |
| 31 | + ); |
| 32 | + |
| 33 | + const lastActivity = this.storageService.get<number>( |
| 34 | + SessionService.LAST_ACTIVITY_KEY |
| 35 | + ); |
| 36 | + if (lastActivity) { |
| 37 | + const elapsed = Date.now() - lastActivity; |
| 38 | + if (elapsed <= this.effectiveTimeoutMs) { |
| 39 | + this.pendingMessageId = undefined; |
| 40 | + this.updateLastActivity(); |
| 41 | + ClixLogger.debug('Continuing existing session'); |
| 42 | + return; |
| 43 | + } |
| 44 | + } |
| 45 | + await this.startNewSession(); |
| 46 | + } |
| 47 | + |
| 48 | + private async handleAppStateChange( |
| 49 | + nextAppState: AppStateStatus |
| 50 | + ): Promise<void> { |
| 51 | + const previousAppState = this.lastAppState; |
| 52 | + this.lastAppState = nextAppState; |
| 53 | + |
| 54 | + if (previousAppState === 'background' && nextAppState === 'active') { |
| 55 | + // Small delay to allow notification tap handlers to set pendingMessageId |
| 56 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 57 | + |
| 58 | + const lastActivity = this.storageService.get<number>( |
| 59 | + SessionService.LAST_ACTIVITY_KEY |
| 60 | + ); |
| 61 | + if (lastActivity) { |
| 62 | + const elapsed = Date.now() - lastActivity; |
| 63 | + if (elapsed <= this.effectiveTimeoutMs) { |
| 64 | + this.pendingMessageId = undefined; |
| 65 | + this.updateLastActivity(); |
| 66 | + return; |
| 67 | + } |
| 68 | + } |
| 69 | + await this.startNewSession(); |
| 70 | + } else if (nextAppState === 'background') { |
| 71 | + this.updateLastActivity(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + setPendingMessageId(messageId?: string): void { |
| 76 | + this.pendingMessageId = messageId; |
| 77 | + } |
| 78 | + |
| 79 | + private async startNewSession(): Promise<void> { |
| 80 | + const messageId = this.pendingMessageId; |
| 81 | + this.pendingMessageId = undefined; |
| 82 | + this.updateLastActivity(); |
| 83 | + |
| 84 | + try { |
| 85 | + await this.eventService.trackEvent( |
| 86 | + SessionEvent.SESSION_START, |
| 87 | + {}, |
| 88 | + messageId |
| 89 | + ); |
| 90 | + ClixLogger.debug(`${SessionEvent.SESSION_START} tracked`); |
| 91 | + } catch (error) { |
| 92 | + ClixLogger.error(`Failed to track ${SessionEvent.SESSION_START}`, error); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private updateLastActivity(): void { |
| 97 | + this.storageService.set(SessionService.LAST_ACTIVITY_KEY, Date.now()); |
| 98 | + } |
| 99 | + |
| 100 | + cleanup(): void { |
| 101 | + this.appStateSubscription?.remove(); |
| 102 | + } |
| 103 | +} |
0 commit comments