-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (77 loc) · 2.46 KB
/
index.js
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
80
81
82
83
84
85
86
87
88
89
90
91
var Player = require('player');
var path = require('path');
// mp3 reporter class
// @param config: available opts is 'red', 'green', 'fail', 'error', 'success'
var Mp3Reporter = function(config) {
var soundDir = path.join(__dirname, 'sound');
// default configuration
var cfg = {
red: path.join(soundDir, 'airplane-alert.mp3'),
green: path.join(soundDir, 'lite-bounce.mp3')
}
// merge config over defaults
for (var opt in config) if (config.hasOwnProperty(opt)) {
cfg[opt] = config[opt];
};
// fallbacks scheme:
// -------------------------------
// fail --> | red --> | NULL
// error --> | |
// |
// success --> | green --> |
// -------------------------------
var fallbacks = {
fail: 'red',
error: 'red',
success: 'green'
// red: null,
// green: null,
}
// @see issue below
// var playersCache = {};
// smart source chooser & player
var play = function(what) {
var target = what;
while (target != undefined) { // looking for target or its fallback
if (cfg[target] != undefined) break;
target = fallbacks[target];
}
if (target == undefined || cfg[target] == undefined) return;
// ISSUE:x2es: it should be as following lines
// but npm player has memory leaks and may not be cached
// commented until solving memory leaks / changing playing library
// if (playersCache[target] == undefined) playersCache[target] = new Player(cfg[target]);
// playersCache[target].play();
var p = new Player(cfg[target]);
// workaround for https://github.com/guo-yu/player/issues/51
p.on('error', function(err) {
if (err === "No next song was found") return;
throw '<player>: ' + err;
});
p.play();
}
this.onBrowserComplete = function(browser) {
// tip about browser (sample data)
// browser: {
// lastResult: {
// success: 24,
// failed: 0,
// skipped: 0,
// total: 24,
// totalTime: 616,
// netTime: 14,
// error: false,
// disconnected: false,
// totalTimeEnd: [Function]
// },
// }
var r = browser.lastResult;
if (r.error) play('error');
else if (r.failed > 0) play('fail');
else play('success');
}
}
Mp3Reporter.$inject = ['config.mp3Reporter'];
module.exports = {
'reporter:mp3': ['type', Mp3Reporter]
};