import { EntityValidationProblem, EventSubscriber, ObjectIdentifier, Operation, StorageFieldSchema } from '@verdant-web/common'; import { Context } from '../context/context.js'; import { FileManager } from '../files/FileManager.js'; import { ClientWithCollections } from '../index.js'; import { EntityCache } from './EntityCache.js'; import { EntityFamilyMetadata, EntityMetadataView } from './EntityMetadata.js'; import { EntityStoreEvents } from './EntityStore.js'; import { AnyEntity, BaseEntityValue, DataFromInit, EntityChange, EntityEvents, ListEntity, ListItemInit, ListItemValue, ObjectEntity } from './types.js'; export interface EntityInit { oid: ObjectIdentifier; schema: StorageFieldSchema; entityFamily?: EntityCache; metadataFamily: EntityFamilyMetadata; parent?: Entity; ctx: Context; files: FileManager; readonlyKeys?: string[]; fieldPath?: (string | number)[]; storeEvents: EntityStoreEvents; deleteSelf: () => void; } declare const PRIVATE_ENTITY_CONTEXT_KEY: unique symbol; export declare class Entity> extends EventSubscriber implements ObjectEntity, ListEntity { readonly oid: ObjectIdentifier; private readonlyKeys; private fieldPath; private entityFamily; private metadataFamily; readonly schema: StorageFieldSchema; private parent; private ctx; private files; private storeEvents; get [PRIVATE_ENTITY_CONTEXT_KEY](): Context; private _viewData; private validationError; private cachedDeepUpdatedAt; private wasDeletedLastChange; private cachedView; private _deleteSelf; private _deleting; constructor({ oid, schema, entityFamily: childCache, parent, ctx, metadataFamily, readonlyKeys, files, storeEvents, deleteSelf, fieldPath, }: EntityInit); private onAdd; private onReplace; private onResetAll; private get metadata(); private get patchCreator(); /** * The view of this Entity, not including nested * entities (that's the snapshot - see #getSnapshot()) * * Nested entities are represented by refs. */ private get viewData(); /** convenience getter for viewData.view */ private get rawView(); /** * An Entity's View includes the rendering of its underlying data, * connecting of children where refs were, and validation * and pruning according to schema. */ private get view(); private childIsNull; get uid(): string; get deleted(): boolean; get invalid(): boolean; /** * Returns true if this or any child is invalid (pruned) */ get deepInvalid(): boolean; get isList(): any; get updatedAt(): number; get deepUpdatedAt(): number; /** * @internal - this is relevant to Verdant's system, not users. * * Indicates whether this document is from an outdated version * of the schema - which means it cannot be used until it is upgraded. */ get isOutdatedVersion(): boolean; /** * Returns the storage namespace this entity came from. For example, if you * have multiple stores initialized from the same schema, you can use this * to figure out where an isolated entity was created / stored. */ get namespace(): string; /** * The authz string signifying the permissions this entity has. * On the client (where we are) it's only ever possible to see * an entity with either full access or access for the current * user. */ get access(): import("@verdant-web/common").AuthorizationKey | undefined; get isAuthorized(): boolean; /** * Introspects the schema of a child field of this entity. */ getFieldSchema: (key: any) => StorageFieldSchema; /** * Pruning - when entities have invalid children, we 'prune' that * data up to the nearest prunable point - a nullable field, * or a list. */ protected validate: (...args: unknown[]) => EntityValidationProblem | undefined; private viewWithMappedChildren; private rawViewWithMappedChildren; /** * A current snapshot of this Entity's data, including nested * Entities. */ getSnapshot: () => any; /** * A snapshot of this Entity with unpruned (invalid) data. This will * not conform to the entity schema and should be used carefully. * * Can be used to inspect or recover invalid, pruned data not * otherwise accessible. */ getUnprunedSnapshot: () => any; private addPendingOperations; private applyPendingOperations; private getPruneDiff; private canonizePrunedVersion; private addConfirmedData; private replaceAllData; private resetAllData; private invalidateCachedView; private invalidate; private change; private changeRoot; private changeNested; protected deepChange: (target: Entity, ev: EntityChange) => void; private createFileContext; private getChild; subscribeToField: (key: K, event: "change", callback: (value: KeyValue[K], info: { previousValue: KeyValue[K]; isLocal?: boolean; }) => void) => () => void; /** * Gets a value from this Entity. If the value * is an object, it will be wrapped in another * Entity. */ get: (key: Key) => KeyValue[Key]; private processPrunedChild; /** * Gets a value on this entity. If the value is not * present, it will be set to the provided default * and returned synchronously. This method only sets * a new value once when a field is empty; subsequent * calls will retrieve the created value until it is * deleted. * * Note that this should only be called for nullable * fields. If the field is not nullable, the existing * value or the default value will always be returned, * and the default will not be set. */ getOrSet: (key: Key, init: Init[Key]) => KeyValue[Key]; private processInputValue; private getDeleteMode; /** * Returns the referent value of an item in the list, used for * operations which act on items. if the item is an object, * it will attempt to create an OID reference to it. If it * is a primitive, it will return the primitive. */ private getItemRefValue; set: (key: any, value: any, options?: { /** * Forces the creation of a change for this set even if the value is the same * as the current value for this key. This has an effect in situations where * offline changes from others are interleaved with local changes; when setting * a value to its current value (force=true), if that value were retroactively changed from * offline changes, the set would put it back to the value specified. If the * set is ignored because the value is the same (force=false), then retroactive * changes will be preserved. */ force: boolean; }) => void; /** * Returns a destructured version of this Entity, where child * Entities are accessible at their respective keys. */ getAll: () => KeyValue; delete: (key: any) => void; keys: () => string[]; entries: () => [string, Exclude][]; values: () => Exclude[]; get size(): number; update: (data: any, { merge, replaceSubObjects, preserveUndefined, dangerouslyDisableMerge, }?: { replaceSubObjects?: boolean; merge?: boolean; preserveUndefined?: boolean; dangerouslyDisableMerge?: boolean; }) => void; get length(): number; push: (value: ListItemInit) => void; insert: (index: number, value: ListItemInit) => void; move: (from: number, to: number) => void; moveItem: (item: ListItemValue, to: number) => void; add: (value: ListItemValue) => void; removeAll: (item: ListItemValue) => void; removeFirst: (item: ListItemValue) => void; removeLast: (item: ListItemValue) => void; [Symbol.iterator](): { next: () => { readonly value: ListItemValue; readonly done: false; } | { readonly value: undefined; readonly done: true; }; }; map: (callback: (value: ListItemValue, index: number) => U) => U[]; filter: (callback: (value: ListItemValue, index: number) => boolean) => ListItemValue[]; has: (value: ListItemValue) => boolean; forEach: (callback: (value: ListItemValue, index: number) => void) => void; reduce: (callback: (previousValue: U, currentValue: ListItemValue, index: number) => U, initialValue: U) => U; some: (predicate: (value: ListItemValue) => boolean) => boolean; every: (predicate: (value: ListItemValue) => boolean) => boolean; find: (predicate: (value: ListItemValue) => boolean) => ListItemValue | undefined; includes: (value: ListItemValue) => boolean; /** * Deletes this entity. WARNING: this can be tricky to * use correctly. You must not reference this entity * instance in any way after the deletion happens, or * you will get an error! * * It's a little easier to delete using client.delete * if you can manage it with your app's code. For example, * in React, use hooks.useClient() to get the client and * call delete from there. */ deleteSelf: () => void; __getViewData__: (oid: ObjectIdentifier, type: "confirmed" | "pending") => EntityMetadataView; __getFamilyOids__: () => string[]; __discardPendingOperation__: (operation: Operation) => void; __markDeleting__: () => void; } export declare function getEntityClient(entity: AnyEntity): ClientWithCollections; export {};