mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-03 13:53:53 +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! };
|
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) => {
|
export const removeItem = <T>(arr: T[], item: T) => {
|
||||||
const index = arr.indexOf(item);
|
const index = arr.indexOf(item);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
|
|||||||
@@ -10,36 +10,7 @@ import {
|
|||||||
VideoSampleCursor,
|
VideoSampleCursor,
|
||||||
} from '../../src/cursors.js';
|
} from '../../src/cursors.js';
|
||||||
import { AudioSample, VideoSample } from '../../src/sample.js';
|
import { AudioSample, VideoSample } from '../../src/sample.js';
|
||||||
import { promiseIterateAll, promiseWithResolvers } from '../../src/misc.js';
|
import { promiseAllEnsureOrder, promiseIterateAll } 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
test('Sample cursor seeking', async () => {
|
test('Sample cursor seeking', async () => {
|
||||||
using input = new Input({
|
using input = new Input({
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import path from 'node:path';
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import { ALL_FORMATS } from '../../src/input-format.js';
|
import { ALL_FORMATS } from '../../src/input-format.js';
|
||||||
import { PacketCursor, PacketReader } from '../../src/cursors.js';
|
import { PacketCursor, PacketReader } from '../../src/cursors.js';
|
||||||
|
import { promiseAllEnsureOrder } from '../../src/misc.js';
|
||||||
|
|
||||||
const __dirname = new URL('.', import.meta.url).pathname;
|
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);
|
expect(commands.every(x => x instanceof Promise)).toBe(true);
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/await-thenable
|
const resolved = await promiseAllEnsureOrder(commands);
|
||||||
const resolved = await Promise.all(commands);
|
|
||||||
|
|
||||||
expect(resolved[0]!.timestamp).toBe(0);
|
expect(resolved[0]!.timestamp).toBe(0);
|
||||||
|
|
||||||
Reference in New Issue
Block a user