From e40e7de8083ecfe220ef7f94f7447d560f822272 Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Tue, 26 Apr 2022 07:28:43 +1000 Subject: [PATCH] [EC-159] [BEEEP] Remove factory providers in Angular DI (#775) --- angular/src/services/jslib-services.module.ts | 182 +++++------------- common/src/services/notifications.service.ts | 4 +- common/src/services/vaultTimeout.service.ts | 4 +- .../services/electronPlatformUtils.service.ts | 7 +- 4 files changed, 58 insertions(+), 139 deletions(-) diff --git a/angular/src/services/jslib-services.module.ts b/angular/src/services/jslib-services.module.ts index 8d8aa85bea..68b527bc95 100644 --- a/angular/src/services/jslib-services.module.ts +++ b/angular/src/services/jslib-services.module.ts @@ -1,4 +1,4 @@ -import { Injector, LOCALE_ID, NgModule } from "@angular/core"; +import { InjectionToken, Injector, LOCALE_ID, NgModule } from "@angular/core"; import { ApiService as ApiServiceAbstraction } from "jslib-common/abstractions/api.service"; import { AppIdService as AppIdServiceAbstraction } from "jslib-common/abstractions/appId.service"; @@ -82,20 +82,49 @@ import { PasswordRepromptService } from "./passwordReprompt.service"; import { UnauthGuardService } from "./unauth-guard.service"; import { ValidationService } from "./validation.service"; +export const WINDOW = new InjectionToken("WINDOW"); +export const SECURE_STORAGE = new InjectionToken("SECURE_STORAGE"); +export const STATE_FACTORY = new InjectionToken("STATE_FACTORY"); +export const STATE_SERVICE_USE_CACHE = new InjectionToken("STATE_SERVICE_USE_CACHE"); +export const LOGOUT_CALLBACK = new InjectionToken<(expired: boolean, userId?: string) => void>( + "LOGOUT_CALLBACK" +); +export const LOCKED_CALLBACK = new InjectionToken<() => void>("LOCKED_CALLBACK"); +export const CLIENT_TYPE = new InjectionToken("CLIENT_TYPE"); + @NgModule({ declarations: [], providers: [ - { provide: "WINDOW", useValue: window }, - { - provide: LOCALE_ID, - useFactory: (i18nService: I18nServiceAbstraction) => i18nService.translationLocale, - deps: [I18nServiceAbstraction], - }, ValidationService, AuthGuardService, UnauthGuardService, LockGuardService, ModalService, + { provide: WINDOW, useValue: window }, + { + provide: LOCALE_ID, + useFactory: (i18nService: I18nServiceAbstraction) => i18nService.translationLocale, + deps: [I18nServiceAbstraction], + }, + { + provide: STATE_FACTORY, + useValue: new StateFactory(GlobalState, Account), + }, + { + provide: STATE_SERVICE_USE_CACHE, + useValue: true, + }, + { + provide: LOGOUT_CALLBACK, + useFactory: + (messagingService: MessagingServiceAbstraction) => (expired: boolean, userId?: string) => + messagingService.send("logout", { expired: expired, userId: userId }), + deps: [MessagingServiceAbstraction], + }, + { + provide: LOCKED_CALLBACK, + useValue: null, + }, { provide: AppIdServiceAbstraction, useClass: AppIdService, @@ -207,26 +236,13 @@ import { ValidationService } from "./validation.service"; }, { provide: ApiServiceAbstraction, - useFactory: ( - tokenService: TokenServiceAbstraction, - platformUtilsService: PlatformUtilsServiceAbstraction, - environmentService: EnvironmentServiceAbstraction, - messagingService: MessagingServiceAbstraction, - appIdService: AppIdServiceAbstraction - ) => - new ApiService( - tokenService, - platformUtilsService, - environmentService, - appIdService, - async (expired: boolean) => messagingService.send("logout", { expired: expired }) - ), + useClass: ApiService, deps: [ TokenServiceAbstraction, PlatformUtilsServiceAbstraction, EnvironmentServiceAbstraction, - MessagingServiceAbstraction, AppIdServiceAbstraction, + LOGOUT_CALLBACK, ], }, { @@ -236,39 +252,7 @@ import { ValidationService } from "./validation.service"; }, { provide: SyncServiceAbstraction, - useFactory: ( - apiService: ApiServiceAbstraction, - settingsService: SettingsServiceAbstraction, - folderService: FolderServiceAbstraction, - cipherService: CipherServiceAbstraction, - cryptoService: CryptoServiceAbstraction, - collectionService: CollectionServiceAbstraction, - messagingService: MessagingServiceAbstraction, - policyService: PolicyServiceAbstraction, - sendService: SendServiceAbstraction, - logService: LogService, - keyConnectorService: KeyConnectorServiceAbstraction, - stateService: StateServiceAbstraction, - organizationService: OrganizationServiceAbstraction, - providerService: ProviderServiceAbstraction - ) => - new SyncService( - apiService, - settingsService, - folderService, - cipherService, - cryptoService, - collectionService, - messagingService, - policyService, - sendService, - logService, - keyConnectorService, - stateService, - organizationService, - providerService, - async (expired: boolean) => messagingService.send("logout", { expired: expired }) - ), + useClass: SyncService, deps: [ ApiServiceAbstraction, SettingsServiceAbstraction, @@ -284,6 +268,7 @@ import { ValidationService } from "./validation.service"; StateServiceAbstraction, OrganizationServiceAbstraction, ProviderServiceAbstraction, + LOGOUT_CALLBACK, ], }, { provide: BroadcasterServiceAbstraction, useClass: BroadcasterService }, @@ -294,35 +279,7 @@ import { ValidationService } from "./validation.service"; }, { provide: VaultTimeoutServiceAbstraction, - useFactory: ( - cipherService: CipherServiceAbstraction, - folderService: FolderServiceAbstraction, - collectionService: CollectionServiceAbstraction, - cryptoService: CryptoServiceAbstraction, - platformUtilsService: PlatformUtilsServiceAbstraction, - messagingService: MessagingServiceAbstraction, - searchService: SearchServiceAbstraction, - tokenService: TokenServiceAbstraction, - policyService: PolicyServiceAbstraction, - keyConnectorService: KeyConnectorServiceAbstraction, - stateService: StateServiceAbstraction - ) => - new VaultTimeoutService( - cipherService, - folderService, - collectionService, - cryptoService, - platformUtilsService, - messagingService, - searchService, - tokenService, - policyService, - keyConnectorService, - stateService, - null, - async (userId?: string) => - messagingService.send("logout", { expired: false, userId: userId }) - ), + useClass: VaultTimeoutService, deps: [ CipherServiceAbstraction, FolderServiceAbstraction, @@ -335,42 +292,26 @@ import { ValidationService } from "./validation.service"; PolicyServiceAbstraction, KeyConnectorServiceAbstraction, StateServiceAbstraction, + LOCKED_CALLBACK, + LOGOUT_CALLBACK, ], }, { provide: StateServiceAbstraction, - useFactory: ( - storageService: StorageServiceAbstraction, - secureStorageService: StorageServiceAbstraction, - logService: LogService, - stateMigrationService: StateMigrationServiceAbstraction - ) => - new StateService( - storageService, - secureStorageService, - logService, - stateMigrationService, - new StateFactory(GlobalState, Account) - ), + useClass: StateService, deps: [ StorageServiceAbstraction, - "SECURE_STORAGE", + SECURE_STORAGE, LogService, StateMigrationServiceAbstraction, + STATE_FACTORY, + STATE_SERVICE_USE_CACHE, ], }, { provide: StateMigrationServiceAbstraction, - useFactory: ( - storageService: StorageServiceAbstraction, - secureStorageService: StorageServiceAbstraction - ) => - new StateMigrationService( - storageService, - secureStorageService, - new StateFactory(GlobalState, Account) - ), - deps: [StorageServiceAbstraction, "SECURE_STORAGE"], + useClass: StateMigrationService, + deps: [StorageServiceAbstraction, SECURE_STORAGE, STATE_FACTORY], }, { provide: ExportServiceAbstraction, @@ -389,33 +330,14 @@ import { ValidationService } from "./validation.service"; }, { provide: NotificationsServiceAbstraction, - useFactory: ( - syncService: SyncServiceAbstraction, - appIdService: AppIdServiceAbstraction, - apiService: ApiServiceAbstraction, - vaultTimeoutService: VaultTimeoutServiceAbstraction, - environmentService: EnvironmentServiceAbstraction, - messagingService: MessagingServiceAbstraction, - logService: LogService, - stateService: StateServiceAbstraction - ) => - new NotificationsService( - syncService, - appIdService, - apiService, - vaultTimeoutService, - environmentService, - async () => messagingService.send("logout", { expired: true }), - logService, - stateService - ), + useClass: NotificationsService, deps: [ SyncServiceAbstraction, AppIdServiceAbstraction, ApiServiceAbstraction, VaultTimeoutServiceAbstraction, EnvironmentServiceAbstraction, - MessagingServiceAbstraction, + LOGOUT_CALLBACK, LogService, StateServiceAbstraction, ], @@ -423,7 +345,7 @@ import { ValidationService } from "./validation.service"; { provide: CryptoFunctionServiceAbstraction, useClass: WebCryptoFunctionService, - deps: ["WINDOW"], + deps: [WINDOW], }, { provide: EventServiceAbstraction, diff --git a/common/src/services/notifications.service.ts b/common/src/services/notifications.service.ts index d4e3fef475..f3d8411587 100644 --- a/common/src/services/notifications.service.ts +++ b/common/src/services/notifications.service.ts @@ -31,7 +31,7 @@ export class NotificationsService implements NotificationsServiceAbstraction { private apiService: ApiService, private vaultTimeoutService: VaultTimeoutService, private environmentService: EnvironmentService, - private logoutCallback: () => Promise, + private logoutCallback: (expired: boolean) => Promise, private logService: LogService, private stateService: StateService ) { @@ -169,7 +169,7 @@ export class NotificationsService implements NotificationsServiceAbstraction { break; case NotificationType.LogOut: if (isAuthenticated) { - this.logoutCallback(); + this.logoutCallback(true); } break; case NotificationType.SyncSendCreate: diff --git a/common/src/services/vaultTimeout.service.ts b/common/src/services/vaultTimeout.service.ts index a6513d65b3..0247c2264c 100644 --- a/common/src/services/vaultTimeout.service.ts +++ b/common/src/services/vaultTimeout.service.ts @@ -29,7 +29,7 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction { private keyConnectorService: KeyConnectorService, private stateService: StateService, private lockedCallback: (userId?: string) => Promise = null, - private loggedOutCallback: (userId?: string) => Promise = null + private loggedOutCallback: (expired: boolean, userId?: string) => Promise = null ) {} init(checkOnInterval: boolean) { @@ -116,7 +116,7 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction { async logOut(userId?: string): Promise { if (this.loggedOutCallback != null) { - await this.loggedOutCallback(userId); + await this.loggedOutCallback(false, userId); } } diff --git a/electron/src/services/electronPlatformUtils.service.ts b/electron/src/services/electronPlatformUtils.service.ts index c80218d2a0..3774f3c37a 100644 --- a/electron/src/services/electronPlatformUtils.service.ts +++ b/electron/src/services/electronPlatformUtils.service.ts @@ -11,17 +11,14 @@ import { ThemeType } from "jslib-common/enums/themeType"; import { isDev, isMacAppStore } from "../utils"; export class ElectronPlatformUtilsService implements PlatformUtilsService { - private clientType: ClientType; private deviceCache: DeviceType = null; constructor( protected i18nService: I18nService, private messagingService: MessagingService, - private isDesktopApp: boolean, + private clientType: ClientType.Desktop | ClientType.DirectoryConnector, private stateService: StateService - ) { - this.clientType = isDesktopApp ? ClientType.Desktop : ClientType.DirectoryConnector; - } + ) {} getDevice(): DeviceType { if (!this.deviceCache) {