mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
52 lines
2.5 KiB
TypeScript
52 lines
2.5 KiB
TypeScript
import { AbortControllerRegistry } from "./abort-controller-registry.js";
|
|
import { UnknownState } from "./state.js";
|
|
import { Simplify, UnionToIntersection } from "@videojs/utils/types";
|
|
//#region src/core/slice.d.ts
|
|
type Attach<Target, State> = (ctx: AttachContext<Target, State>) => void;
|
|
interface AttachStore {
|
|
readonly state: UnknownState;
|
|
subscribe: (callback: () => void) => () => void;
|
|
}
|
|
interface AttachContext<Target, State> {
|
|
target: Target;
|
|
signal: AbortSignal;
|
|
store: AttachStore;
|
|
get: () => Readonly<State>;
|
|
set: (partial: Partial<State>) => void;
|
|
reportError: (error: unknown) => void;
|
|
}
|
|
interface StateContext<Target> {
|
|
/** Returns the current target. Throws if not attached. */
|
|
target: () => Target;
|
|
/**
|
|
* Cancellation signals for async operations.
|
|
*
|
|
* - `signals.base` — Aborts on detach or reattach. Use for cleanup.
|
|
* - `signals.supersede(key)` — Returns a signal that aborts when the same key
|
|
* is superseded or when base aborts. Use for operations that should cancel
|
|
* previous in-flight work (e.g., seek superseding seek).
|
|
* - `signals.clear()` — Aborts all keyed signals. Use when starting fresh
|
|
* (e.g., loading a new source cancels pending seeks).
|
|
*/
|
|
signals: AbortControllerRegistry;
|
|
/** Read current slice state. Safe to use inside action closures (not during `state()` init). */
|
|
get: () => Readonly<Record<string, unknown>>;
|
|
/** Patch the slice state. Safe to use inside action closures (not during `state()` init). */
|
|
set: (partial: Record<string, unknown>) => void;
|
|
}
|
|
interface SliceConfig<Target, State> {
|
|
/** Debug label. Used as `displayName` on selectors created from this slice. */
|
|
name?: string;
|
|
state: (ctx: StateContext<Target>) => State;
|
|
attach?: (ctx: AttachContext<Target, State>) => void;
|
|
}
|
|
type Slice<Target, State> = SliceConfig<Target, State>;
|
|
type AnySlice<Target = any> = Slice<Target, any>;
|
|
type SliceFactory<Target> = <State>(config: SliceConfig<Target, State>) => Slice<Target, State>;
|
|
declare function defineSlice<Target>(): SliceFactory<Target>;
|
|
type InferSliceTarget<S> = S extends Slice<infer Target, any> ? Target : never;
|
|
type InferSliceState<S> = S extends Slice<any, infer State> ? State : never;
|
|
type UnionSliceState<Slices extends AnySlice[]> = Simplify<UnionToIntersection<InferSliceState<Slices[number]>>>;
|
|
//#endregion
|
|
export { AnySlice, Attach, AttachContext, AttachStore, InferSliceState, InferSliceTarget, Slice, SliceConfig, SliceFactory, StateContext, UnionSliceState, defineSlice };
|
|
//# sourceMappingURL=slice.d.ts.map
|