* Creates a new Collection instance * * @param config - Configuration object for the collection * @throws Error if sync config is missing
(config: CollectionConfig<TOutput, TKey, TSchema>)
| 317 | * @throws Error if sync config is missing |
| 318 | */ |
| 319 | constructor(config: CollectionConfig<TOutput, TKey, TSchema>) { |
| 320 | // eslint-disable-next-line |
| 321 | if (!config) { |
| 322 | throw new CollectionRequiresConfigError() |
| 323 | } |
| 324 | |
| 325 | // eslint-disable-next-line |
| 326 | if (!config.sync) { |
| 327 | throw new CollectionRequiresSyncConfigError() |
| 328 | } |
| 329 | |
| 330 | if (config.id) { |
| 331 | this.id = config.id |
| 332 | } else { |
| 333 | this.id = safeRandomUUID() |
| 334 | } |
| 335 | |
| 336 | // Set default values for optional config properties |
| 337 | this.config = { |
| 338 | ...config, |
| 339 | autoIndex: config.autoIndex ?? `off`, |
| 340 | } |
| 341 | |
| 342 | if (this.config.autoIndex === `eager` && !config.defaultIndexType) { |
| 343 | throw new CollectionConfigurationError( |
| 344 | `autoIndex: 'eager' requires defaultIndexType to be set. ` + |
| 345 | `Import an index type and set it:\n` + |
| 346 | ` import { BasicIndex } from '@tanstack/db'\n` + |
| 347 | ` createCollection({ defaultIndexType: BasicIndex, autoIndex: 'eager', ... })`, |
| 348 | ) |
| 349 | } |
| 350 | |
| 351 | this._changes = new CollectionChangesManager() |
| 352 | this._events = new CollectionEventsManager() |
| 353 | this._indexes = new CollectionIndexesManager() |
| 354 | this._lifecycle = new CollectionLifecycleManager(config, this.id) |
| 355 | this._mutations = new CollectionMutationsManager(config, this.id) |
| 356 | this._state = new CollectionStateManager(config) |
| 357 | this._sync = new CollectionSyncManager(config, this.id) |
| 358 | |
| 359 | this.comparisonOpts = buildCompareOptionsFromConfig(config) |
| 360 | |
| 361 | this._changes.setDeps({ |
| 362 | collection: this, // Required for passing to CollectionSubscription |
| 363 | lifecycle: this._lifecycle, |
| 364 | sync: this._sync, |
| 365 | events: this._events, |
| 366 | state: this._state, // Required for enriching changes with virtual properties |
| 367 | }) |
| 368 | this._events.setDeps({ |
| 369 | collection: this, // Required for adding to emitted events |
| 370 | }) |
| 371 | this._indexes.setDeps({ |
| 372 | state: this._state, |
| 373 | lifecycle: this._lifecycle, |
| 374 | defaultIndexType: config.defaultIndexType, |
| 375 | events: this._events, |
| 376 | }) |
nothing calls this directly
no test coverage detected