-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathindex.ts
79 lines (67 loc) · 2.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type {UserInDatabase} from '@shared/schema/Auth';
import ClientGatewayService from './interfaces/clientGatewayService';
import FeedService from './feedService';
import HistoryService from './historyService';
import NotificationService from './notificationService';
import SettingService from './settingService';
import TaxonomyService from './taxonomyService';
import TorrentService from './torrentService';
import DelugeClientGatewayService from './Deluge/clientGatewayService';
import QBittorrentClientGatewayService from './qBittorrent/clientGatewayService';
import RTorrentClientGatewayService from './rTorrent/clientGatewayService';
import TransmissionClientGatewayService from './Transmission/clientGatewayService';
export interface ServiceInstances {
clientGatewayService: ClientGatewayService;
feedService: FeedService;
historyService: HistoryService;
notificationService: NotificationService;
settingService: SettingService;
taxonomyService: TaxonomyService;
torrentService: TorrentService;
}
const serviceInstances: Record<string, ServiceInstances> = {};
const newClientGatewayService = (user: UserInDatabase): ClientGatewayService => {
switch (user.client.client) {
case 'Deluge':
return new DelugeClientGatewayService(user);
case 'qBittorrent':
return new QBittorrentClientGatewayService(user);
case 'rTorrent':
return new RTorrentClientGatewayService(user);
case 'Transmission':
return new TransmissionClientGatewayService(user);
}
};
export const getAllServices = ({_id}: UserInDatabase) => {
return serviceInstances[_id];
};
export const destroyUserServices = (userId: UserInDatabase['_id']) => {
const userServiceInstances = serviceInstances[userId];
delete serviceInstances[userId];
Object.keys(userServiceInstances).forEach((key) => {
const serviceName = key as keyof ServiceInstances;
userServiceInstances[serviceName].destroy();
});
};
export const bootstrapServicesForUser = (user: UserInDatabase) => {
const {_id} = user;
if (serviceInstances[_id] != null) {
destroyUserServices(_id);
}
const userServiceInstances = {
clientGatewayService: newClientGatewayService(user),
feedService: new FeedService(user),
historyService: new HistoryService(user),
notificationService: new NotificationService(user),
settingService: new SettingService(user),
taxonomyService: new TaxonomyService(user),
torrentService: new TorrentService(user),
};
Object.keys(userServiceInstances).forEach((key) => {
const serviceName = key as keyof ServiceInstances;
if (userServiceInstances[serviceName] != null) {
userServiceInstances[serviceName].updateServices(userServiceInstances);
}
});
serviceInstances[_id] = userServiceInstances;
};