(
fn: (player: Player) => Promise,
expectationConvertible: SingleEventExpectation | EventType,
@@ -58,6 +138,30 @@ export const callPlayerAndExpectEvent = async (
);
};
+/**
+ * Starts listening for the specified `MultipleEventExpectation` before executing the passed `fn` function.
+ * This is the race-condition-safe version of calling `callPlayer` and `expectEvents` after that.
+ * Useful when events are directly tied to calls in the `fn` function block and therefore synchronously emitted.
+ * @param fn The function to call.
+ * @param expectationsConvertible The events to expect.
+ * @param timeoutSeconds The number of seconds to wait for the events to occur.
+ * @returns A promise that resolves when the function is finished.
+ * @throws An error if the events do not occur.
+ * @example
+ * ```typescript
+ * await callPlayerAndExpectEvents(async (player) => {
+ * // ...
+ * }, [EventType.Play, EventType.Playing]);
+ * ```
+ *
+ * ```typescript
+ * await callPlayerAndExpectEvents(async (player) => {
+ * // ...
+ * }, EventSequence(EventType.Play, EventType.Playing));
+ * ```
+ * @see {@link Player}
+ * @see {@link EventType}
+ */
export const callPlayerAndExpectEvents = async (
fn: (player: Player) => void,
expectationsConvertible: MultipleEventsExpectation | EventType[],
@@ -70,6 +174,23 @@ export const callPlayerAndExpectEvents = async (
);
};
+/**
+ * Loads the given source configuration and expects `ReadyEvent` to occur.
+ * @param sourceConfig The source configuration to load.
+ * @param timeoutSeconds The number of seconds to wait for the event to occur.
+ * @returns A promise that resolves when the function is finished.
+ * @throws An error if the event does not occur.
+ * @example
+ * ```typescript
+ * await loadSourceConfig({
+ * url: 'https://bitmovin-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8',
+ * type: SourceType.HLS,
+ * });
+ * ```
+ * @see {@link SourceConfig}
+ * @see {@link EventType}
+ * @see {@link ReadyEvent}
+ */
export const loadSourceConfig = async (
sourceConfig: SourceConfig,
timeoutSeconds: number = 10
@@ -80,6 +201,18 @@ export const loadSourceConfig = async (
);
};
+/**
+ * Plays the player for the given time and expects `TimeChangedEvent` to occur.
+ * @param time The time to play for.
+ * @param timeoutSeconds The number of seconds to wait for the event to occur.
+ * @returns A promise that resolves when the function is finished.
+ * @throws An error if the event does not occur.
+ * @example
+ * ```typescript
+ * await playFor(5);
+ * ```
+ * @see {@link TimeChangedEvent}
+ */
export const playFor = async (
time: number,
timeoutSeconds: number = 10
@@ -87,6 +220,18 @@ export const playFor = async (
return await PlayerTestWorld.shared.playFor(time, timeoutSeconds);
};
+/**
+ * Plays the player until the given time and expects `TimeChangedEvent` to occur.
+ * @param time The time to play until.
+ * @param timeoutSeconds The number of seconds to wait for the event to occur.
+ * @returns A promise that resolves when the function is finished.
+ * @throws An error if the event does not occur.
+ * @example
+ * ```typescript
+ * await playUntil(5);
+ * ```
+ * @see {@link TimeChangedEvent}
+ */
export const playUntil = async (
time: number,
timeoutSeconds: number = 10