import { SessionID } from "cojson"; import { CoValueLoadingState, ItemsSym, TypeSym } from "../internal.js"; import { type Account } from "./account.js"; import { CoFeedEntry } from "./coFeed.js"; import { type CoKeys } from "./coMap.js"; import { type CoValue, type ID } from "./interfaces.js"; /** * Returns a boolean for whether the given type is a union. * * Taken from https://github.com/sindresorhus/type-fest/blob/main/source/is-union.d.ts */ type IsUnion = ([ T ] extends [never] ? false : T extends any ? [U] extends [T] ? false : true : never) extends infer Result ? boolean extends Result ? true : Result : never; /** * A CoValue that may or may not be loaded. */ export type MaybeLoaded = T | NotLoaded; /** * A CoValue that is either successfully loaded or that could not be loaded. */ export type Settled = T | Inaccessible; /** * A CoValue that is not loaded. */ export type NotLoaded = { $jazz: { id: ID; loadingState: typeof CoValueLoadingState.LOADING | typeof CoValueLoadingState.DELETED | typeof CoValueLoadingState.UNAVAILABLE | typeof CoValueLoadingState.UNAUTHORIZED; }; $isLoaded: false; }; /** * A CoValue that is being loaded */ export type Loading = { $jazz: { id: ID; loadingState: typeof CoValueLoadingState.LOADING; }; $isLoaded: false; }; /** * A CoValue that could not be loaded */ export type Inaccessible = { $jazz: { id: ID; loadingState: typeof CoValueLoadingState.DELETED | typeof CoValueLoadingState.UNAVAILABLE | typeof CoValueLoadingState.UNAUTHORIZED; }; $isLoaded: false; }; /** * Narrows a maybe-loaded, optional CoValue to a loaded and required CoValue. */ export type LoadedAndRequired = Exclude | undefined>; /** * Narrows a maybe-loaded, optional CoValue to a loaded and optional CoValue */ export type AsLoaded = Exclude>; /** * By default, if a nested CoValue is not loaded, the parent CoValue will not be loaded either. * When `$onError: "catch"` is used, the parent CoValue will always be loaded, and an {@link NotLoaded} * value will be returned for the nested CoValue if it cannot be loaded. * * Use `$onError` to handle cases where some data you have requested is inaccessible, * similar to a `try...catch` block in your query. */ type OnError = { $onError?: "catch"; }; export type RefsToResolve = boolean | (DepthLimit extends CurrentDepth["length"] ? any : V extends ReadonlyArray ? LoadedAndRequired extends CoValue ? ({ $each?: RefsToResolve, DepthLimit, [ 0, ...CurrentDepth ]>; } & OnError) | boolean : OnError | boolean : V extends { [TypeSym]: "CoMap" | "Group" | "Account"; } ? ({ [Key in CoKeys as LoadedAndRequired extends CoValue ? Key : never]?: RefsToResolve, DepthLimit, [ 0, ...CurrentDepth ]>; } & OnError) | (ItemsSym extends keyof V ? { $each: RefsToResolve, DepthLimit, [ 0, ...CurrentDepth ]>; } & OnError : never) | boolean : V extends { [TypeSym]: "CoStream"; byMe: CoFeedEntry | undefined; } ? ({ $each: RefsToResolve, DepthLimit, [ 0, ...CurrentDepth ]>; } & OnError) | boolean : V extends { [TypeSym]: "CoPlainText" | "BinaryCoStream"; } ? boolean | OnError : V extends { [TypeSym]: "SnapshotRef"; ref: infer Item; } ? ({ ref: RefsToResolve, DepthLimit, [ 0, ...CurrentDepth ]>; } & OnError) | boolean : boolean); export type RefsToResolveStrict = [V] extends [RefsToResolve] ? RefsToResolve : V; export type Resolved | undefined = true> = DeeplyLoaded; /** * If the resolve query contains `$onError: "catch"`, we return a not loaded value for this nested CoValue. * Otherwise, the whole load operation returns a not-loaded value. */ type OnErrorResolvedValue = Depth extends { $onError: "catch"; } ? NotLoaded : never; type CoMapLikeLoaded = IsUnion> extends true ? V extends V ? CoMapLikeLoaded, DepthLimit, CurrentDepth> : never : { readonly [Key in keyof Omit]-?: Key extends CoKeys ? LoadedAndRequired extends CoValue ? DeeplyLoaded, Depth[Key], DepthLimit, [ 0, ...CurrentDepth ]> | (undefined extends V[Key] ? undefined : never) | OnErrorResolvedValue : never : never; } & V; export type DeeplyLoaded = DepthLimit extends CurrentDepth["length"] ? V : Depth extends true | undefined ? V : [ V ] extends [ReadonlyArray] ? AsLoaded extends CoValue ? Depth extends { $each: infer ItemDepth; } ? // Deeply loaded CoList ReadonlyArray, ItemDepth, DepthLimit, [ 0, ...CurrentDepth ]> | OnErrorResolvedValue, Depth["$each"]>> & V : never : V : [ V ] extends [{ [TypeSym]: "CoMap" | "Group" | "Account"; }] ? keyof Depth extends never ? V : ItemsSym extends keyof V ? Depth extends { $each: infer ItemDepth; } ? { readonly [key: string]: DeeplyLoaded, ItemDepth, DepthLimit, [ 0, ...CurrentDepth ]> | OnErrorResolvedValue, Depth["$each"]>; } & V : string extends keyof Depth ? // if at least one key is `string`, then we treat the resolve as it was empty DeeplyLoaded & V : CoMapLikeLoaded : CoMapLikeLoaded : [V] extends [ { [TypeSym]: "SnapshotRef"; ref: infer Item; } ] ? Depth extends { ref: infer ItemDepth; } ? { readonly ref: DeeplyLoaded, ItemDepth, DepthLimit, [ 0, ...CurrentDepth ]> | OnErrorResolvedValue, ItemDepth>; } & V : V : [V] extends [ { [TypeSym]: "CoStream"; byMe: CoFeedEntry | undefined; } ] ? // Deeply loaded CoStream { byMe?: { value: AsLoaded; }; inCurrentSession?: { value: AsLoaded; }; perSession: { [key: SessionID]: { value: AsLoaded; }; }; } & { [key: ID]: { value: AsLoaded; }; } & V : [V] extends [ { [TypeSym]: "BinaryCoStream"; } ] ? V : [V] extends [ { [TypeSym]: "CoPlainText"; } ] ? V : never; export {}; //# sourceMappingURL=deepLoading.d.ts.map