-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtube.js
88 lines (76 loc) · 1.99 KB
/
tube.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
var io = require('socket.io-client');
var socket = io.connect('http://berrytube.tv:8344');
var links = require('./links');
var newVideoListeners = [];
var timeChangeListeners = [];
var stateChangeListeners = [];
var lastState = -1;
socket.emit('myPlaylistIsInited');
socket.on('forceVideoChange', handleNewVideo);
socket.on('createPlayer', handleNewVideo);
socket.on('hbVideoDetail', handleTimeChange);
function handleNewVideo(data) {
var cb = function (link) {
if (link) {
emitLink(link, data.time);
}
};
switch (data.video.videotype) {
case 'yt':
links.youtube(data.video.videoid, cb);
break;
case 'vimeo':
links.vimeo(data.video.videoid, cb);
break;
case 'dailymotion':
links.dailymotion(data.video.videoid, cb);
break;
case 'soundcloud':
links.soundcloud(data.video.videoid, cb);
break;
case 'osmf':
links.osmf(data.video.videoid, cb);
break;
default:
break;
}
}
function handleTimeChange(data) {
emitTimeChange(data.time);
if (data.state !== lastState) {
lastState = data.state;
emitStateChange(data.state);
}
}
function emitLink(link, inittime) {
newVideoListeners.forEach(function (fn) {
setImmediate(function () {
fn(link, inittime);
});
});
}
function emitTimeChange(time) {
timeChangeListeners.forEach(function (fn) {
setImmediate(function () {
fn(time);
});
});
}
function emitStateChange(state) {
stateChangeListeners.forEach(function (fn) {
setImmediate(function () {
fn(state);
});
});
}
module.exports = {
onVideoChange: function (cb) {
newVideoListeners.push(cb);
},
onTimeChange: function (cb) {
timeChangeListeners.push(cb);
},
onStateChange: function (cb) {
stateChangeListeners.push(cb);
}
};