1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/src/backend/model/ObjectManagers.ts

237 lines
7.7 KiB
TypeScript
Raw Normal View History

import {IUserManager} from './database/interfaces/IUserManager';
import {IGalleryManager} from './database/interfaces/IGalleryManager';
import {ISearchManager} from './database/interfaces/ISearchManager';
import {SQLConnection} from './database/sql/SQLConnection';
import {ISharingManager} from './database/interfaces/ISharingManager';
2018-03-31 03:30:30 +08:00
import {Logger} from '../Logger';
import {IIndexingManager} from './database/interfaces/IIndexingManager';
import {IPersonManager} from './database/interfaces/IPersonManager';
import {IVersionManager} from './database/interfaces/IVersionManager';
2019-12-24 19:22:25 +08:00
import {IJobManager} from './database/interfaces/IJobManager';
import {LocationManager} from './database/LocationManager';
import {IAlbumManager} from './database/interfaces/IAlbumManager';
import {JobManager} from './jobs/JobManager';
import {IPreviewManager} from './database/interfaces/IPreviewManager';
import {ParentDirectoryDTO} from '../../common/entities/DirectoryDTO';
import {IObjectManager} from './database/interfaces/IObjectManager';
2016-04-22 19:23:44 +08:00
const LOG_TAG = '[ObjectManagers]';
export class ObjectManagers {
2016-05-09 23:04:56 +08:00
private static instance: ObjectManagers = null;
private readonly managers: IObjectManager[];
private galleryManager: IGalleryManager;
private userManager: IUserManager;
private searchManager: ISearchManager;
private sharingManager: ISharingManager;
private indexingManager: IIndexingManager;
private personManager: IPersonManager;
private previewManager: IPreviewManager;
private versionManager: IVersionManager;
private jobManager: IJobManager;
private locationManager: LocationManager;
private albumManager: IAlbumManager;
constructor() {
this.managers = [];
}
get VersionManager(): IVersionManager {
return this.versionManager;
}
set VersionManager(value: IVersionManager) {
if (this.versionManager) {
this.managers.splice(this.managers.indexOf(this.versionManager), 1);
}
this.versionManager = value;
this.managers.push(this.versionManager);
}
2019-01-14 00:38:39 +08:00
get LocationManager(): LocationManager {
return this.locationManager;
}
set LocationManager(value: LocationManager) {
if (this.locationManager) {
this.managers.splice(this.managers.indexOf(this.locationManager), 1);
}
this.locationManager = value;
this.managers.push(this.locationManager);
}
get AlbumManager(): IAlbumManager {
return this.albumManager;
}
set AlbumManager(value: IAlbumManager) {
if (this.albumManager) {
this.managers.splice(this.managers.indexOf(this.albumManager), 1);
}
this.albumManager = value;
this.managers.push(this.albumManager);
}
2019-01-14 00:38:39 +08:00
get PersonManager(): IPersonManager {
return this.personManager;
2019-01-14 00:38:39 +08:00
}
set PersonManager(value: IPersonManager) {
if (this.personManager) {
this.managers.splice(this.managers.indexOf(this.personManager), 1);
}
this.personManager = value;
this.managers.push(this.personManager);
2019-01-14 00:38:39 +08:00
}
get PreviewManager(): IPreviewManager {
return this.previewManager;
}
set PreviewManager(value: IPreviewManager) {
if (this.previewManager) {
this.managers.splice(this.managers.indexOf(this.previewManager), 1);
}
this.previewManager = value;
this.managers.push(this.previewManager);
}
2016-04-22 19:23:44 +08:00
2017-07-26 03:09:37 +08:00
get IndexingManager(): IIndexingManager {
return this.indexingManager;
}
2017-07-26 03:09:37 +08:00
set IndexingManager(value: IIndexingManager) {
if (this.indexingManager) {
this.managers.splice(this.managers.indexOf(this.indexingManager), 1);
}
this.indexingManager = value;
this.managers.push(this.indexingManager);
}
2016-05-09 23:04:56 +08:00
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
get GalleryManager(): IGalleryManager {
return this.galleryManager;
}
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
set GalleryManager(value: IGalleryManager) {
if (this.galleryManager) {
this.managers.splice(this.managers.indexOf(this.galleryManager), 1);
}
this.galleryManager = value;
this.managers.push(this.galleryManager);
}
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
get UserManager(): IUserManager {
return this.userManager;
}
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
set UserManager(value: IUserManager) {
if (this.userManager) {
this.managers.splice(this.managers.indexOf(this.userManager), 1);
}
this.userManager = value;
this.managers.push(this.userManager);
}
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
get SearchManager(): ISearchManager {
return this.searchManager;
}
2016-05-05 00:34:54 +08:00
2017-07-04 01:17:49 +08:00
set SearchManager(value: ISearchManager) {
if (this.searchManager) {
this.managers.splice(this.managers.indexOf(this.searchManager), 1);
}
this.searchManager = value;
this.managers.push(this.searchManager);
}
2016-05-05 00:34:54 +08:00
2017-07-04 01:17:49 +08:00
get SharingManager(): ISharingManager {
return this.sharingManager;
2017-07-04 01:17:49 +08:00
}
set SharingManager(value: ISharingManager) {
if (this.sharingManager) {
this.managers.splice(this.managers.indexOf(this.sharingManager), 1);
}
this.sharingManager = value;
this.managers.push(this.sharingManager);
2017-07-04 01:17:49 +08:00
}
2019-12-24 19:22:25 +08:00
get JobManager(): IJobManager {
return this.jobManager;
2019-07-28 04:56:12 +08:00
}
2019-12-24 19:22:25 +08:00
set JobManager(value: IJobManager) {
if (this.jobManager) {
this.managers.splice(this.managers.indexOf(this.jobManager), 1);
}
this.jobManager = value;
this.managers.push(this.jobManager);
2019-07-28 04:56:12 +08:00
}
public static getInstance(): ObjectManagers {
if (this.instance === null) {
this.instance = new ObjectManagers();
2019-01-14 00:38:39 +08:00
}
return this.instance;
2019-01-14 00:38:39 +08:00
}
public static async reset(): Promise<void> {
Logger.silly(LOG_TAG, 'Object manager reset begin');
2021-01-04 17:32:19 +08:00
if (ObjectManagers.getInstance().IndexingManager &&
ObjectManagers.getInstance().IndexingManager.IsSavingInProgress) {
await ObjectManagers.getInstance().IndexingManager.SavingReady;
}
2019-12-24 19:22:25 +08:00
if (ObjectManagers.getInstance().JobManager) {
ObjectManagers.getInstance().JobManager.stopSchedules();
}
2019-01-14 00:38:39 +08:00
await SQLConnection.close();
this.instance = null;
Logger.debug(LOG_TAG, 'Object manager reset');
2019-01-14 00:38:39 +08:00
}
public static async InitMemoryManagers(): Promise<void> {
await ObjectManagers.reset();
this.initManagers('memory');
Logger.debug(LOG_TAG, 'Memory DB inited');
2017-07-26 03:09:37 +08:00
}
public static async InitSQLManagers(): Promise<void> {
await ObjectManagers.reset();
2017-07-26 03:09:37 +08:00
await SQLConnection.init();
this.initManagers('sql');
Logger.debug(LOG_TAG, 'SQL DB inited');
2017-07-26 03:09:37 +08:00
}
private static initManagers(type: 'memory' | 'sql'): void {
ObjectManagers.getInstance().AlbumManager = new (require(`./database/${type}/AlbumManager`).AlbumManager)();
ObjectManagers.getInstance().GalleryManager = new (require(`./database/${type}/GalleryManager`).GalleryManager)();
ObjectManagers.getInstance().IndexingManager = new (require(`./database/${type}/IndexingManager`).IndexingManager)();
ObjectManagers.getInstance().PersonManager = new (require(`./database/${type}/PersonManager`).PersonManager)();
ObjectManagers.getInstance().PreviewManager = new (require(`./database/${type}/PreviewManager`).PreviewManager)();
ObjectManagers.getInstance().SearchManager = new (require(`./database/${type}/SearchManager`).SearchManager)();
ObjectManagers.getInstance().SharingManager = new (require(`./database/${type}/SharingManager`).SharingManager)();
ObjectManagers.getInstance().UserManager = new (require(`./database/${type}/UserManager`).UserManager)();
ObjectManagers.getInstance().VersionManager = new (require(`./database/${type}/VersionManager`).VersionManager)();
ObjectManagers.getInstance().JobManager = new JobManager();
ObjectManagers.getInstance().LocationManager = new LocationManager();
}
public async onDataChange(changedDir: ParentDirectoryDTO = null): Promise<void> {
await this.VersionManager.onNewDataVersion(changedDir);
for (const manager of this.managers) {
if (manager === this.versionManager) {
continue;
}
if (manager.onNewDataVersion) {
await manager.onNewDataVersion(changedDir);
}
}
}
}