-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceRegistry.js
More file actions
58 lines (50 loc) · 1.69 KB
/
ServiceRegistry.js
File metadata and controls
58 lines (50 loc) · 1.69 KB
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
/**
* DownloadLib core module
* Module to manage services (MangaLib, Ranobelib, etc.)
* @module core/ServiceRegistry
* @license MIT
* @author ivanvit
* @version 1.0.1
*/
'use strict';
(function(global) {
console.log('[ServiceRegistry] Loading...');
class ServiceRegistry {
constructor() {
this.services = new Map();
console.log('[ServiceRegistry] Instance created');
}
register(ServiceClass) {
try {
const instance = new ServiceClass();
this.services.set(instance.name, {
class: ServiceClass,
instance: instance,
matcher: ServiceClass.matches
});
console.log(`[ServiceRegistry] Registered: ${instance.name}`);
} catch (e) {
console.error(`[ServiceRegistry] Failed to register service:`, e);
}
}
getServiceByUrl(url) {
for (const [name, { instance, matcher }] of this.services) {
console.log(name, matcher);
try {
if (matcher(url)) return instance;
} catch (e) {
console.error(`[ServiceRegistry] Error checking matcher for ${name}:`, e);
}
}
return null;
}
getService(name) {
return this.services.get(name)?.instance || null;
}
getAllServices() {
return Array.from(this.services.values()).map(s => s.instance);
}
}
global.ServiceRegistry = ServiceRegistry;
global.serviceRegistry = new ServiceRegistry();
})(typeof window !== 'undefined' ? window : self);