1
|
|
|
/** |
2
|
|
|
* @summary SARAH action helper |
3
|
|
|
* @description SarahActionHelper provide helpful methods regarding action |
4
|
|
|
* for module which want to run in SARAH v3 AND v4 |
5
|
|
|
* |
6
|
|
|
* @requires sarahVersion |
7
|
|
|
* |
8
|
|
|
* @example <caption>Instantiation</caption> |
9
|
|
|
* const SarahActionHelper = require('sarah.lib.utils/actionHelper'); |
10
|
|
|
* var helper = new SarahActionHelper(<SarahActionContext> actionContext); |
11
|
|
|
* |
12
|
|
|
* @example <caption>Helper</caption> |
13
|
|
|
* helper.speak(tts); |
14
|
|
|
* |
15
|
|
|
* @example <caption>Getter</caption |
16
|
|
|
* helper.getContext(); |
17
|
|
|
* |
18
|
|
|
* @example <caption>S.A.R.A.H. module integration</caption |
19
|
|
|
* // MyModule.js |
20
|
|
|
* MyModule.prototype.process = function (helper) { |
21
|
|
|
* helper.speak('c\'est partis'); |
22
|
|
|
* |
23
|
|
|
* // @var {SarahActionContext} context |
24
|
|
|
* var context = helper.getContext(); |
25
|
|
|
* // @var {object} data |
26
|
|
|
* var data = context.getData(); |
27
|
|
|
* // @var {callable} callback |
28
|
|
|
* var callback = context.getCallback(); |
29
|
|
|
* // @var {SARAH} sarah |
30
|
|
|
* var sarah = context.getSARAH(); |
31
|
|
|
* |
32
|
|
|
* if (data.action == 'action1') { |
33
|
|
|
* ... |
34
|
|
|
* } |
35
|
|
|
* ... |
36
|
|
|
* }; |
37
|
|
|
*/ |
38
|
|
|
|
39
|
4 |
|
const version = require('./version'); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @constructor |
43
|
|
|
* |
44
|
|
|
* @param {SarahActionContext} actionContext |
45
|
|
|
*/ |
46
|
|
|
function SarahActionHelper(actionContext) { |
47
|
3 |
|
this.actionContext = actionContext; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @public |
52
|
|
|
* |
53
|
|
|
* @param {string} tts Text to speach |
54
|
|
|
*/ |
55
|
4 |
|
SarahActionHelper.prototype.speak = function(tts) { |
56
|
2 |
|
var callback = this.actionContext.getCallback(); |
57
|
2 |
|
if (version.isV3() === true) { |
58
|
1 |
|
callback({tts: tts}); |
59
|
|
|
} else { |
60
|
1 |
|
this.actionContext |
61
|
|
|
.getSARAH() |
62
|
|
|
.speak(tts); |
63
|
1 |
|
callback(); |
64
|
|
|
} |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @public |
69
|
|
|
* |
70
|
|
|
* @returns {SarahActionContext} |
71
|
|
|
*/ |
72
|
4 |
|
SarahActionHelper.prototype.getContext = function() { |
73
|
1 |
|
return this.actionContext; |
74
|
|
|
}; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @private |
78
|
|
|
* @type {SarahActionContext} |
79
|
|
|
*/ |
80
|
4 |
|
SarahActionHelper.prototype.actionContext = null; |
81
|
|
|
|
82
|
|
|
/* Export class */ |
83
|
|
|
module.exports = SarahActionHelper; |
84
|
|
|
|