import { CollectionQuery, Models } from '@triplit/db'; import { DBChanges } from './types.js'; import { DB } from './db.js'; import { EntityDataStore } from './entity-data-store.js'; import { BTreeKVStore } from './kv-store/memory-btree.js'; interface SubscribedQueryInfo { ogQuery: CollectionQuery; query: CollectionQuery; listeners: Set; uninitializedListeners: WeakSet; results?: any[]; capturedChanges?: DBChanges; hasChanged?: boolean; } type SubscriptionCallback = (update: { results: any[]; changes: DBChanges; }) => void; export interface IVMOptions { shouldTrackChanges?: boolean; } export declare class IVM { readonly db: DB; readonly options: IVMOptions; changeBuffer: DBChanges; storage: BTreeKVStore; entityStore: EntityDataStore; readonly subscribedQueries: Map; private queryNodes; constructor(db: DB, options: IVMOptions); subscribe(query: CollectionQuery, callback: SubscriptionCallback): () => void; private initializeQueryResults; flushChangesToListeners(): void; private createQueryNodesForRootQuery; bufferChanges(changes: DBChanges): void; updateViews(): Promise; private handlePotentialResultAdditions; private handlePotentialResultUpdate; private handlePotentialResultEviction; clear(): Promise; } export declare function queryResultsToChanges(results: any[], query: CollectionQuery, changes?: DBChanges): DBChanges; export declare function createQueryWithExistsAddedToIncludes(query: CollectionQuery): CollectionQuery; export {}; /** * # Notes and TODOS * 1. Initial fetch [DONE!] * - This is tricky because we want subscribers that subscribe to an existing query * - after it's been initialized to get the initial results as changes, however, the * - results themselves are not * 2. Batch optimizations * - There's probably a good deal of work we can cut out by observing that a bunch of changes * - can be processed together e.g. inserts are guaranteed to be not match "before" and more so * - if a matching query is a root query with no subqueries they can be added directly to * - the results and even more so if they query has no limit or order it can just be * - quickly appended * 3. Stateful fetch * - Our current idea here is to send entityIds that the client has for a given query * - which actually could be quite efficient because we can check to see if those entityIds * - are still in the result set and, for the ones that aren't, we can fetch them for the client * - to invalidate. Effectively this is doing a diff between the client's state and * - the server's state */