Skip to content

Commit

Permalink
refactor: improve saveToFileSystem to use PromiseQueue for better asy…
Browse files Browse the repository at this point in the history
…nc handling
  • Loading branch information
shinyoshiaki committed Feb 12, 2025
1 parent 65d55f6 commit 55cb8da
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions packages/rtp/src/extra/processor/webmCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,37 @@ export class WebmCallback extends WebmBase {
* @param path
* @returns eol
*/
export const saveToFileSystem = (path: string) => async (value: WebmOutput) => {
if (value.saveToFile) {
await appendFile(path, value.saveToFile);
export const saveToFileSystem = (path: string) => {
const queue = new PromiseQueue();
return async (value: WebmOutput): Promise<boolean> => {
return await queue.push<boolean>(async () => {
if (value.saveToFile) {
await appendFile(path, value.saveToFile);

return false;
} else if (value.eol) {
const { durationElement } = value.eol;
const handler = await open(path, "r+");
return false;
} else if (value.eol) {
const { durationElement } = value.eol;
const handler = await open(path, "r+");

// set duration
await handler.write(
durationElement,
0,
durationElement.length,
DurationPosition,
);
// set duration
await handler.write(
durationElement,
0,
durationElement.length,
DurationPosition,
);

// set size
const meta = await stat(path);
const resize = replaceSegmentSize(meta.size);
await handler.write(resize, 0, resize.length, SegmentSizePosition);
// set size
const meta = await stat(path);
const resize = replaceSegmentSize(meta.size);
await handler.write(resize, 0, resize.length, SegmentSizePosition);

await handler.close();
await handler.close();

return true;
}
return true;
}

return false;
});
};
};

0 comments on commit 55cb8da

Please sign in to comment.