2021-01-27 00:41:42 +01:00
|
|
|
export default class UWGlobals {
|
2021-10-19 22:49:13 +02:00
|
|
|
|
2021-01-27 00:41:42 +01:00
|
|
|
constructor() {
|
|
|
|
this.videos = [];
|
2021-10-19 22:49:13 +02:00
|
|
|
this.busSubscriptions = [];
|
|
|
|
this.actionSubscriptions = {};
|
|
|
|
this.bus = {
|
|
|
|
sendMessage: (action, config) => this.propagateMessages(action, config),
|
|
|
|
subscribe: this.subscribeToAny,
|
|
|
|
subscribeToAction: this.subscribeToAction
|
|
|
|
}
|
2021-01-27 00:41:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getNewVideoID() {
|
|
|
|
let random;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// 4-digit [a-z0-9] string. Should be unique per page on first try
|
|
|
|
random = (Math.random() * 1679616).toFixed().toString(36);
|
2021-10-19 22:49:13 +02:00
|
|
|
|
2021-01-27 00:41:42 +01:00
|
|
|
if (this.videos.findIndex(x => x.vdid === random) === -1) {
|
|
|
|
return random;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addVideo(video) {
|
|
|
|
// get video ID
|
|
|
|
const id = this.getNewVideoID();
|
|
|
|
video.vdid = id;
|
|
|
|
this.videos.push(video);
|
|
|
|
}
|
|
|
|
|
|
|
|
getVideo(id) {
|
|
|
|
return this.videos.find(x => x.vdid === id);
|
|
|
|
}
|
|
|
|
|
2021-10-19 22:49:13 +02:00
|
|
|
importSubscriptionsFromCommsHandlers(commands) {
|
|
|
|
for (const action in commands) {
|
|
|
|
for (const command of commands[action]) {
|
|
|
|
this.subscribeToAction(action, command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeToAction(action, callback) {
|
|
|
|
if (!this.actionSubscriptions[action]) {
|
|
|
|
this.actionSubscriptions[action] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.actionSubscriptions[action].push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeToAny(callback) {
|
|
|
|
this.busSubscriptions.push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
propagateMessages(action, config) {
|
|
|
|
if (this.busSubscriptions) {
|
|
|
|
for (const subscription of this.busSubscriptions) {
|
|
|
|
subscription(action, config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.actionSubscriptions && this.actionSubscriptions[action]) {
|
|
|
|
for (const subscription of this.actionSubscriptions[action]) {
|
|
|
|
subscription(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 00:41:42 +01:00
|
|
|
destroy() {
|
|
|
|
// todo: implement
|
|
|
|
}
|
|
|
|
}
|