-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
116 lines (101 loc) · 3.42 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
/* global exp */
var sequence;
var timeouts = {};
var loadResolve;
function getTime () {
var date = new Date();
return (date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds()) * 1000;
}
function setSyncTimeout (now, target) {
clearTimeout(timeouts[target]);
if (now - target > 10000) return;
timeouts[target] = setTimeout(sync, target - now);
}
function setSyncTimeouts () {
var now = getTime();
sequence.forEach(function (item) {
setSyncTimeout(now, item.block.startTime - 3000);
setSyncTimeout(now, item.block.startTime - 1500);
setSyncTimeout(now, item.block.startTime);
setSyncTimeout(now, item.block.startTime + 1500);
setSyncTimeout(now, item.block.startTime + 3000);
setSyncTimeout(now, item.block.endTime - 3000);
setSyncTimeout(now, item.block.endTime - 1500);
setSyncTimeout(now, item.block.endTime);
setSyncTimeout(now, item.block.endTime + 1500);
setSyncTimeout(now, item.block.endTime + 3000);
setSyncTimeout(now, item.block.startTime + 86400 * 1000 - 3000);
setSyncTimeout(now, item.block.startTime + 86400 * 1000 - 1500);
setSyncTimeout(now, item.block.startTime + 86400 * 1000);
setSyncTimeout(now, item.block.startTime + 86400 * 1000 + 1500);
setSyncTimeout(now, item.block.startTime + 86400 * 1000 + 3000);
setSyncTimeout(now, item.block.endTime + 86400 * 1000 - 3000);
setSyncTimeout(now, item.block.endTime + 86400 * 1000 - 1500);
setSyncTimeout(now, item.block.endTime + 86400 * 1000);
setSyncTimeout(now, item.block.endTime + 86400 * 1000 + 1500);
setSyncTimeout(now, item.block.endTime + 86400 * 1000 + 3000);
});
}
function sync () {
var now = getTime();
var hasOne = false;
sequence.forEach(function (item) {
if (now < item.block.startTime || now >= item.block.endTime) {
if (item.promise) stopItem(item);
} else if (now >= item.block.startTime && now < item.block.endTime) {
hasOne = true;
if (item.promise) playItem(item);
else loadItem(item);
}
});
if (!hasOne) loadResolve();
}
function loadItem (item) {
window.console.log('Loading child application.');
item.promise = exp.player.load({
key: item.block.key,
duration: 86400 * 1000
}).then(function (app) {
window.console.log('Child application loaded.');
setTimeout(sync, 0);
loadResolve();
return app;
}, function (error) {
exp.app.abort(error);
});
}
function playItem (item) {
item.promise.then(function (app) {
if (exp.app.status !== 'playing' || app.status !== 'ready') return;
window.console.log('Child application playback started.');
app.play().then(function () {
delete item.promise;
window.console.info('Child application playback finished.');
setTimeout(sync, 0);
}, function (error) {
exp.app.abort(error);
});
});
}
function stopItem (item) {
item.promise.then(function (app) {
if (app.status === 'loading') app.abort('Stopped by dayplan.');
else if (app.status === 'playing') app.stop();
});
}
window.play = function () {
setSyncTimeouts();
sync();
return new Promise(function () {}); // Dayplan will loop forever.
};
window.load = function () {
sequence = exp.app.config.blocks.map(function (block) {
block.key = block.appKey || block.key; // Backwards compatability.
return { block: block, app: null };
});
return new Promise(function (loadResolve_) {
loadResolve = loadResolve_;
sync();
});
};