import type { CoValueUniqueness, RawCoList } from "cojson"; import { Account, CoFieldInit, CoValue, CoValueClass, CoValueJazzApi, Group, ID, AsLoaded, Settled, RefsToResolve, RefsToResolveStrict, Resolved, Schema, SubscribeListenerOptions, TypeSym, BranchDefinition, AnonymousJazzAgent, ItemsSym, Ref, inspect, CoValueCreateOptionsInternal, CoValueCursor, LoadCoValueCursorOption } from "../internal.js"; import { CoreCoListSchema } from "../implementation/zodSchema/schemaTypes/CoListSchema.js"; import { type LocalValidationMode } from "../implementation/zodSchema/validationSettings.js"; /** * CoLists are collaborative versions of plain arrays. * * @categoryDescription Content * You can access items on a `CoList` as if they were normal items on a plain array, using `[]` notation, etc. * * All readonly array methods are available on `CoList`. You can also use the `.$jazz` API to mutate the CoList. * * ```ts * const colorList = ColorList.create(["red", "green", "blue"]); * ``` * * ```ts * colorList[0]; * colorList.$jazz.set(3, "yellow"); * colorList.$jazz.push("Kawazaki Green"); * colorList.$jazz.splice(1, 1); * ``` * * @category CoValues */ export declare class CoList extends Array implements ReadonlyArray, CoValue { static coValueSchema?: CoreCoListSchema; $jazz: CoListJazzApi; $isLoaded: true; /** @category Type Helpers */ [TypeSym]: "CoList"; /** @internal This is only a marker type and doesn't exist at runtime */ [ItemsSym]: Item; static get [Symbol.species](): ArrayConstructor; constructor(options: { fromRaw: RawCoList; } | undefined); /** * Create a new CoList with the given initial values and owner. * * The owner (a Group or Account) determines access rights to the CoMap. * * The CoList will immediately be persisted and synced to connected peers. * * @example * ```ts * const colours = ColorList.create( * ["red", "green", "blue"], * { owner: me } * ); * const animals = AnimalList.create( * [cat, dog, fish], * { owner: me } * ); * ``` * * @category Creation * @deprecated Use `co.list(...).create` instead. **/ static create(this: CoValueClass, items: L[number][], options?: CoValueCreateOptionsInternal): L; toJSON(_key?: string, seenAbove?: ID[]): any[]; [inspect](): any[]; /** @category Internals */ static fromRaw(this: CoValueClass & typeof CoList, raw: RawCoList): V & CoList; /** * Load a `CoList` with a given ID, as a given account. * * `depth` specifies if item CoValue references should be loaded as well before resolving. * The `DeeplyLoaded` return type guarantees that corresponding referenced CoValues are loaded to the specified depth. * * You can pass `[]` or for shallowly loading only this CoList, or `[itemDepth]` for recursively loading referenced CoValues. * * Check out the `load` methods on `CoMap`/`CoList`/`CoFeed`/`Group`/`Account` to see which depth structures are valid to nest. * * @example * ```ts * const animalsWithVets = * await ListOfAnimals.load( * "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax", * me, * [{ vet: {} }] * ); * ``` * * @category Subscription & Loading * @deprecated Use `co.list(...).load` instead. */ static load = true>(this: CoValueClass, id: ID, options?: { resolve?: RefsToResolveStrict; loadAs?: Account | AnonymousJazzAgent; }): Promise>>; /** * Load and subscribe to a `CoList` with a given ID, as a given account. * * Automatically also subscribes to updates to all referenced/nested CoValues as soon as they are accessed in the listener. * * `depth` specifies if item CoValue references should be loaded as well before calling `listener` for the first time. * The `DeeplyLoaded` return type guarantees that corresponding referenced CoValues are loaded to the specified depth. * * You can pass `[]` or for shallowly loading only this CoList, or `[itemDepth]` for recursively loading referenced CoValues. * * Check out the `load` methods on `CoMap`/`CoList`/`CoFeed`/`Group`/`Account` to see which depth structures are valid to nest. * * Returns an unsubscribe function that you should call when you no longer need updates. * * Also see the `useCoState` hook to reactively subscribe to a CoValue in a React component. * * @example * ```ts * const unsub = ListOfAnimals.subscribe( * "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax", * me, * { vet: {} }, * (animalsWithVets) => console.log(animalsWithVets) * ); * ``` * * @category Subscription & Loading * @deprecated Use `co.list(...).subscribe` instead. */ static subscribe = true>(this: CoValueClass, id: ID, listener: (value: Resolved, unsubscribe: () => void) => void): () => void; static subscribe = true>(this: CoValueClass, id: ID, options: SubscribeListenerOptions, listener: (value: Resolved, unsubscribe: () => void) => void): () => void; /** @deprecated Use `CoList.upsertUnique` and `CoList.loadUnique` instead. */ static findUnique(this: CoValueClass, unique: CoValueUniqueness["uniqueness"], ownerID: ID | ID, as?: Account | Group | AnonymousJazzAgent): `co_z${string}`; /** * Get an existing unique CoList or create a new one if it doesn't exist. * * Unlike `upsertUnique`, this method does NOT update existing values with the provided value. * The provided value is only used when creating a new CoList. * * @example * ```ts * const items = await ItemList.getOrCreateUnique({ * value: [item1, item2, item3], * unique: ["user-items", me.id], * owner: me, * }); * ``` * * @param options The options for creating or loading the CoList. * @returns Either an existing CoList (unchanged), or a new initialised CoList if none exists. * @category Subscription & Loading */ static getOrCreateUnique = true>(this: CoValueClass, options: { value: L[number][]; unique: CoValueUniqueness["uniqueness"]; owner: Account | Group; resolve?: RefsToResolveStrict; }): Promise>>; /** * Given some data, updates an existing CoList or initialises a new one if none exists. * * Note: This method respects resolve options, and thus can return a not-loaded value if the references cannot be resolved. * * @example * ```ts * const activeItems = await ItemList.upsertUnique( * { * value: [item1, item2, item3], * unique: sourceData.identifier, * owner: workspace, * } * ); * ``` * * @param options The options for creating or loading the CoList. This includes the intended state of the CoList, its unique identifier, its owner, and the references to resolve. * @returns Either an existing & modified CoList, or a new initialised CoList if none exists. * @category Subscription & Loading * * @deprecated Use `getOrCreateUnique` instead. Note: getOrCreateUnique does not update existing values. * If you need to update, use getOrCreateUnique followed by `$jazz.applyDiff`. */ static upsertUnique = true>(this: CoValueClass, options: { value: L[number][]; unique: CoValueUniqueness["uniqueness"]; owner: Account | Group; resolve?: RefsToResolveStrict; }): Promise>>; /** * Loads a CoList by its unique identifier and owner's ID. * @param unique The unique identifier of the CoList to load. * @param ownerID The ID of the owner of the CoList. * @param options Additional options for loading the CoList. * @returns The loaded CoList, or an not-loaded value if unavailable. * * @category Subscription & Loading */ static loadUnique = true>(this: CoValueClass, unique: CoValueUniqueness["uniqueness"], ownerID: ID | ID, options?: { resolve?: RefsToResolveStrict; loadAs?: Account | AnonymousJazzAgent; }): Promise>>; /** * @deprecated Use `.$jazz.push` instead. */ push(...items: never): never; /** * @deprecated Use `.$jazz.unshift` instead. */ unshift(...items: never): number; /** * @deprecated Use `.$jazz.pop` instead. */ pop(value: never): never; /** * @deprecated Use `.$jazz.shift` instead. */ shift(value: never): never; /** * @deprecated Use `.$jazz.splice` instead. */ splice(start: never, deleteCount: never, ...items: never): never; /** * @deprecated Use `.$jazz.set` instead. */ copyWithin(target: never, start: never, end: never): never; /** * @deprecated Use `.$jazz.set` instead. */ fill(value: never, start?: never, end?: never): never; /** * @deprecated Use `.toReversed` if you want a reversed copy, or `.$jazz.set` to mutate the CoList. */ reverse(value: never): never; /** * @deprecated Use `.toSorted()` if you want a sorted copy, or `.$jazz.set` to mutate the CoList. */ sort(compareFn?: never): never; } /** @internal */ type CoListItem = L extends CoList ? L[number] : never; export declare class CoListJazzApi extends CoValueJazzApi { private coList; private getRaw; private coListSchema; constructor(coList: L, getRaw: () => RawCoList, coListSchema: CoreCoListSchema); private getItemSchema; /** @category Collaboration */ get owner(): Group; set(index: number, value: CoFieldInit>, options?: { validation?: LocalValidationMode; }): void; /** * Appends new elements to the end of an array, and returns the new length of the array. * @param items New elements to add to the array. * * @category Content */ push(...items: CoFieldInit>[]): number; /** * Appends new elements to the end of an array, and returns the new length of the array. * Schema validation is not applied to the items. * @param items New elements to add to the array. * * @category Content */ pushLoose(...items: CoFieldInit>[]): number; /** * Inserts new elements at the start of an array, and returns the new length of the array. * @param items Elements to insert at the start of the array. * * @category Content */ unshift(...items: CoFieldInit>[]): number; /** * Inserts new elements at the start of an array, and returns the new length of the array. * Schema validation is not applied to the items. * @param items Elements to insert at the start of the array. * * @category Content */ unshiftLoose(...items: CoFieldInit>[]): number; /** * Removes the last element from an array and returns it. * If the array is empty, undefined is returned and the array is not modified. * * @category Content */ pop(): CoListItem | undefined; /** * Removes the first element from an array and returns it. * If the array is empty, undefined is returned and the array is not modified. * * @category Content */ shift(): CoListItem | undefined; /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * Items are validated using the schema. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. * @param items Elements to insert into the array in place of the deleted elements. * @returns An array containing the elements that were deleted. * * @category Content */ splice(start: number, deleteCount: number, ...items: CoFieldInit>[]): CoListItem[]; /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. * @param items Elements to insert into the array in place of the deleted elements. * @returns An array containing the elements that were deleted. * * @category Content */ spliceLoose(start: number, deleteCount: number, ...items: CoFieldInit>[]): CoListItem[]; /** * Removes the elements at the specified indices from the array. * @param indices The indices of the elements to remove. * @returns The removed elements. * * @category Content */ remove(...indices: number[]): CoListItem[]; /** * Removes the elements matching the predicate from the array. * @param predicate The predicate to match the elements to remove. * @returns The removed elements. * * @category Content */ remove(predicate: (item: CoListItem, index: number, coList: L) => boolean): CoListItem[]; /** * Retains only the elements matching the predicate from the array. * @param predicate The predicate to match the elements to retain. * @returns The removed elements. * * @category Content */ retain(predicate: (item: CoListItem, index: number, coList: L) => boolean): CoListItem[]; /** * Modify the `CoList` to match another list, where the changes are managed internally. * * Changes are detected using `Object.is` for non-collaborative values and `$jazz.id` for collaborative values. * * @param result - The resolved list of items. For collaborative values, both CoValues and JSON values are supported. * @returns The modified CoList. * * @category Content */ applyDiff(result: CoFieldInit>[], options?: { validation?: LocalValidationMode; }): L; /** * Given an already loaded `CoList`, ensure that items are loaded to the specified depth. * * Works like `CoList.load()`, but you don't need to pass the ID or the account to load as again. * * @category Subscription & Loading */ ensureLoaded>(this: CoListJazzApi, options: { resolve: RefsToResolveStrict; unstable_branch?: BranchDefinition; cursor?: LoadCoValueCursorOption; }): Promise>; /** * Given an already loaded `CoList`, subscribe to updates to the `CoList` and ensure that items are loaded to the specified depth. * * Works like `CoList.subscribe()`, but you don't need to pass the ID or the account to load as again. * * Returns an unsubscribe function that you should call when you no longer need updates. * * @category Subscription & Loading **/ subscribe = true>(this: CoListJazzApi, listener: (value: Resolved, unsubscribe: () => void) => void): () => void; subscribe = true>(this: CoListJazzApi, options: { resolve?: RefsToResolveStrict; unstable_branch?: BranchDefinition; cursor?: CoValueCursor; }, listener: (value: Resolved, unsubscribe: () => void) => void): () => void; /** * Wait for the `CoList` to be uploaded to the other peers. * * @category Subscription & Loading */ waitForSync(options?: { timeout?: number; }): Promise; /** * Get the descriptor for the items in the `CoList` * @internal */ getItemsDescriptor(): Schema; /** * If a `CoList`'s items are a `coField.ref(...)`, you can use `coList.$jazz.refs[i]` to access * the `Ref` instead of the potentially loaded/null value. * * This allows you to always get the ID or load the value manually. * * @example * ```ts * animals.$jazz.refs[0].id; // => ID * animals.$jazz.refs[0].value; * // => Animal | null * const animal = await animals.$jazz.refs[0].load(); * ``` * * @category Content **/ get refs(): { [idx: number]: AsLoaded> extends CoValue ? Ref>> : never; } & { length: number; [Symbol.iterator](): IterableIterator> extends CoValue ? Ref>> : never>; }; /** * Get the edits made to the CoList. * * @category Collaboration */ getEdits(): { [idx: number]: { value?: CoListItem; ref?: CoListItem extends CoValue ? Ref> : never; by: Account | null; madeAt: Date; }; }; /** @internal */ get raw(): RawCoList; } export {}; //# sourceMappingURL=coList.d.ts.map