Added support for multi single command handler and implemented the new music song list method

canary
WolverinDEV 2020-04-04 12:03:48 +02:00
parent 15bacdc9d6
commit 67d1c909ac
4 changed files with 42 additions and 25 deletions

View File

@ -497,7 +497,7 @@ namespace server {
let php: string; let php: string;
let options: Options; let options: Options;
const use_https = true; const use_https = false;
export async function launch(_files: ProjectResource[], options_: Options) { export async function launch(_files: ProjectResource[], options_: Options) {
options = options_; options = options_;
files = _files; files = _files;

View File

@ -58,6 +58,8 @@ export abstract class AbstractCommandHandlerBoss {
register_single_handler(handler: SingleCommandHandler) { register_single_handler(handler: SingleCommandHandler) {
if(typeof handler.command === "string")
handler.command = [handler.command];
this.single_command_handler.push(handler); this.single_command_handler.push(handler);
} }
@ -82,7 +84,8 @@ export abstract class AbstractCommandHandlerBoss {
} }
for(const handler of [...this.single_command_handler]) { for(const handler of [...this.single_command_handler]) {
if(handler.command && handler.command != command.command) // We already know that handler.command must be an array (It will be converted within the register single handler methode)
if(handler.command && (handler.command as string[]).findIndex(e => e === command.command) == -1)
continue; continue;
try { try {

View File

@ -263,39 +263,53 @@ export class CommandHelper extends AbstractCommandHandler {
} }
request_playlist_songs(playlist_id: number) : Promise<PlaylistSong[]> { request_playlist_songs(playlist_id: number) : Promise<PlaylistSong[]> {
//FIXME: notifyplaylistsonglistfinish for the result! let bulked_response = false;
let bulk_index = 0;
const result: PlaylistSong[] = [];
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const single_handler: SingleCommandHandler = { const single_handler: SingleCommandHandler = {
command: "notifyplaylistsonglist", command: ["notifyplaylistsonglist", "notifyplaylistsonglistfinished"],
function: command => { function: command => {
const json = command.arguments; const json = command.arguments;
if(json[0]["playlist_id"] != playlist_id) { if(bulk_index === 0) {
log.error(LogCategory.NETWORKING, tr("Received invalid notification for playlist songs")); /* we're sending the response as bulk */
return false; bulked_response = parseInt(json[0]["version"]) >= 2;
} }
const result: PlaylistSong[] = []; if(parseInt(json[0]["playlist_id"]) !== playlist_id)
return false; /* not our request */
for(const entry of json) { if(command.command === "notifyplaylistsonglistfinished") {
try { resolve(result);
result.push({ return true;
song_id: parseInt(entry["song_id"]), } else {
song_invoker: entry["song_invoker"], for(const entry of json) {
song_previous_song_id: parseInt(entry["song_previous_song_id"]), try {
song_url: entry["song_url"], result.push({
song_url_loader: entry["song_url_loader"], song_id: parseInt(entry["song_id"]),
song_invoker: entry["song_invoker"],
song_previous_song_id: parseInt(entry["song_previous_song_id"]),
song_url: entry["song_url"],
song_url_loader: entry["song_url_loader"],
song_loaded: entry["song_loaded"] == true || entry["song_loaded"] == "1", song_loaded: entry["song_loaded"] == true || entry["song_loaded"] == "1",
song_metadata: entry["song_metadata"] song_metadata: entry["song_metadata"]
}); });
} catch(error) { } catch(error) {
log.error(LogCategory.NETWORKING, tr("Failed to parse playlist song entry: %o"), error); log.error(LogCategory.NETWORKING, tr("Failed to parse playlist song entry: %o"), error);
}
}
if(bulked_response) {
bulk_index++;
return false;
} else {
resolve(result);
return true;
} }
} }
resolve(result);
return true;
} }
}; };
this.handler_boss.register_single_handler(single_handler); this.handler_boss.register_single_handler(single_handler);

View File

@ -121,7 +121,7 @@ export class ServerCommand {
export interface SingleCommandHandler { export interface SingleCommandHandler {
name?: string; name?: string;
command?: string; command?: string | string[];
timeout?: number; timeout?: number;
/* if the return is true then the command handler will be removed */ /* if the return is true then the command handler will be removed */