mirror of
https://gitlab.com/futo-org/fcast.git
synced 2025-08-03 07:47:01 +00:00
Refractored WebOS preload out of common preload
This commit is contained in:
parent
3142709d7f
commit
bff3440a8e
5 changed files with 244 additions and 421 deletions
|
@ -1,6 +1,9 @@
|
|||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { preloadData } from 'common/main/Preload';
|
||||
import { toast, ToastIcon } from 'common/components/Toast';
|
||||
require('lib/webOSTVjs-1.2.10/webOSTV.js');
|
||||
require('lib/webOSTVjs-1.2.10/webOSTV-dev.js');
|
||||
|
||||
enum RemoteKeyCode {
|
||||
Stop = 413,
|
||||
|
@ -12,116 +15,31 @@ enum RemoteKeyCode {
|
|||
}
|
||||
|
||||
try {
|
||||
const serviceId = 'com.futo.fcast.receiver.service';
|
||||
const startupStorageClearService = registerService('startup-storage-clear', () => { preloadData.onStartupStorageClearCb(); });
|
||||
const toastService = registerService('toast', (message: any) => { toast(message.value.message, message.value.icon, message.value.duration); });
|
||||
const getDeviceInfoService = registerService('getDeviceInfo', (message: any) => {
|
||||
console.log(`Main: getDeviceInfo ${JSON.stringify(message)}`);
|
||||
preloadData.deviceInfo = message.value;
|
||||
preloadData.onDeviceInfoCb();
|
||||
}, false);
|
||||
const onConnectService = registerService('connect', (message: any) => { preloadData.onConnectCb(null, message.value); });
|
||||
const onDisconnectService = registerService('disconnect', (message: any) => { preloadData.onDisconnectCb(null, message.value); });
|
||||
const playService = registerService('play', (message: any) => {
|
||||
if (message.value !== undefined && message.value.playData !== undefined) {
|
||||
console.log(`Main: Playing ${JSON.stringify(message)}`);
|
||||
getDeviceInfoService.cancel();
|
||||
startupStorageClearService.cancel();
|
||||
toastService.cancel();
|
||||
onConnectService.cancel();
|
||||
onDisconnectService.cancel();
|
||||
playService.cancel();
|
||||
|
||||
const startupStorageClearService = window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method:"startup-storage-clear",
|
||||
parameters: {},
|
||||
onSuccess: (message: any) => {
|
||||
if (message.value.subscribed === true) {
|
||||
console.log('Main: Registered startup-storage-clear handler with service');
|
||||
}
|
||||
else {
|
||||
preloadData.onStartupStorageClearCb();
|
||||
}
|
||||
},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Main: startup-storage-clear ${JSON.stringify(message)}`);
|
||||
toast(`Main: startup-storage-clear ${JSON.stringify(message)}`, ToastIcon.ERROR);
|
||||
},
|
||||
subscribe: true,
|
||||
resubscribe: true
|
||||
});
|
||||
|
||||
|
||||
const toastService = window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method:"toast",
|
||||
parameters: {},
|
||||
onSuccess: (message: any) => {
|
||||
if (message.value.subscribed === true) {
|
||||
console.log('Main: Registered toast handler with service');
|
||||
}
|
||||
else {
|
||||
toast(message.value.message, message.value.icon, message.value.duration);
|
||||
}
|
||||
},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Main: toast ${JSON.stringify(message)}`);
|
||||
toast(`Main: toast ${JSON.stringify(message)}`, ToastIcon.ERROR);
|
||||
},
|
||||
subscribe: true,
|
||||
resubscribe: true
|
||||
});
|
||||
|
||||
const onConnectService = window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method:"connect",
|
||||
parameters: {},
|
||||
onSuccess: (message: any) => {
|
||||
if (message.value.subscribed === true) {
|
||||
console.log('Main: Registered connect handler with service');
|
||||
}
|
||||
else {
|
||||
preloadData.onConnectCb(null, message.value);
|
||||
}
|
||||
},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Main: connect ${JSON.stringify(message)}`);
|
||||
toast(`Main: connect ${JSON.stringify(message)}`, ToastIcon.ERROR);
|
||||
},
|
||||
subscribe: true,
|
||||
resubscribe: true
|
||||
});
|
||||
|
||||
const onDisconnectService = window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method:"disconnect",
|
||||
parameters: {},
|
||||
onSuccess: (message: any) => {
|
||||
if (message.value.subscribed === true) {
|
||||
console.log('Main: Registered disconnect handler with service');
|
||||
}
|
||||
else {
|
||||
preloadData.onDisconnectCb(null, message.value);
|
||||
}
|
||||
},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Main: disconnect ${JSON.stringify(message)}`);
|
||||
toast(`Main: disconnect ${JSON.stringify(message)}`, ToastIcon.ERROR);
|
||||
},
|
||||
subscribe: true,
|
||||
resubscribe: true
|
||||
});
|
||||
|
||||
const playService = window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method:"play",
|
||||
parameters: {},
|
||||
onSuccess: (message: any) => {
|
||||
if (message.value.subscribed === true) {
|
||||
console.log('Main: Registered play handler with service');
|
||||
}
|
||||
else {
|
||||
if (message.value !== undefined && message.value.playData !== undefined) {
|
||||
console.log(`Main: Playing ${JSON.stringify(message)}`);
|
||||
preloadData.getDeviceInfoService.cancel();
|
||||
startupStorageClearService.cancel();
|
||||
toastService.cancel();
|
||||
onConnectService.cancel();
|
||||
onDisconnectService.cancel();
|
||||
playService.cancel();
|
||||
|
||||
// WebOS 22 and earlier does not work well using the history API,
|
||||
// so manually handling page navigation...
|
||||
// history.pushState({}, '', '../main_window/index.html');
|
||||
window.open('../player/index.html');
|
||||
}
|
||||
}
|
||||
},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Main: play ${JSON.stringify(message)}`);
|
||||
toast(`Main: play ${JSON.stringify(message)}`, ToastIcon.ERROR);
|
||||
},
|
||||
subscribe: true,
|
||||
resubscribe: true
|
||||
});
|
||||
// WebOS 22 and earlier does not work well using the history API,
|
||||
// so manually handling page navigation...
|
||||
// history.pushState({}, '', '../main_window/index.html');
|
||||
window.open('../player/index.html');
|
||||
}
|
||||
});
|
||||
|
||||
const launchHandler = (args: any) => {
|
||||
// args don't seem to be passed in via event despite what documentation says...
|
||||
|
@ -131,24 +49,12 @@ try {
|
|||
const lastTimestamp = localStorage.getItem('lastTimestamp');
|
||||
if (params.playData !== undefined && params.timestamp != lastTimestamp) {
|
||||
localStorage.setItem('lastTimestamp', params.timestamp);
|
||||
if (preloadData.getDeviceInfoService !== undefined) {
|
||||
preloadData.getDeviceInfoService.cancel();
|
||||
}
|
||||
if (startupStorageClearService !== undefined) {
|
||||
startupStorageClearService.cancel();
|
||||
}
|
||||
if (toastService !== undefined) {
|
||||
toastService.cancel();
|
||||
}
|
||||
if (onConnectService !== undefined) {
|
||||
onConnectService.cancel();
|
||||
}
|
||||
if (onDisconnectService !== undefined) {
|
||||
onDisconnectService.cancel();
|
||||
}
|
||||
if (playService !== undefined) {
|
||||
playService.cancel();
|
||||
}
|
||||
startupStorageClearService?.cancel();
|
||||
toastService?.cancel();
|
||||
getDeviceInfoService?.cancel();
|
||||
onConnectService?.cancel();
|
||||
onDisconnectService?.cancel();
|
||||
playService?.cancel();
|
||||
|
||||
// WebOS 22 and earlier does not work well using the history API,
|
||||
// so manually handling page navigation...
|
||||
|
@ -183,3 +89,27 @@ catch (err) {
|
|||
console.error(`Main: preload ${JSON.stringify(err)}`);
|
||||
toast(`Main: preload ${JSON.stringify(err)}`, ToastIcon.ERROR);
|
||||
}
|
||||
|
||||
function registerService(method: string, callback: (message: any) => void, subscribe: boolean = true): any {
|
||||
const serviceId = 'com.futo.fcast.receiver.service';
|
||||
|
||||
return window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method: method,
|
||||
parameters: {},
|
||||
onSuccess: (message: any) => {
|
||||
if (message.value.subscribed === true) {
|
||||
console.log(`Main: Registered ${method} handler with service`);
|
||||
}
|
||||
else {
|
||||
callback(message);
|
||||
}
|
||||
},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Main: ${method} ${JSON.stringify(message)}`);
|
||||
toast(`Main: ${method} ${JSON.stringify(message)}`, ToastIcon.ERROR);
|
||||
},
|
||||
// onComplete: (message) => {},
|
||||
subscribe: subscribe,
|
||||
resubscribe: subscribe
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,46 +1,158 @@
|
|||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { preloadData } from 'common/player/Preload';
|
||||
import { PlaybackErrorMessage, PlaybackUpdateMessage, VolumeUpdateMessage } from 'common/Packets';
|
||||
import { toast, ToastIcon } from 'common/components/Toast';
|
||||
require('lib/webOSTVjs-1.2.10/webOSTV.js');
|
||||
require('lib/webOSTVjs-1.2.10/webOSTV-dev.js');
|
||||
|
||||
const launchHandler = (args: any) => {
|
||||
// args don't seem to be passed in via event despite what documentation says...
|
||||
const params = window.webOSDev.launchParams();
|
||||
console.log(`Player: (Re)launching FCast Receiver with args: ${JSON.stringify(params)}`);
|
||||
try {
|
||||
const serviceId = 'com.futo.fcast.receiver.service';
|
||||
let playerWindowOpen = false;
|
||||
|
||||
const lastTimestamp = localStorage.getItem('lastTimestamp');
|
||||
if (params.playData !== undefined && params.timestamp != lastTimestamp) {
|
||||
localStorage.setItem('lastTimestamp', params.timestamp);
|
||||
window.webOSAPI = {
|
||||
pendingPlay: null
|
||||
};
|
||||
|
||||
if (preloadData.playerWindowOpen !== undefined) {
|
||||
preloadData.playerWindowOpen = false;
|
||||
}
|
||||
if (preloadData.playService !== undefined) {
|
||||
preloadData.playService.cancel();
|
||||
}
|
||||
if (preloadData.pauseService !== undefined) {
|
||||
preloadData.pauseService.cancel();
|
||||
}
|
||||
if (preloadData.resumeService !== undefined) {
|
||||
preloadData.resumeService.cancel();
|
||||
}
|
||||
if (preloadData.stopService !== undefined) {
|
||||
preloadData.stopService.cancel();
|
||||
}
|
||||
if (preloadData.seekService !== undefined) {
|
||||
preloadData.seekService.cancel();
|
||||
}
|
||||
if (preloadData.setVolumeService !== undefined) {
|
||||
preloadData.setVolumeService.cancel();
|
||||
}
|
||||
if (preloadData.setSpeedService !== undefined) {
|
||||
preloadData.setSpeedService.cancel();
|
||||
}
|
||||
preloadData.sendPlaybackErrorCb = (error: PlaybackErrorMessage) => {
|
||||
window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method: 'send_playback_error',
|
||||
parameters: { error },
|
||||
onSuccess: () => {},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Player: send_playback_error ${JSON.stringify(message)}`);
|
||||
},
|
||||
});
|
||||
};
|
||||
preloadData.sendPlaybackUpdateCb = (update: PlaybackUpdateMessage) => {
|
||||
window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method: 'send_playback_update',
|
||||
parameters: { update },
|
||||
// onSuccess: (message: any) => {
|
||||
// console.log(`Player: send_playback_update ${JSON.stringify(message)}`);
|
||||
// },
|
||||
onSuccess: () => {},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Player: send_playback_update ${JSON.stringify(message)}`);
|
||||
},
|
||||
});
|
||||
};
|
||||
preloadData.sendVolumeUpdateCb = (update: VolumeUpdateMessage) => {
|
||||
window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method: 'send_volume_update',
|
||||
parameters: { update },
|
||||
onSuccess: () => {},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Player: send_volume_update ${JSON.stringify(message)}`);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const playService = window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method:"play",
|
||||
parameters: {},
|
||||
onSuccess: (message: any) => {
|
||||
// console.log(JSON.stringify(message));
|
||||
if (message.value.subscribed === true) {
|
||||
console.log('Player: Registered play handler with service');
|
||||
}
|
||||
|
||||
if (message.value.playData !== null) {
|
||||
if (!playerWindowOpen) {
|
||||
playerWindowOpen = true;
|
||||
}
|
||||
|
||||
if (preloadData.onPlayCb === undefined) {
|
||||
window.webOSAPI.pendingPlay = message.value.playData;
|
||||
}
|
||||
else {
|
||||
preloadData.onPlayCb(null, message.value.playData);
|
||||
}
|
||||
}
|
||||
},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Player: play ${JSON.stringify(message)}`);
|
||||
},
|
||||
subscribe: true,
|
||||
resubscribe: true
|
||||
});
|
||||
|
||||
const pauseService = registerService('pause', () => { preloadData.onPauseCb(); });
|
||||
const resumeService = registerService('resume', () => { preloadData.onResumeCb(); });
|
||||
const stopService = registerService('stop', () => {
|
||||
playerWindowOpen = false;
|
||||
playService.cancel();
|
||||
pauseService.cancel();
|
||||
resumeService.cancel();
|
||||
stopService.cancel();
|
||||
seekService.cancel();
|
||||
setVolumeService.cancel();
|
||||
setSpeedService.cancel();
|
||||
|
||||
// WebOS 22 and earlier does not work well using the history API,
|
||||
// so manually handling page navigation...
|
||||
// history.pushState({}, '', '../main_window/index.html');
|
||||
window.open('../player/index.html');
|
||||
}
|
||||
};
|
||||
// history.back();
|
||||
window.open('../main_window/index.html');
|
||||
});
|
||||
|
||||
document.addEventListener('webOSLaunch', (ags) => { console.log('lunch'); launchHandler(ags)});
|
||||
document.addEventListener('webOSRelaunch', (ags) => { console.log('relun'); launchHandler(ags)});
|
||||
const seekService = registerService('seek', (message: any) => { preloadData.onSeekCb(null, message.value); });
|
||||
const setVolumeService = registerService('setvolume', (message: any) => { preloadData.onSetVolumeCb(null, message.value); });
|
||||
const setSpeedService = registerService('setspeed', (message: any) => { preloadData.onSetSpeedCb(null, message.value); });
|
||||
|
||||
const launchHandler = (args: any) => {
|
||||
// args don't seem to be passed in via event despite what documentation says...
|
||||
const params = window.webOSDev.launchParams();
|
||||
console.log(`Player: (Re)launching FCast Receiver with args: ${JSON.stringify(params)}`);
|
||||
|
||||
const lastTimestamp = localStorage.getItem('lastTimestamp');
|
||||
if (params.playData !== undefined && params.timestamp != lastTimestamp) {
|
||||
localStorage.setItem('lastTimestamp', params.timestamp);
|
||||
playerWindowOpen = false;
|
||||
|
||||
playService?.cancel();
|
||||
pauseService?.cancel();
|
||||
resumeService?.cancel();
|
||||
stopService?.cancel();
|
||||
seekService?.cancel();
|
||||
setVolumeService?.cancel();
|
||||
setSpeedService?.cancel();
|
||||
|
||||
// WebOS 22 and earlier does not work well using the history API,
|
||||
// so manually handling page navigation...
|
||||
// history.pushState({}, '', '../main_window/index.html');
|
||||
window.open('../player/index.html');
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('webOSLaunch', (ags) => { launchHandler(ags)});
|
||||
document.addEventListener('webOSRelaunch', (ags) => { launchHandler(ags)});
|
||||
|
||||
}
|
||||
catch (err) {
|
||||
console.error(`Player: preload ${JSON.stringify(err)}`);
|
||||
toast(`Player: preload ${JSON.stringify(err)}`, ToastIcon.ERROR);
|
||||
}
|
||||
|
||||
function registerService(method: string, callback: (message: any) => void, subscribe: boolean = true): any {
|
||||
const serviceId = 'com.futo.fcast.receiver.service';
|
||||
|
||||
return window.webOS.service.request(`luna://${serviceId}/`, {
|
||||
method: method,
|
||||
parameters: {},
|
||||
onSuccess: (message: any) => {
|
||||
if (message.value.subscribed === true) {
|
||||
console.log(`Player: Registered ${method} handler with service`);
|
||||
}
|
||||
else {
|
||||
callback(message);
|
||||
}
|
||||
},
|
||||
onFailure: (message: any) => {
|
||||
console.error(`Player: ${method} ${JSON.stringify(message)}`);
|
||||
// toast(`Player: ${method} ${JSON.stringify(message)}`, ToastIcon.ERROR);
|
||||
},
|
||||
// onComplete: (message) => {},
|
||||
subscribe: subscribe,
|
||||
resubscribe: subscribe
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue