1
|
|
|
"use strict"; |
2
|
|
|
|
3
|
|
|
const sarahClient = require('./client/sarahClient'); |
|
|
|
|
4
|
|
|
const pMapNormalizeAnyway = require('./plugins/promise/mapNormalizeAnyway'); |
5
|
|
|
const pAllAnyway = require('./plugins/promise/allAnyway'); |
6
|
|
|
const logger = require('./logger'); |
7
|
|
|
const DecoratorPlugin = require('./model/DecoratorPlugin'); |
8
|
|
|
const NestedError = require('nested-error-stacks'); |
9
|
|
|
const ActorPlugin = require('./model/ActorPlugin'); |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Allow plugins management |
14
|
|
|
*/ |
15
|
|
|
class PluginManager { |
16
|
|
|
/** |
17
|
|
|
* @public |
18
|
|
|
* |
19
|
|
|
* @param {Plugin[]} pluginList |
20
|
|
|
*/ |
21
|
|
|
constructor(pluginList = []) { |
22
|
|
|
this.logger = logger; |
23
|
|
|
/** @type {Plugin[]} */ |
24
|
|
|
this.pluginList = pluginList; |
25
|
|
|
/** @type {DecoratorPlugin[]} */ |
26
|
|
|
this.decoratorList = []; |
27
|
|
|
/** @type {ActorPlugin[]} */ |
28
|
|
|
this.actorList = []; |
29
|
|
|
this.initialized = false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @public |
34
|
|
|
* |
35
|
|
|
* @param {Promise<string|Error>} textToSpeech |
36
|
|
|
*/ |
37
|
|
|
decorateTts(textToSpeech) { |
38
|
|
|
if (this.initialized === false) { |
39
|
|
|
return Promise.reject(new Error('PluginManager not initialized')); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return pMapNormalizeAnyway( |
43
|
|
|
textToSpeech, |
44
|
|
|
this.decoratorList |
45
|
|
|
// Return a callback that accept the value to normalize |
46
|
|
|
.map(decorator => decorator.normalizeTts) |
47
|
|
|
) |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @public |
52
|
|
|
* |
53
|
|
|
* @return {Promise<null|Error>} |
54
|
|
|
*/ |
55
|
|
|
init() { |
56
|
|
|
return pAllAnyway( |
57
|
|
|
this.pluginList.map(plugin => plugin.dispose(this)) |
58
|
|
|
) |
59
|
|
|
.then(({resolvedList, rejectedList}) => { |
60
|
|
|
if (rejectedList.length > 0) { |
61
|
|
|
this.logger.warning('Some plugin are rejected : '); |
62
|
|
|
resolvedList |
63
|
|
|
.forEach((error, pluginKey) => { |
64
|
|
|
const plugin = this.pluginList[pluginKey]; |
65
|
|
|
this.logger.warning(`${plugin.getName()} : ${error}`); |
66
|
|
|
}) |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return this.cleanPluginList(resolvedList.length ? resolvedList.keys() : []); |
70
|
|
|
}) |
71
|
|
|
.then(() => this.splitPluginListByRole()) |
72
|
|
|
.then(() => {this.initialized = true;}) |
73
|
|
|
.then(() => null) |
74
|
|
|
; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @public |
79
|
|
|
* |
80
|
|
|
* @returns {Promise.<null|Error>} |
81
|
|
|
*/ |
82
|
|
|
dispose() { |
83
|
|
|
if (this.initialized === false) { |
84
|
|
|
return Promise.resolve(null); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return pAllAnyway(this.pluginList.map(plugin => plugin.dispose(this))) |
88
|
|
|
.then(() => null); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @private |
93
|
|
|
* |
94
|
|
|
* @return {Promise<undefined|Error>} |
95
|
|
|
*/ |
96
|
|
|
cleanPluginList(validPluginIdList) { |
97
|
|
|
// Override plugins list with only valid ones |
98
|
|
|
const backupPluginList = this.pluginList; |
99
|
|
|
this.pluginList = []; |
100
|
|
|
|
101
|
|
|
return new Promise((resolve, reject) => { |
102
|
|
|
try { |
103
|
|
|
this.pluginList = validPluginIdList.map(index => { |
104
|
|
|
return backupPluginList[index]; |
105
|
|
|
}); |
106
|
|
|
resolve(); |
107
|
|
|
} catch (e) { |
108
|
|
|
reject(new NestedError('Error during plugins validity split', error)); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @private |
115
|
|
|
* |
116
|
|
|
* @return {Promise<undefined|Error>} |
117
|
|
|
*/ |
118
|
|
|
splitPluginListByRole() { |
119
|
|
|
return Promise.all([ |
120
|
|
|
() => { |
121
|
|
|
this.decoratorList = this.pluginList |
122
|
|
|
.map(plugin => plugin instanceof DecoratorPlugin); |
123
|
|
|
}, |
124
|
|
|
() => { |
125
|
|
|
this.actorList = this.pluginList |
126
|
|
|
.map(plugin => plugin instanceof ActorPlugin); |
127
|
|
|
} |
128
|
|
|
]) |
129
|
|
|
.catch(error => Promise.reject(new NestedError('Error during plugins roles split', error))); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
module.exports = PluginManager; |
134
|
|
|
|