Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce names for singleton imports #8639

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/database/manager/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if (__DEV__) {
logger.silence();
}

class DatabaseManager {
class DatabaseManagerSingleton {
public appDatabase?: AppDatabase;
public serverDatabases: ServerDatabases = {};
private readonly appModels: Models;
Expand Down Expand Up @@ -399,4 +399,5 @@ class DatabaseManager {
};
}

export default new DatabaseManager();
const DatabaseManager = new DatabaseManagerSingleton();
export default DatabaseManager;
5 changes: 3 additions & 2 deletions app/database/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type {AppDatabase, CreateServerDatabaseArgs, RegisterServerDatabaseArgs,
const {SERVERS} = MM_TABLES.APP;
const APP_DATABASE = 'app';

class DatabaseManager {
class DatabaseManagerSingleton {
public appDatabase?: AppDatabase;
public serverDatabases: ServerDatabases = {};
private readonly appModels: Models;
Expand Down Expand Up @@ -554,4 +554,5 @@ if (!__DEV__) {
logger.silence();
}

export default new DatabaseManager();
const DatabaseManager = new DatabaseManagerSingleton();
export default DatabaseManager;
5 changes: 3 additions & 2 deletions app/init/managed_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {getIOSAppGroupDetails} from '@utils/mattermost_managed';

const PROMPT_IN_APP_PIN_CODE_AFTER = toMilliseconds({minutes: 5});

class ManagedApp {
class ManagedAppSingleton {
backgroundSince = 0;
enabled = false;
inAppPinCode = false;
Expand Down Expand Up @@ -199,4 +199,5 @@ class ManagedApp {
};
}

export default new ManagedApp();
const ManagedApp = new ManagedAppSingleton();
export default ManagedApp;
5 changes: 3 additions & 2 deletions app/init/push_notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {isMainActivity, isTablet} from '@utils/helpers';
import {logDebug, logInfo} from '@utils/log';
import {convertToNotificationData} from '@utils/notification';

class PushNotifications {
class PushNotificationsSingleton {
configured = false;
subscriptions?: EmitterSubscription[];

Expand Down Expand Up @@ -333,4 +333,5 @@ class PushNotifications {
};
}

export default new PushNotifications();
const PushNotifications = new PushNotificationsSingleton();
export default PushNotifications;
2 changes: 1 addition & 1 deletion app/managers/apps_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {getChannelById} from '@queries/servers/channel';
import {getConfig} from '@queries/servers/system';
import {logDebug} from '@utils/log';

import {AppsManager as AppsManagerClass} from './apps_manager';
import {AppsManagerSingleton as AppsManagerClass} from './apps_manager';

import type ChannelModel from '@typings/database/models/servers/channel';

Expand Down
5 changes: 3 additions & 2 deletions app/managers/apps_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const emptyBindings: AppBinding[] = [];

const normalizeBindings = (bindings: AppBinding[]) => bindings.reduce<AppBinding[]>((acc, v) => (v.bindings ? acc.concat(v.bindings) : acc), []);

export class AppsManager {
export class AppsManagerSingleton {
private enabled: {[serverUrl: string]: BehaviorSubject<boolean>} = {};

private bindings: {[serverUrl: string]: BehaviorSubject<AppBinding[]>} = {};
Expand Down Expand Up @@ -209,4 +209,5 @@ export class AppsManager {
};
}

export default new AppsManager();
const AppsManager = new AppsManagerSingleton();
export default AppsManager;
20 changes: 10 additions & 10 deletions app/managers/draft_upload_manager/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {exportedForTesting} from '.';
import type ServerDataOperator from '@database/operator/server_data_operator';
import type {ClientResponse, ProgressPromise} from '@mattermost/react-native-network-client';

const {DraftUploadManager} = exportedForTesting;
const {DraftUploadManagerSingleton} = exportedForTesting;

const url = 'baseHandler.test.com';
const mockClient = TestHelper.createClient();
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('draft upload manager', () => {
});

it('File is uploaded and stored', async () => {
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();
const uploadMocks = mockUpload();

const fileClientId = 'clientId';
Expand All @@ -103,7 +103,7 @@ describe('draft upload manager', () => {
});

it('Progress is not stored on progress, but stored on fail', async () => {
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();
const uploadMocks = mockUpload();

const fileClientId = 'clientId';
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('draft upload manager', () => {
const spyNow = jest.spyOn(Date, 'now');
spyNow.mockImplementation(() => now);
AppState.currentState = 'active';
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();

const progressFunc: {[fileUrl: string] : ((fractionCompleted: number, bytesRead?: number | null | undefined) => void)} = {};
const cancel = jest.fn();
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('draft upload manager', () => {
});

it('Error on complete: Received wrong response code', async () => {
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();
const uploadMocks = mockUpload();

const fileClientId = 'clientId';
Expand All @@ -280,7 +280,7 @@ describe('draft upload manager', () => {
});

it('Error on complete: Received no data', async () => {
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();
const uploadMocks = mockUpload();

const clientId = 'clientId';
Expand All @@ -304,7 +304,7 @@ describe('draft upload manager', () => {
});

it('Error on complete: Received no file info', async () => {
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();
const uploadMocks = mockUpload();

const clientId = 'clientId';
Expand All @@ -328,7 +328,7 @@ describe('draft upload manager', () => {
});

it('Progress handler', async () => {
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();
const uploadMocks = mockUpload();

const clientId = 'clientId';
Expand Down Expand Up @@ -369,7 +369,7 @@ describe('draft upload manager', () => {
});

it('Error handler: normal error', async () => {
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();
const uploadMocks = mockUpload();

const clientId = 'clientId';
Expand Down Expand Up @@ -405,7 +405,7 @@ describe('draft upload manager', () => {
});

it('Error handler: complete error', async () => {
const manager = new DraftUploadManager();
const manager = new DraftUploadManagerSingleton();
const uploadMocks = mockUpload();

const clientId = 'clientId';
Expand Down
7 changes: 4 additions & 3 deletions app/managers/draft_upload_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type FileHandler = {
};
}

class DraftUploadManager {
class DraftUploadManagerSingleton {
private handlers: FileHandler = {};
private previousAppState: AppStateStatus;

Expand Down Expand Up @@ -186,8 +186,9 @@ class DraftUploadManager {
};
}

export default new DraftUploadManager();
const DraftUploadManager = new DraftUploadManagerSingleton();
export default DraftUploadManager;

export const exportedForTesting = {
DraftUploadManager,
DraftUploadManagerSingleton,
};
5 changes: 3 additions & 2 deletions app/managers/global_event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type LinkingCallbackArg = {url: string};

const splitViewEmitter = new NativeEventEmitter(RNUtils);

class GlobalEventHandler {
class GlobalEventHandlerSingleton {
JavascriptAndNativeErrorHandler: jsAndNativeErrorHandler | undefined;

constructor() {
Expand Down Expand Up @@ -112,4 +112,5 @@ class GlobalEventHandler {
};
}

export default new GlobalEventHandler();
const GlobalEventHandler = new GlobalEventHandlerSingleton();
export default GlobalEventHandler;
5 changes: 3 additions & 2 deletions app/managers/integrations_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ServerIntegrationsManager {
}
}

class IntegrationsManager {
class IntegrationsManagerSingleton {
private serverManagers: {[serverUrl: string]: ServerIntegrationsManager | undefined} = {};
public getManager(serverUrl: string): ServerIntegrationsManager {
if (!this.serverManagers[serverUrl]) {
Expand All @@ -72,4 +72,5 @@ class IntegrationsManager {
}
}

export default new IntegrationsManager();
const IntegrationsManager = new IntegrationsManagerSingleton();
export default IntegrationsManager;
5 changes: 3 additions & 2 deletions app/managers/network_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const messages = defineMessages({
},
});

class NetworkManager {
class NetworkManagerSingleton {
private clients: Record<string, Client> = {};

private intl = getIntlShape();
Expand Down Expand Up @@ -174,4 +174,5 @@ class NetworkManager {
};
}

export default new NetworkManager();
const NetworkManager = new NetworkManagerSingleton();
export default NetworkManager;
2 changes: 1 addition & 1 deletion app/managers/performance_metrics_manager/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {getBaseReportRequest} from './test_utils';

import {testExports} from '.';

const PerformanceMetricsManagerClass = testExports.PerformanceMetricsManager;
const PerformanceMetricsManagerClass = testExports.PerformanceMetricsManagerSingleton;
const {
RETRY_TIME,
MAX_RETRIES,
Expand Down
7 changes: 4 additions & 3 deletions app/managers/performance_metrics_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type MetricName = 'mobile_channel_switch' |
const RETRY_TIME = 100;
const MAX_RETRIES = 3;

class PerformanceMetricsManager {
class PerformanceMetricsManagerSingleton {
private target: Target;
private batchers: {[serverUrl: string]: Batcher} = {};
private lastAppStateIsActive = AppState.currentState === 'active';
Expand Down Expand Up @@ -136,9 +136,10 @@ class PerformanceMetricsManager {
}

export const testExports = {
PerformanceMetricsManager,
PerformanceMetricsManagerSingleton,
RETRY_TIME,
MAX_RETRIES,
};

export default new PerformanceMetricsManager();
const PerformanceMetricsManager = new PerformanceMetricsManagerSingleton();
export default PerformanceMetricsManager;
2 changes: 1 addition & 1 deletion app/managers/session_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import WebsocketManager from '@managers/websocket_manager';
import {deleteFileCache, deleteFileCacheByDir} from '@utils/file';
import {isMainActivity} from '@utils/helpers';

import {SessionManager as SessionManagerClass} from './session_manager';
import {SessionManagerSingleton as SessionManagerClass} from './session_manager';

jest.mock('@react-native-cookies/cookies', () => ({
get: jest.fn(),
Expand Down
5 changes: 3 additions & 2 deletions app/managers/session_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type LogoutCallbackArg = {
removeServer: boolean;
}

export class SessionManager {
export class SessionManagerSingleton {
private previousAppState: AppStateStatus;
private scheduling = false;
private terminatingSessionUrl = new Set<string>();
Expand Down Expand Up @@ -207,4 +207,5 @@ export class SessionManager {
};
}

export default new SessionManager();
const SessionManager = new SessionManagerSingleton();
export default SessionManager;
5 changes: 3 additions & 2 deletions app/managers/websocket_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {logError} from '@utils/log';
const WAIT_TO_CLOSE = toMilliseconds({seconds: 15});
const WAIT_UNTIL_NEXT = toMilliseconds({seconds: 5});

class WebsocketManager {
class WebsocketManagerSingleton {
private connectedSubjects: {[serverUrl: string]: BehaviorSubject<WebsocketConnectedState>} = {};

private clients: Record<string, WebSocketClient> = {};
Expand Down Expand Up @@ -292,4 +292,5 @@ class WebsocketManager {
};
}

export default new WebsocketManager();
const WebsocketManager = new WebsocketManagerSingleton();
export default WebsocketManager;
5 changes: 3 additions & 2 deletions app/store/ephemeral_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {toMilliseconds} from '@utils/datetime';

const TIME_TO_CLEAR_WEBSOCKET_ACTIONS = toMilliseconds({seconds: 30});

class EphemeralStore {
class EphemeralStoreSingleton {
theme: Theme | undefined;
creatingChannel = false;
creatingDMorGMTeammates: string[] = [];
Expand Down Expand Up @@ -281,4 +281,5 @@ class EphemeralStore {
};
}

export default new EphemeralStore();
const EphemeralStore = new EphemeralStoreSingleton();
export default EphemeralStore;
5 changes: 3 additions & 2 deletions app/store/navigation_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {BehaviorSubject} from 'rxjs';

import type {AvailableScreens} from '@typings/screens/navigation';

class NavigationStore {
class NavigationStoreSingleton {
private screensInStack: AvailableScreens[] = [];
private modalsInStack: AvailableScreens[] = [];
private visibleTab = 'Home';
Expand Down Expand Up @@ -144,4 +144,5 @@ class NavigationStore {
};
}

export default new NavigationStore();
const NavigationStore = new NavigationStoreSingleton();
export default NavigationStore;
5 changes: 3 additions & 2 deletions app/utils/error_handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import {logWarning} from './log';

class JavascriptAndNativeErrorHandler {
class JavascriptAndNativeErrorHandlerSingleton {
initializeErrorHandling = () => {
initializeSentry();
setJSExceptionHandler(this.errorHandler, false);
Expand Down Expand Up @@ -61,4 +61,5 @@ class JavascriptAndNativeErrorHandler {
};
}

export default new JavascriptAndNativeErrorHandler();
const JavascriptAndNativeErrorHandler = new JavascriptAndNativeErrorHandlerSingleton();
export default JavascriptAndNativeErrorHandler;
5 changes: 3 additions & 2 deletions test/test_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {APIClientInterface} from '@mattermost/react-native-network-client';

const DEFAULT_LOCALE = 'en';

class TestHelper {
class TestHelperSingleton {
basicClient: Client | null;
basicUser: UserProfile | null;
basicTeam: Team | null;
Expand Down Expand Up @@ -733,4 +733,5 @@ class TestHelper {
tick = () => new Promise((r) => setImmediate(r));
}

export default new TestHelper();
const TestHelper = new TestHelperSingleton();
export default TestHelper;