-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathservice_instance.ts
50 lines (39 loc) · 1.69 KB
/
service_instance.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
export enum ServiceType {
TRANSLATE = 'translate',
RECOGNIZE = 'recognize',
TTS = 'tts',
COLLECTION = 'Collection'
}
export enum ServiceSourceType {
BUILDIN = 'buildin',
PLUGIN = 'plugin',
}
export function getServiceSouceType(serviceInstanceKey: string): ServiceSourceType {
if (serviceInstanceKey.startsWith('[plugin]')) {
return ServiceSourceType.PLUGIN
} else {
return ServiceSourceType.BUILDIN
}
}
export function whetherPluginService(serviceInstanceKey: string): boolean {
return getServiceSouceType(serviceInstanceKey) === ServiceSourceType.PLUGIN
}
// The serviceInstanceKey consists of the service name and it's id, separated by @
// In earlier versions, the @ separator and id were optional, so they all have only one instance.
export function createServiceInstanceKey(serviceName: string): string {
const randomId = Math.random().toString(36).substring(2)
return `${serviceName}@${randomId}`
}
// if the serviceInstanceKey is from a plugin, serviceName is it's pluginId
export function getServiceName(serviceInstanceKey: string): string {
return serviceInstanceKey.split('@')[0]
}
export function getDisplayInstanceName(instanceName: string, serviceNameSupplier: () => string): string {
return instanceName || serviceNameSupplier()
}
export const INSTANCE_NAME_CONFIG_KEY = 'instanceName'
export function whetherAvailableService(serviceInstanceKey: string, availableServices: Record<ServiceSourceType, Record<string, any>>) {
const serviceSourceType = getServiceSouceType(serviceInstanceKey)
const serviceName = getServiceName(serviceInstanceKey)
return availableServices[serviceSourceType]?.[serviceName] !== undefined
}