Skip to content

Commit

Permalink
refactor: StorageService dependency injected (#9903)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyLnd authored Oct 28, 2020
1 parent 6066420 commit dd70307
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 40 deletions.
5 changes: 2 additions & 3 deletions src/script/backup/BackupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

import type Dexie from 'dexie';
import DexieBatch from 'dexie-batch';
import {container} from 'tsyringe';

import {Logger, getLogger} from 'Util/Logger';

import {StorageSchemata, StorageService} from '../storage';

export class BackupService {
private readonly logger: Logger;
private readonly storageService: StorageService;

static get CONFIG() {
return {
Expand All @@ -35,9 +35,8 @@ export class BackupService {
};
}

constructor(storageService: StorageService) {
constructor(private readonly storageService = container.resolve(StorageService)) {
this.logger = getLogger('BackupService');
this.storageService = storageService;
}

public async exportTable(table: Dexie.Table<any, string>, onProgress: (batch: any[]) => void): Promise<void> {
Expand Down
10 changes: 6 additions & 4 deletions src/script/client/ClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import {container} from 'tsyringe';

import {Logger, getLogger} from 'Util/Logger';

import type {ClientRecord, StorageService} from '../storage';
import type {ClientRecord} from '../storage';
import {StorageService} from '../storage';
import {StorageSchemata} from '../storage/StorageSchemata';
import {APIClient} from '../service/APIClientSingleton';

export class ClientService {
private readonly storageService: StorageService;
private readonly logger: Logger;
private readonly CLIENT_STORE_NAME: string;

Expand All @@ -39,8 +39,10 @@ export class ClientService {
return '/users';
}

constructor(storageService: StorageService, private readonly apiClient = container.resolve(APIClient)) {
this.storageService = storageService;
constructor(
private readonly storageService = container.resolve(StorageService),
private readonly apiClient = container.resolve(APIClient),
) {
this.logger = getLogger('ClientService');

this.CLIENT_STORE_NAME = StorageSchemata.OBJECT_STORE.CLIENTS;
Expand Down
6 changes: 2 additions & 4 deletions src/script/conversation/ConversationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,20 @@ import type {Conversation as ConversationEntity, SerializedConversation} from '.
import type {EventService} from '../event/EventService';
import {MessageCategory} from '../message/MessageCategory';
import {search as fullTextSearch} from '../search/FullTextSearch';
import type {StorageService} from '../storage';
import {StorageService} from '../storage';
import {StorageSchemata} from '../storage/StorageSchemata';
import {APIClient} from '../service/APIClientSingleton';

export class ConversationService {
private readonly eventService: EventService;
private readonly logger: Logger;
private readonly storageService: StorageService;

constructor(
eventService: EventService,
storageService: StorageService,
private readonly storageService = container.resolve(StorageService),
private readonly apiClient = container.resolve(APIClient),
) {
this.eventService = eventService;
this.storageService = storageService;
this.logger = getLogger('ConversationService');
}

Expand Down
3 changes: 2 additions & 1 deletion src/script/event/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import type {Dexie} from 'dexie';
import {container} from 'tsyringe';

import {getLogger, Logger} from 'Util/Logger';

Expand Down Expand Up @@ -50,7 +51,7 @@ export const compareEventsByTime = (eventA: EventRecord, eventB: EventRecord) =>
export class EventService {
logger: Logger;

constructor(public readonly storageService: StorageService) {
constructor(public readonly storageService = container.resolve(StorageService)) {
this.logger = getLogger('EventService');
}

Expand Down
4 changes: 3 additions & 1 deletion src/script/event/EventServiceNoCompound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*
*/

import {container} from 'tsyringe';

import {
EventService,
Includes,
Expand All @@ -35,7 +37,7 @@ type DateComparator = (dateA: Date, dateB: Date) => boolean;
// TODO: This function can be removed once Microsoft Edge's IndexedDB supports compound indices:
// - https://developer.microsoft.com/en-us/microsoft-edge/platform/status/indexeddbarraysandmultientrysupport/
export class EventServiceNoCompound extends EventService {
constructor(storageService: StorageService) {
constructor(storageService = container.resolve(StorageService)) {
super(storageService);
}

Expand Down
7 changes: 4 additions & 3 deletions src/script/event/NotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {APIClient} from '../service/APIClientSingleton';

export class NotificationService {
private readonly logger: Logger;
private readonly storageService: StorageService;
private readonly AMPLIFY_STORE_NAME: string;

static get CONFIG() {
Expand All @@ -42,8 +41,10 @@ export class NotificationService {
};
}

constructor(storageService: StorageService, private readonly apiClient = container.resolve(APIClient)) {
this.storageService = storageService;
constructor(
private readonly storageService = container.resolve(StorageService),
private readonly apiClient = container.resolve(APIClient),
) {
this.logger = getLogger('NotificationService');
this.AMPLIFY_STORE_NAME = StorageSchemata.OBJECT_STORE.AMPLIFY;
}
Expand Down
21 changes: 10 additions & 11 deletions src/script/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ class App {
repositories.giphy = new GiphyRepository(new GiphyService());
repositories.properties = new PropertiesRepository(new PropertiesService(), selfService);
repositories.serverTime = serverTimeHandler;
repositories.storage = new StorageRepository(this.service.storage);
repositories.storage = new StorageRepository();

repositories.cryptography = new CryptographyRepository(new CryptographyService(), repositories.storage);
repositories.client = new ClientRepository(new ClientService(this.service.storage), repositories.cryptography);
repositories.client = new ClientRepository(new ClientService(), repositories.cryptography);
repositories.media = new MediaRepository(new PermissionRepository());
repositories.user = new UserRepository(
new UserService(this.service.storage),
new UserService(),
repositories.asset,
selfService,
repositories.client,
Expand Down Expand Up @@ -297,7 +297,7 @@ class App {
readReceiptMiddleware.processEvent.bind(readReceiptMiddleware),
]);
repositories.backup = new BackupRepository(
new BackupService(this.service.storage),
new BackupService(),
repositories.connection,
repositories.conversation,
repositories.user,
Expand Down Expand Up @@ -341,17 +341,16 @@ class App {
* @returns All services
*/
private _setupServices(encryptedEngine: SQLeetEngine) {
const storageService = new StorageService(encryptedEngine);
const eventService = Runtime.isEdge()
? new EventServiceNoCompound(storageService)
: new EventService(storageService);
container.registerInstance(StorageService, new StorageService(encryptedEngine));
const storageService = container.resolve(StorageService);
const eventService = Runtime.isEdge() ? new EventServiceNoCompound() : new EventService();

return {
asset: container.resolve(AssetService),
conversation: new ConversationService(eventService, storageService),
conversation: new ConversationService(eventService),
event: eventService,
integration: new IntegrationService(),
notification: new NotificationService(storageService),
notification: new NotificationService(),
storage: storageService,
webSocket: new WebSocketService(),
};
Expand Down Expand Up @@ -609,7 +608,7 @@ class App {
}
}

await this.service.storage.init(userEntity.id);
await container.resolve(StorageService).init(userEntity.id);
this.repository.client.init(userEntity);
await this.repository.properties.init(userEntity);
this._checkUserInformation(userEntity);
Expand Down
7 changes: 3 additions & 4 deletions src/script/storage/StorageRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*/

import {Logger, getLogger} from 'Util/Logger';
import {container} from 'tsyringe';

import {StorageSchemata} from '../storage/StorageSchemata';
import type {StorageService} from './StorageService';
import {StorageService} from './StorageService';
import {StorageError} from '../error/StorageError';

type AmplifyRecord = {key: string; value: string};

export class StorageRepository {
private readonly AMPLIFY_STORE_NAME: string;
private readonly logger: Logger;
public readonly storageService: StorageService;

static get CONFIG() {
return {
Expand All @@ -42,8 +42,7 @@ export class StorageRepository {
};
}

constructor(storageService: StorageService) {
this.storageService = storageService;
constructor(public readonly storageService = container.resolve(StorageService)) {
this.logger = getLogger('StorageRepository');
this.AMPLIFY_STORE_NAME = StorageSchemata.OBJECT_STORE.AMPLIFY;
}
Expand Down
2 changes: 2 additions & 0 deletions src/script/storage/StorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {ClientType} from '@wireapp/api-client/dist/client/';
import {IndexedDBEngine} from '@wireapp/store-engine-dexie';
import {SQLeetEngine} from '@wireapp/store-engine-sqleet';
import Dexie from 'dexie';
import {singleton} from 'tsyringe';

import {getEphemeralValue} from 'Util/ephemeralValueStore';
import {Logger, getLogger} from 'Util/Logger';
Expand Down Expand Up @@ -49,6 +50,7 @@ enum DEXIE_CRUD_EVENT {
UPDATING = 'updating',
}

@singleton()
export class StorageService {
// Quickfix to use table name index; can be removed once we have proper db instance typings: https://dexie.org/docs/Typescript#create-a-subclass
public db?: Dexie & DexieObservable & {[tableName: string]: any};
Expand Down
10 changes: 5 additions & 5 deletions src/script/user/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ import {Logger, getLogger} from 'Util/Logger';

import type {User} from '../entity/User';
import {StorageSchemata} from '../storage/StorageSchemata';
import type {StorageService} from '../storage/StorageService';
import {StorageService} from '../storage/StorageService';
import {APIClient} from '../service/APIClientSingleton';

export class UserService {
private readonly logger: Logger;
private readonly storageService: StorageService;
private readonly USER_STORE_NAME: string;

constructor(storageService: StorageService, private readonly apiClient = container.resolve(APIClient)) {
constructor(
private readonly storageService = container.resolve(StorageService),
private readonly apiClient = container.resolve(APIClient),
) {
this.logger = getLogger('UserService');
this.storageService = storageService;

this.USER_STORE_NAME = StorageSchemata.OBJECT_STORE.USERS;
}

Expand Down
10 changes: 7 additions & 3 deletions test/helper/TestFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ import {SearchService} from 'src/script/search/SearchService';
import {AssetRepository} from '../../src/script/assets/AssetRepository';

export class TestFactory {
constructor() {
container.clearInstances();
}
/**
* @returns {Promise<AuthRepository>} The authentication repository.
*/
Expand All @@ -80,7 +83,8 @@ export class TestFactory {
* @returns {Promise<StorageRepository>} The storage repository.
*/
async exposeStorageActors() {
this.storage_service = new StorageService();
container.registerInstance(StorageService, new StorageService());
this.storage_service = container.resolve(StorageService);
if (!this.storage_service.db) {
this.storage_service.init(entities.user.john_doe.id, false);
}
Expand Down Expand Up @@ -178,7 +182,7 @@ export class TestFactory {
this.event_service = new EventService(this.storage_service);
this.event_service_no_compound = new EventServiceNoCompound(this.storage_service);
this.notification_service = new NotificationService(this.storage_service);
this.conversation_service = new ConversationService(this.event_service, this.storage_service);
this.conversation_service = new ConversationService(this.event_service);

this.event_repository = new EventRepository(
this.event_service,
Expand Down Expand Up @@ -255,7 +259,7 @@ export class TestFactory {
await this.exposeTeamActors();
await this.exposeEventActors();

this.conversation_service = new ConversationService(this.event_service, this.storage_service);
this.conversation_service = new ConversationService(this.event_service);

this.propertyRepository = new PropertiesRepository(new PropertiesService(), new SelfService());

Expand Down
4 changes: 3 additions & 1 deletion test/unit_tests/storage/StorageServiceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
*
*/

import {container} from 'tsyringe';

import {StorageSchemata, StorageService} from 'src/script/storage/';
import {StorageError} from 'src/script/error/StorageError';

describe('StorageRepository', () => {
describe('save', () => {
it('does not save "null" values', () => {
const storageService = new StorageService();
const storageService = container.resolve(StorageService);
return storageService
.save(StorageSchemata.OBJECT_STORE.AMPLIFY, 'primary_key', null)
.then(fail)
Expand Down

0 comments on commit dd70307

Please sign in to comment.