Processing Time Slots / Random Slots will be less likely to cause a stream freeze

This commit is contained in:
vexorian 2021-06-01 20:45:50 -04:00
parent ae7f0ce703
commit ce22bcd12a

View File

@ -281,10 +281,10 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService
return res.status(404).send("Channel doesn't have programs?");
}
res.writeHead(200, {
'Content-Type': 'application.json'
'Content-Type': 'application/json'
});
let transformStream = JSONStream.stringify(); //false makes it not add 'separators'
let transformStream = JSONStream.stringify();
transformStream.pipe(res);
for (let i = 0; i < programs.length; i++) {
@ -1003,7 +1003,7 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService
console.error("time slots error: " + toolRes.userError);
return res.status(400).send(toolRes.userError);
}
res.status(200).send(toolRes);
await streamToolResult(toolRes, res);
} catch(err) {
console.error(err);
res.status(500).send("Internal error");
@ -1017,7 +1017,7 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService
console.error("random slots error: " + toolRes.userError);
return res.status(400).send(toolRes.userError);
}
res.status(200).send(toolRes);
await streamToolResult(toolRes, res);
} catch(err) {
console.error(err);
res.status(500).send("Internal error");
@ -1069,6 +1069,30 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService
channel.fallback.forEach( cleanUpProgram );
}
async function streamToolResult(toolRes, res) {
let programs = toolRes.programs;
delete toolRes.programs;
let s = JSON.stringify(toolRes);
s = s.slice(0, -1);
console.log( JSON.stringify(toolRes));
res.writeHead(200, {
'Content-Type': 'application/json'
});
let transformStream = JSONStream.stringify(
s + ',"programs":[',
',' ,
']}');
transformStream.pipe(res);
for (let i = 0; i < programs.length; i++) {
transformStream.write( programs[i] );
await throttle();
}
transformStream.end();
}
return router
}