mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-28 03:13:51 +02:00
Rename and extract function
This commit is contained in:
+29
@@ -338,6 +338,35 @@ export const promiseWithResolvers = <T = void>() => {
|
||||
return { promise, resolve: resolve!, reject: reject! };
|
||||
};
|
||||
|
||||
export const promiseAllEnsureOrder = async <T>(promises: T[]) => {
|
||||
const results: Awaited<T>[] = [];
|
||||
const { promise, resolve, reject } = promiseWithResolvers();
|
||||
|
||||
const onValue = (value: Awaited<T>, i: number) => {
|
||||
if (results.length === i) {
|
||||
results.push(value);
|
||||
|
||||
if (results.length === promises.length) {
|
||||
resolve();
|
||||
}
|
||||
} else {
|
||||
reject(new Error('Order violation'));
|
||||
}
|
||||
};
|
||||
|
||||
for (let i = 0; i < promises.length; i++) {
|
||||
const value = promises[i]!;
|
||||
if (value instanceof Promise) {
|
||||
void value.then(x => onValue(x as Awaited<T>, i));
|
||||
} else {
|
||||
onValue(value as Awaited<T>, i);
|
||||
}
|
||||
}
|
||||
|
||||
await promise;
|
||||
return results;
|
||||
};
|
||||
|
||||
export const removeItem = <T>(arr: T[], item: T) => {
|
||||
const index = arr.indexOf(item);
|
||||
if (index !== -1) {
|
||||
|
||||
@@ -10,36 +10,7 @@ import {
|
||||
VideoSampleCursor,
|
||||
} from '../../src/cursors.js';
|
||||
import { AudioSample, VideoSample } from '../../src/sample.js';
|
||||
import { promiseIterateAll, promiseWithResolvers } from '../../src/misc.js';
|
||||
|
||||
const promiseAllEnsureOrder = async <T>(promises: T[]) => {
|
||||
const results: Awaited<T>[] = [];
|
||||
const { promise, resolve, reject } = promiseWithResolvers();
|
||||
|
||||
const onValue = (value: Awaited<T>, i: number) => {
|
||||
if (results.length === i) {
|
||||
results.push(value);
|
||||
|
||||
if (results.length === promises.length) {
|
||||
resolve();
|
||||
}
|
||||
} else {
|
||||
reject(new Error('Order violation'));
|
||||
}
|
||||
};
|
||||
|
||||
for (let i = 0; i < promises.length; i++) {
|
||||
const value = promises[i]!;
|
||||
if (value instanceof Promise) {
|
||||
void value.then(x => onValue(x as Awaited<T>, i));
|
||||
} else {
|
||||
onValue(value as Awaited<T>, i);
|
||||
}
|
||||
}
|
||||
|
||||
await promise;
|
||||
return results;
|
||||
};
|
||||
import { promiseAllEnsureOrder, promiseIterateAll } from '../../src/misc.js';
|
||||
|
||||
test('Sample cursor seeking', async () => {
|
||||
using input = new Input({
|
||||
|
||||
@@ -5,6 +5,7 @@ import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import { ALL_FORMATS } from '../../src/input-format.js';
|
||||
import { PacketCursor, PacketReader } from '../../src/cursors.js';
|
||||
import { promiseAllEnsureOrder } from '../../src/misc.js';
|
||||
|
||||
const __dirname = new URL('.', import.meta.url).pathname;
|
||||
|
||||
@@ -251,8 +252,7 @@ test('Command queuing', async () => {
|
||||
|
||||
expect(commands.every(x => x instanceof Promise)).toBe(true);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/await-thenable
|
||||
const resolved = await Promise.all(commands);
|
||||
const resolved = await promiseAllEnsureOrder(commands);
|
||||
|
||||
expect(resolved[0]!.timestamp).toBe(0);
|
||||
|
||||
Reference in New Issue
Block a user