mattermost-mobile-upstream/app/init/app.ts
Guillermo Vayá 6b0618d043 feat(drafts): Phase 4.4 — wire DraftSyncManager lifecycle teardown
Connect the manager to the app lifecycle so late HTTP completions can never
touch a destroyed database.

- app/init/app.ts: initialize DraftSyncManager for every server after pending
  ephemeral wipes finish and before WebSocket startup.
- actions/local/session/index.ts: invalidate(serverUrl) at the start of
  terminateSession, before client invalidation and database destruction
  (un-flushed outbox intent is intentionally discarded on logout).
- managers/ephemeral_mode_manager: invalidate before wipeServerArtifacts and
  re-initialize after the server is re-added post-wipe.

Checks: npm run tsc clean; eslint --quiet clean; 87 Jest tests pass across the
session, ephemeral, and manager suites (no regressions).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-04 12:52:58 +02:00

84 lines
3.0 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {CallsManager} from '@calls/calls_manager';
import DatabaseManager from '@database/manager';
import CallsNative from '@init/calls_native';
import {getAllServerCredentials} from '@init/credentials';
import ManagedApp from '@init/managed_app';
import PushNotifications from '@init/push_notifications';
import DraftSyncManager from '@managers/draft_sync_manager';
import EphemeralModeManager from '@managers/ephemeral_mode_manager';
import GlobalEventHandler from '@managers/global_event_handler';
import NetworkManager from '@managers/network_manager';
import SecurityManager from '@managers/security_manager';
import SessionManager from '@managers/session_manager';
import WebsocketManager from '@managers/websocket_manager';
import EphemeralStore from '@store/ephemeral_store';
import {NavigationStore} from '@store/navigation_store';
// Controls whether the main initialization (database, etc...) is done, either on app launch
// or on the Share Extension, for example.
let baseAppInitialized = false;
let serverCredentials: ServerCredential[] = [];
// Fallback Polyfill for Promise.allSettle
Promise.allSettled = Promise.allSettled || (<T>(promises: Array<Promise<T>>) => Promise.all(
promises.map((p) => p.
then((value) => ({
status: 'fulfilled',
value,
})).
catch((reason) => ({
status: 'rejected',
reason,
})),
),
));
export async function initialize() {
if (!baseAppInitialized) {
baseAppInitialized = true;
serverCredentials = await getAllServerCredentials();
const serverUrls = serverCredentials.map((credential) => credential.serverUrl);
await DatabaseManager.init(serverUrls);
await NetworkManager.init(serverCredentials);
// EphemeralModeManager init runs before WS init so any pending wipes
// complete before WebSocket clients start populating server databases.
await EphemeralModeManager.init(serverCredentials);
// Draft sync initializes after any pending ephemeral wipes finish and before
// WebSocket startup, so it never races a database that is being destroyed.
await Promise.all(serverUrls.map((serverUrl) => DraftSyncManager.initialize(serverUrl)));
await WebsocketManager.init(serverCredentials);
}
NavigationStore.reset();
EphemeralStore.setCurrentThreadId('');
EphemeralStore.setProcessingNotification('');
await SecurityManager.init();
GlobalEventHandler.init();
ManagedApp.init();
SessionManager.init();
CallsManager.initialize();
CallsNative.init();
PushNotifications.init(serverCredentials.length > 0);
}
export function cleanup() {
ManagedApp.cleanup();
GlobalEventHandler.cleanup();
SecurityManager.cleanup();
SessionManager.cleanup();
CallsManager.cleanup();
CallsNative.cleanup();
PushNotifications.cleanup();
EphemeralModeManager.cleanup();
}