@dittolive/ditto 4.0.2-experimental.shutdown-support.2.darwin-arm64 → 4.0.2-experimental.shutdown-support.3.darwin-arm64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/types/ditto.d.ts CHANGED
@@ -68,7 +68,6 @@ declare class ObserverManager {
68
68
  private hasObservers;
69
69
  private registerIfNeeded;
70
70
  private unregisterIfNeeded;
71
- private finalize;
72
71
  }
73
72
 
74
73
  /** @internal */
@@ -1313,724 +1312,746 @@ declare class Presence {
1313
1312
  private observerManager;
1314
1313
  }
1315
1314
 
1316
- /** Types of connections that can be established between two peers. */
1317
- type TransportCondition = 'Unknown' | 'OK' | 'GenericFailure' | 'AppInBackground' | 'MDNSFailure' | 'TCPListenFailure' | 'NoBLECentralPermission' | 'NoBLEPeripheralPermission' | 'CannotEstablishConnection' | 'BLEDisabled' | 'NoBLEHardware' | 'WiFiDisabled' | 'TemporarilyUnavailable';
1318
- /** The source for a transport condition. */
1319
- type ConditionSource = 'BLE' | 'TCP' | 'AWDL' | 'MDNS';
1320
- /**
1321
- * Types of connections that can be established between two peers.
1322
- * @deprecated replaced by {@link ConnectionType}.
1323
- */
1324
- type PresenceConnectionType = 'WiFi' | 'WebSocket' | 'AWDL' | 'BLE';
1325
- /**
1326
- * A peer object with information about an observed peer.
1327
- * @deprecated replaced by {@link Peer}.
1328
- */
1329
- type RemotePeer = {
1330
- networkID: string;
1331
- deviceName: string;
1332
- rssi?: number;
1333
- approximateDistanceInMeters?: number;
1334
- connections: PresenceConnectionType[];
1335
- };
1336
1315
  /**
1337
- * Ditto is the entry point for accessing Ditto-related functionality.
1316
+ * Used to subscribe to receive updates from remote peers about matching
1317
+ * documents.
1318
+ *
1319
+ * While {@link Subscription} objects remain in scope they ensure that
1320
+ * documents in the collection specified and that match the query provided will
1321
+ * try to be kept up-to-date with the latest changes from remote peers.
1338
1322
  */
1339
- declare class Ditto {
1340
- /**
1341
- * Configure a custom identifier for the current device.
1342
- *
1343
- * When using {@link Presence.observe | presence.observe()}, each remote peer is
1344
- * represented by a short UTF-8 "device name". By default this will be a
1345
- * truncated version of the device's hostname. It does not need to be unique
1346
- * among peers. Configure the device name before calling
1347
- * {@link startSync | startSync()}. If it is too long it may be truncated.
1348
- */
1349
- deviceName: string;
1350
- /** Returns a string identifying the version of the Ditto SDK. */
1351
- get sdkVersion(): string;
1323
+ declare class Subscription {
1352
1324
  /**
1353
- * The (validated) identity this Ditto instance was initialized with.
1325
+ * The query that the subscription is based on.
1354
1326
  */
1355
- readonly identity: Identity;
1327
+ readonly query: string;
1356
1328
  /**
1357
- * The path this Ditto instance was initialized with, if no path was given at
1358
- * construction time, the default value is returned (see constructor).
1329
+ * Returns `true` if subscription has been explicitly cancelled, `false`
1330
+ * otherwise.
1359
1331
  */
1360
- readonly path: string;
1332
+ readonly isCancelled = false;
1361
1333
  /**
1362
- * Provides access to the SDK's store functionality.
1334
+ * The name of the collection that the subscription is based on.
1363
1335
  */
1364
- readonly store: Store;
1336
+ get collectionName(): string;
1365
1337
  /**
1366
- * Provides access to the SDK's presence functionality.
1338
+ * Cancels a subscription and releases all associated resources.
1367
1339
  */
1368
- readonly presence: Presence;
1340
+ cancel(): void;
1341
+ /** @internal */
1342
+ constructor(collection: Collection, query: string, queryArgsCBOR: Uint8Array | null, orderBys: OrderBy[], limit: number, offset: number);
1369
1343
  /**
1370
- * Provides access to authentication methods for logging on to Ditto Cloud.
1344
+ * The collection this subscription belongs to.
1345
+ * @internal Because not exposed in any of the other SDKs (yet?).
1371
1346
  */
1372
- readonly auth: Authenticator;
1347
+ readonly collection: Collection;
1373
1348
  /**
1374
- * The site ID that the instance of `Ditto` is using as part of its identity.
1349
+ * The corresponding named arguments for {@link query}, if any.
1350
+ * @internal Because not exposed in any of the other SDKs (yet?).
1375
1351
  */
1376
- readonly siteID: number | BigInt;
1352
+ readonly queryArgsCBOR: Uint8Array | null;
1353
+ private static finalizationRegistry;
1354
+ private static add;
1355
+ private static remove;
1356
+ private contextInfo;
1357
+ }
1358
+
1359
+ /**
1360
+ * An object that describes how a document's position in a live query's list of
1361
+ * matching documents has changed since the previous live query event.
1362
+ */
1363
+ interface LiveQueryMove {
1377
1364
  /**
1378
- * Returns `true` if an offline license token has been set, otherwise returns `false`.
1379
- *
1380
- * @see {@link setOfflineOnlyLicenseToken | setOfflineOnlyLicenseToken()}
1365
+ * The index of the document in the list of matching documents from the
1366
+ * previous live query event.
1381
1367
  */
1382
- readonly isActivated: boolean;
1368
+ readonly from: number;
1383
1369
  /**
1384
- * `true` once {@link close | Ditto.close()} has been called, otherwise
1385
- * `false`.
1370
+ * The index of the document in the list of matching documents from the new
1371
+ * live query event.
1386
1372
  */
1387
- get isClosed(): boolean;
1373
+ readonly to: number;
1374
+ }
1375
+ /** @internal */
1376
+ interface LiveQueryEventUpdateParams {
1377
+ oldDocuments: Document[];
1378
+ insertions: number[];
1379
+ deletions: number[];
1380
+ updates: number[];
1381
+ moves: LiveQueryMove[];
1382
+ }
1383
+ /**
1384
+ * First event fired immediately after registering a live query without any
1385
+ * mutations. All subsequent events are of type {@link LiveQueryEventUpdate}.
1386
+ */
1387
+ declare class LiveQueryEventInitial {
1388
1388
  /**
1389
- * Returns `true` if sync is active, otherwise returns `false`. Use
1390
- * {@link startSync | startSync()} to activate and {@link stopSync | stopSync()}
1391
- * to deactivate sync.
1389
+ * Whether or not this is the initial event being delivered. Always `true`
1390
+ * for `LiveQueryEventInitial`.
1392
1391
  */
1393
- readonly isSyncActive: boolean;
1392
+ readonly isInitial = true;
1394
1393
  /**
1395
- * Initializes a new `Ditto` instance.
1396
- *
1397
- * **NOTE**: The `sharedKey` identity is only supported for Node environments,
1398
- * using this to create a Ditto instance in the web browser will throw an
1399
- * exception.
1400
- *
1401
- * @param identity - Identity for the new Ditto instance, defaults to
1402
- * `offlinePlayground` with `appID` being the empty string `''`.
1403
- *
1404
- * @param path - On Node, `path` corresponds to a real directory
1405
- * on the file system (intermediate directories are created if needed). In the
1406
- * browser, `path` is used as an internal namespace for the in-memory storage.
1407
- * Defaults to `"ditto"`.
1408
- *
1409
- * @see {@link Ditto.identity}
1410
- * @see {@link Ditto.path}
1394
+ * Returns a hash that represents the set of matching documents.
1411
1395
  */
1412
- constructor(identity?: Identity, path?: string);
1396
+ hash(documents: Document[]): BigInt;
1413
1397
  /**
1414
- * Check if the current environment supports running Ditto.
1415
- *
1416
- * Required APIs include:
1417
- *
1418
- * - `BigInt`
1419
- * - `FinalizationRegistry`
1420
- * - `WeakRef`
1421
- *
1422
- * Internet Explorer is not supported.
1423
- *
1424
- * @returns true if the environment is supported
1398
+ * Returns a pattern of words that together create a mnemonic, which
1399
+ * represents the set of matching documents.
1425
1400
  */
1426
- static isEnvironmentSupported(): boolean;
1401
+ hashMnemonic(documents: Document[]): string;
1402
+ }
1403
+ /**
1404
+ * Represents an update event describing all changes that occured for documents
1405
+ * covered by a (live) query.
1406
+ */
1407
+ declare class LiveQueryEventUpdate {
1427
1408
  /**
1428
- * Activate a `Ditto` instance by setting an offline only license token. You cannot initiate sync
1429
- * with `Ditto` before you have activated it. The offline license token is only valid for identities
1430
- * of type `development`, `manual`, `offlinePlayground`, and `sharedKey`.
1431
- *
1432
- * @param licenseToken the license token to activate the `Ditto` instance
1433
- * with. You can find yours on the [Ditto portal](https://portal.ditto.live).
1409
+ * Whether or not this is the initial event being delivered. Always `false`
1410
+ * for `LiveQueryEventUpdate`.
1434
1411
  */
1435
- setOfflineOnlyLicenseToken(licenseToken: string): void;
1412
+ readonly isInitial = false;
1436
1413
  /**
1437
- * Returns the current transport configuration, frozen. If you want to modify
1438
- * the transport config, make a {@link TransportConfig.copy | copy} first. Or
1439
- * use the {@link updateTransportConfig | updateTransportConfig()}
1440
- * convenience method. By default peer-to-peer transports (Bluetooth, WiFi,
1441
- * and AWDL) are enabled if available in the current environment
1442
- * (Web, Node, OS, etc.).
1443
- *
1444
- * @see {@link setTransportConfig | setTransportConfig()}
1445
- * @see {@link updateTransportConfig | updateTransportConfig()}
1414
+ * The documents that previously matched the query, before the latest event.
1446
1415
  */
1447
- readonly transportConfig: TransportConfig;
1416
+ readonly oldDocuments: Document[];
1448
1417
  /**
1449
- * Assigns a new transports configuration. By default peer-to-peer transports
1450
- * (Bluetooth, WiFi, and AWDL) are enabled. You may use this method to alter
1451
- * the configuration at any time, however sync will not begin until
1452
- * {@link startSync | startSync()} is called.
1453
- *
1454
- * @see {@link transportConfig}
1455
- * @see {@link updateTransportConfig | updateTransportConfig()}
1418
+ * The indexes in the array of matching documents that accompany this event,
1419
+ * which relate to a document that was not in the previous most recent list of
1420
+ * matching documents.
1456
1421
  */
1457
- setTransportConfig(transportConfig: TransportConfig): void;
1422
+ readonly insertions: number[];
1458
1423
  /**
1459
- * Convenience method for updating the transport config. Creates a copy of the
1460
- * current transport config, passes that copy to the `update` closure,
1461
- * allowing it to mutate as needed, and sets that updated copy afterwards.
1424
+ * The indexes into the array {@link oldDocuments}, which relate to a document
1425
+ * that was in the previous most recent list of matching documents but is no
1426
+ * longer a matching document.
1462
1427
  */
1463
- updateTransportConfig(update: (transportConfig: TransportConfig) => void): Ditto;
1428
+ readonly deletions: number[];
1464
1429
  /**
1465
- * Starts the network transports. Ditto will connect to other devices.
1466
- *
1467
- * By default Ditto will enable all peer-to-peer transport types. On **Node**,
1468
- * this means BluetoothLE, WiFi/LAN, and AWDL. On the **Web**, only connecting
1469
- * via Websockets is supported. The network configuration can be
1470
- * customized with {@link updateTransportConfig | updateTransportConfig()}
1471
- * or replaced entirely with {@link setTransportConfig | setTransportConfig()}.
1472
- *
1473
- *
1474
- * Ditto will prevent the process from exiting until sync is stopped (not
1475
- * relevant when running in the browser).
1476
- *
1477
- * **NOTE**: the BluetoothLE transport on Linux is experimental, this
1478
- * method panics if no BluetoothLE hardware is available. Therefore, contrary
1479
- * to the above, the BluetoothLE transport is temporarily disabled by default
1480
- * on Linux.
1481
- *
1482
- * @see {@link isSyncActive}
1483
- * @see {@link stopSync | stopSync()}
1430
+ * The indexes in the array of matching documents that accompany this event,
1431
+ * which relate to a document that has been updated since the previous live
1432
+ * query event.
1484
1433
  */
1485
- startSync(): void;
1434
+ readonly updates: number[];
1486
1435
  /**
1487
- * Stops all network transports.
1488
- *
1489
- * You may continue to use the database locally but no data will sync to or
1490
- * from other devices.
1491
- *
1492
- * @see {@link isSyncActive}
1493
- * @see {@link startSync | startSync()}
1436
+ * Objects that describe how documents' positions in the list of matching
1437
+ * documents have changed since the previous live query event.
1494
1438
  */
1495
- stopSync(): void;
1439
+ readonly moves: LiveQueryMove[];
1496
1440
  /**
1497
- * Registers an observer for info about Ditto peers in range of this device.
1498
- *
1499
- * Ditto will prevent the process from exiting as long as there are active
1500
- * peers observers (not relevant when running in the browser).
1501
- *
1502
- * @param callback called immediately with the current state of peers
1503
- * in range and whenever that state changes. Then it will be invoked
1504
- * repeatedly when Ditto devices come and go, or the active connections to
1505
- * them change.
1506
- *
1507
- * @deprecated please use {@link Presence.observe | presence.observe()} instead.
1441
+ * Returns a hash that represents the set of matching documents.
1508
1442
  */
1509
- observePeers(callback: (peersData: RemotePeer[]) => void): Observer;
1443
+ hash(documents: Document[]): BigInt;
1510
1444
  /**
1511
- * Register observer for changes of underlying transport conditions.
1512
- *
1513
- * Ditto will prevent the process from exiting as long as there are active
1514
- * transport conditions observers (not relevant when running in the browser).
1515
- *
1516
- * @param callback called when underlying transport conditions change with
1517
- * the changed `condition` and it's `source`.
1445
+ * Returns a pattern of words that together create a mnemonic, which
1446
+ * represents the set of matching documents.
1518
1447
  */
1519
- observeTransportConditions(callback: (condition: TransportCondition, source: ConditionSource) => void): Observer;
1448
+ hashMnemonic(documents: Document[]): string;
1449
+ /** @internal */
1450
+ constructor(params: LiveQueryEventUpdateParams);
1451
+ }
1452
+ /**
1453
+ * Represents events delivered by a {@link LiveQuery}, which can be initial
1454
+ * (fired immediately upon registration) or an update (all subsequent events).
1455
+ */
1456
+ type LiveQueryEvent = LiveQueryEventInitial | LiveQueryEventUpdate;
1457
+ /**
1458
+ * Provides information about a live query event relating to a single document
1459
+ * live query.
1460
+ */
1461
+ declare class SingleDocumentLiveQueryEvent {
1520
1462
  /**
1521
- * Removes all sync metadata for any remote peers which aren't currently
1522
- * connected. This method shouldn't usually be called. Manually running
1523
- * garbage collection often will result in slower sync times. Ditto
1524
- * automatically runs a garbage a collection process in the background at
1525
- * optimal times.
1526
- *
1527
- * Manually running garbage collection is typically only useful during testing
1528
- * if large amounts of data are being generated. Alternatively, if an entire
1529
- * data set is to be evicted and it's clear that maintaining this metadata
1530
- * isn't necessary, then garbage collection could be run after evicting the
1531
- * old data.
1532
- *
1533
- * Only available in Node environments at the moment, no-op in the browser.
1463
+ * Whether or not this is the initial event being delivered.
1534
1464
  */
1535
- runGarbageCollection(): void;
1465
+ readonly isInitial: boolean;
1466
+ /** The old representation of the document with the relveant document ID. */
1467
+ readonly oldDocument?: Document;
1536
1468
  /**
1537
- * Explicitly opt-in to disabling the ability to sync with Ditto peers running
1538
- * any version of the SDK in the v3 (or lower) series of releases.
1539
- *
1540
- * Assuming this succeeds then this peer will only be able to sync with other
1541
- * peers using SDKs in the v4 (or higher) series of releases. Note that this
1542
- * disabling of sync spreads to peers that sync with a peer that has disabled,
1543
- * or has (transitively) had disabled, syncing with v3 SDK peers.
1469
+ * Returns a hash that represents the set of matching documents.
1544
1470
  */
1545
- disableSyncWithV3(): void;
1471
+ hash(document: Document | null): BigInt;
1546
1472
  /**
1547
- * Shut down Ditto and release all resources.
1548
- *
1549
- * Must be called before recreating a Ditto instance that uses the same
1550
- * persistence directory.
1473
+ * Returns a pattern of words that together create a mnemonic, which
1474
+ * represents the set of matching documents.
1551
1475
  */
1552
- close(): Promise<void>;
1476
+ hashMnemonic(document: Document | null): string;
1553
1477
  /** @internal */
1554
- keepAlive: KeepAlive;
1478
+ constructor(isInitial: boolean, oldDocument?: Document);
1479
+ }
1480
+
1481
+ /**
1482
+ * The closure that is called whenever the documents covered by a live query
1483
+ * change.
1484
+ */
1485
+ type QueryObservationHandler = (documents: Document[], event: LiveQueryEvent, signalNext?: () => void) => void | Promise<void>;
1486
+ /**
1487
+ * These objects are returned when using `find`-like functionality on
1488
+ * {@link Collection}.
1489
+ *
1490
+ * They allow chaining of further query-related functions to do things like add
1491
+ * a limit to the number of documents you want returned or specify how you want
1492
+ * the documents to be sorted and ordered.
1493
+ *
1494
+ * You can either call {@link exec | exec()} on the object to get an array of
1495
+ * {@link Document | documents} as an immediate return value, or you can
1496
+ * establish either a live query or a subscription, which both work over time.
1497
+ *
1498
+ * A live query, established by calling
1499
+ * {@link PendingCursorOperation.observeLocal | observeLocal()}, will notify you
1500
+ * every time there's an update to a document that matches the query you
1501
+ * provided in the preceding `find`-like call.
1502
+ *
1503
+ * A subscription, established by calling
1504
+ * {@link PendingCursorOperation.subscribe | subscribe()}, will act as a signal
1505
+ * to other peers that the device connects to that you would like to receive
1506
+ * updates from them about documents that match the query you provided in the
1507
+ * preceding `find`-like call.
1508
+ *
1509
+ * Update and remove functionality is also exposed through this object.
1510
+ */
1511
+ declare class PendingCursorOperation extends BasePendingCursorOperation {
1512
+ sort(propertyPath: string, direction?: SortDirection): PendingCursorOperation;
1513
+ offset(offset: number): PendingCursorOperation;
1514
+ limit(limit: number): PendingCursorOperation;
1515
+ remove(): Promise<DocumentID[]>;
1516
+ evict(): Promise<DocumentID[]>;
1517
+ update(closure: (documents: MutableDocument[]) => void): Promise<UpdateResultsMap>;
1555
1518
  /**
1556
- * The number of operations pending before the Ditto instance can be closed.
1519
+ * Enables you to subscribe to changes that occur in a collection remotely.
1557
1520
  *
1558
- * For testing purposes only.
1559
- * @internal */
1560
- get numPendingOperations(): number;
1561
- /**
1562
- * Makes sure that the closure is executed only if the Ditto instance hasn't
1563
- * been closed yet.
1521
+ * Having a subscription acts as a signal to other peers that you are
1522
+ * interested in receiving updates when local or remote changes are made to
1523
+ * documents that match the query generated by the chain of operations that
1524
+ * precedes the call to {@link subscribe | subscribe()}.
1564
1525
  *
1565
- * @param closure the synchronous closure to execute.
1566
- * @returns the result of the closure.
1567
- * @throws if the Ditto instance was closed before calling this method.
1568
- * @internal */
1569
- deferClose<T>(closure: () => T): T;
1526
+ * The returned {@link Subscription} object must be kept in scope for as long
1527
+ * as you want to keep receiving updates.
1528
+ *
1529
+ * @returns A {@link Subscription} object that must be kept in scope for as
1530
+ * long as you want to keep receiving updates for documents that match the
1531
+ * query specified in the preceding chain.
1532
+ */
1533
+ subscribe(): Subscription;
1570
1534
  /**
1571
- * Makes sure that the closure is executed to completion before the Ditto
1572
- * instance is closed.
1535
+ * Enables you to listen for changes that occur in a collection locally.
1573
1536
  *
1574
- * Any calls to {@link close | `Ditto.close()`} will wait until the closure
1575
- * has completed before closing the Ditto instance.
1537
+ * The `handler` block will be called when local changes are
1538
+ * made to documents that match the query generated by the chain of operations
1539
+ * that precedes the call to {@link PendingCursorOperation.observeLocal | observeLocal()}.
1540
+ * The returned {@link LiveQuery} object must be kept in scope for as long as
1541
+ * you want the provided `handler` to be called when an update occurs.
1576
1542
  *
1577
- * @param closure the asynchronous closure to execute.
1578
- * @returns the result of the closure.
1579
- * @throws if the Ditto instance was closed before calling this method.
1580
- * @internal */
1581
- deferCloseAsync<T>(closure: () => Promise<T>): Promise<T>;
1582
- private sync;
1583
- private _isClosed;
1584
- private deferCloseAllowed;
1585
- private isWebValid;
1586
- private isX509Valid;
1587
- private presenceManager;
1588
- private transportConditionsManager;
1589
- /** Set of pending operations that need to complete before the Ditto instance can be closed in a safe manner. */
1590
- private pendingOperations;
1591
- private authClientValidityChanged;
1592
- private validateIdentity;
1593
- private setSyncActive;
1594
- private makeDefaultTransportConfig;
1595
- }
1596
- /** @internal */
1597
- declare const checkAPIs: (_globalObject?: Window | typeof globalThis) => boolean;
1598
-
1599
- /**
1600
- * Represents an attachment and can be used to insert the associated attachment
1601
- * into a document at a specific key-path. You can't instantiate an attachment
1602
- * directly, please use the {@link Collection.newAttachment | newAttachment()}
1603
- * method of {@link Collection} instead.
1604
- */
1605
- declare class Attachment {
1606
- /** @internal */
1607
- readonly ditto: Ditto;
1608
- /** @internal */
1609
- readonly token: AttachmentToken;
1610
- /** The attachment's metadata. */
1611
- get metadata(): {
1612
- [key: string]: string;
1613
- };
1614
- /**
1615
- * Returns the attachment's data.
1543
+ * This won't subscribe to receive changes made remotely by others and so it
1544
+ * will only fire updates when a local change is made. If you want to receive
1545
+ * remotely performed updates as well, you'll have to create a subscription
1546
+ * via {@link PendingCursorOperation.subscribe | subscribe()} with the
1547
+ * relevant query. The returned {@link LiveQuery} object must be kept in scope
1548
+ * for as long as you want the provided `eventHandler` to be called when an
1549
+ * update occurs.
1550
+ *
1551
+ * @param handler A closure that will be called every time there is a
1552
+ * transaction committed to the store that involves modifications to documents
1553
+ * matching the query in the collection this method was called on.
1554
+ *
1555
+ * @return A {@link LiveQuery} object that must be kept in scope for as long
1556
+ * as you want to keep receiving updates.
1616
1557
  */
1617
- getData(): Promise<Uint8Array>;
1558
+ observeLocal(handler: QueryObservationHandler): LiveQuery;
1618
1559
  /**
1619
- * Copies the attachment to the specified file path. Node-only,
1620
- * throws in the browser.
1560
+ * Enables you to listen for changes that occur in a collection locally and
1561
+ * to signal when you are ready for the live query to deliver the next event.
1621
1562
  *
1622
- * @param path The path that the attachment should be copied to.
1563
+ * The `handler` block will be called when local changes are
1564
+ * made to documents that match the query generated by the chain of operations
1565
+ * that precedes the call to {@link PendingCursorOperation.observeLocalWithNextSignal | observeLocalWithNextSignal()}.
1566
+ * The returned {@link LiveQuery} object must be kept in scope for as long as
1567
+ * you want the provided `handler` to be called when an update occurs.
1568
+ *
1569
+ * This won't subscribe to receive changes made remotely by others and so it
1570
+ * will only fire updates when a local change is made. If you want to receive
1571
+ * remotely performed updates as well, you'll have to create a subscription
1572
+ * via {@link PendingCursorOperation.subscribe | subscribe()} with the
1573
+ * relevant query. The returned {@link LiveQuery} object must be kept in scope
1574
+ * for as long as you want the provided `eventHandler` to be called when an
1575
+ * update occurs.
1576
+ *
1577
+ * @param handler A closure that will be called every time there is a
1578
+ * transaction committed to the store that involves modifications to
1579
+ * documents matching the query in the collection that this method was called
1580
+ * on.
1581
+ *
1582
+ * @return A {@link LiveQuery} object that must be kept in scope for as long
1583
+ * as you want to keep receiving updates.
1623
1584
  */
1624
- copyToPath(path: string): Promise<void>;
1585
+ observeLocalWithNextSignal(handler: QueryObservationHandler): LiveQuery;
1625
1586
  /** @internal */
1626
- constructor(ditto: Ditto, token: AttachmentToken);
1587
+ constructor(query: string, queryArgs: QueryArguments | null, collection: Collection);
1588
+ /** @internal */
1589
+ _observe(handler: QueryObservationHandler, waitForNextSignal: boolean): LiveQuery;
1627
1590
  }
1628
1591
 
1629
1592
  /**
1630
- * The types of attachment fetch events that can be delivered to an attachment
1631
- * fetcher's `callback`.
1632
- */
1633
- type AttachmentFetchEventType = 'Completed' | 'Progress' | 'Deleted';
1634
- /**
1635
- * An attachment fetch event used when the attachment's download has completed.
1636
- */
1637
- type AttachmentFetchEventCompleted = {
1638
- type: 'Completed';
1639
- attachment: Attachment;
1640
- };
1641
- /**
1642
- * An attachment fetch event used when the attachment's download progressed but
1643
- * is not yet complete.
1644
- */
1645
- type AttachmentFetchEventProgress = {
1646
- type: 'Progress';
1647
- totalBytes: number | BigInt;
1648
- downloadedBytes: number | BigInt;
1649
- };
1650
- /**
1651
- * An attachment fetch event used when the attachment is deleted.
1652
- */
1653
- type AttachmentFetchEventDeleted = {
1654
- type: 'Deleted';
1655
- };
1656
- /**
1657
- * A representation of the events that can occur in relation to an attachment
1658
- * fetch.
1659
- *
1660
- * There are three different attachment fetch events: `Completed`, `Progress`,
1661
- * or `Deleted`.
1593
+ * The type that is returned when calling
1594
+ * {@link PendingCursorOperation.observeLocal | observeLocal()} on a
1595
+ * {@link PendingCursorOperation} object. It handles the logic for calling the
1596
+ * event handler that is provided to `observeLocal()` calls.
1662
1597
  *
1663
- * There will be at most one `Completed` or `Deleted` event per attachment
1664
- * fetch. There can be many `Progress` events delivered for each attachment
1665
- * fetch.
1598
+ * Ditto will prevent the process from exiting as long as there are active live
1599
+ * queries (not relevant when running in the browser).
1666
1600
  *
1667
- * Updates relating to an attachment fetch are delivered by registering an
1668
- * {@link AttachmentFetcher} through a call to
1669
- * {@link Collection.fetchAttachment | fetchAttachment()} on a
1670
- * {@link Collection} instance.
1671
- */
1672
- type AttachmentFetchEvent = AttachmentFetchEventCompleted | AttachmentFetchEventProgress | AttachmentFetchEventDeleted;
1673
-
1674
- /**
1675
- * These objects are returned by calls to
1676
- * {@link Collection.fetchAttachment | fetchAttachment()} on {@link Collection}
1677
- * and allow you to stop an in-flight attachment fetch.
1601
+ * `LiveQuery` objects must be kept in scope for as long as you wish to have
1602
+ * your event handler be called when there is an update to a document matching
1603
+ * the query you provide. When you no longer want to receive updates about
1604
+ * documents matching a query then you must call {@link stop | stop()}.
1678
1605
  */
1679
- declare class AttachmentFetcher implements PromiseLike<Attachment | null> {
1680
- /**
1681
- * Returns a promise for the attachment that you can `await`. The promise
1682
- * resolves to either an attachment or `null` if the attachment has been
1683
- * deleted meanwhile. The promise is rejected if an error occurs during
1684
- * the fetch. Note that the `AttachmentFetcher` itself implementes
1685
- * `PromiseLike`, so you can `await` it directly.
1686
- */
1687
- readonly attachment: Promise<Attachment | null>;
1606
+ declare class LiveQuery {
1607
+ /** The query that the live query is based on. */
1608
+ readonly query: string;
1609
+ /** The arguments belonging to {@link query}. */
1610
+ readonly queryArgs: QueryArguments | null;
1611
+ /** The name of the collection that the live query is based on. */
1612
+ get collectionName(): string;
1613
+ /** Returns true if the receiver has been stopped. */
1614
+ get isStopped(): boolean;
1688
1615
  /**
1689
- * Stops fetching the associated attachment and cleans up any associated
1690
- * resources.
1691
- *
1692
- * Note that you are not required to call `stop()` once your attachment fetch
1693
- * operation has finished. The method primarily exists to allow you to cancel
1694
- * an attachment fetch request while it is ongoing if you no longer wish for
1695
- * the attachment to be made available locally to the device.
1616
+ * Stop the live query from delivering updates.
1696
1617
  */
1697
1618
  stop(): void;
1698
1619
  /** @internal */
1699
- then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2>;
1620
+ orderBys: OrderBy[];
1621
+ /** @internal */
1622
+ queryArgsCBOR: Uint8Array | null;
1623
+ /** @internal */
1624
+ readonly limit: number;
1625
+ /** @internal */
1626
+ readonly offset: number;
1700
1627
  /** @internal */
1628
+ readonly collection: Collection;
1629
+ /** @internal */
1630
+ readonly handler: QueryObservationHandler;
1631
+ /** @internal */
1632
+ liveQueryManager: LiveQueryManager | null;
1633
+ /** @internal */
1634
+ readonly liveQueryID: number;
1635
+ /** @internal */
1636
+ constructor(query: string, queryArgs: QueryArguments | null, queryArgsCBOR: Uint8Array | null, orderBys: OrderBy[], limit: number, offset: number, collection: Collection, handler: QueryObservationHandler);
1637
+ /** @internal */
1638
+ signalNext(): Promise<void>;
1639
+ }
1640
+
1641
+ /** @internal */
1642
+ declare class LiveQueryManager {
1701
1643
  readonly ditto: Ditto;
1644
+ readonly keepAlive: KeepAlive;
1702
1645
  /** @internal */
1703
- readonly token: AttachmentToken;
1646
+ constructor(ditto: Ditto, keepAlive: KeepAlive);
1704
1647
  /** @internal */
1705
- readonly eventHandler: (attachmentFetchEvent: AttachmentFetchEvent) => void | null;
1648
+ startLiveQuery(liveQuery: LiveQuery): void;
1706
1649
  /** @internal */
1707
- constructor(ditto: Ditto, token: AttachmentToken, eventHandler?: (attachmentFetchEvent: AttachmentFetchEvent) => void);
1708
- private cancelTokenPromise;
1709
- private static finalizationRegistry;
1710
- private static stopWithContextInfo;
1650
+ stopLiveQuery(liveQuery: LiveQuery): void;
1651
+ /** @internal */
1652
+ close(): void;
1653
+ private liveQueriesByID;
1654
+ private finalizationRegistry;
1655
+ private stopLiveQueryWithID;
1656
+ private finalize;
1711
1657
  }
1712
1658
 
1659
+ /** Types of connections that can be established between two peers. */
1660
+ type TransportCondition = 'Unknown' | 'OK' | 'GenericFailure' | 'AppInBackground' | 'MDNSFailure' | 'TCPListenFailure' | 'NoBLECentralPermission' | 'NoBLEPeripheralPermission' | 'CannotEstablishConnection' | 'BLEDisabled' | 'NoBLEHardware' | 'WiFiDisabled' | 'TemporarilyUnavailable';
1661
+ /** The source for a transport condition. */
1662
+ type ConditionSource = 'BLE' | 'TCP' | 'AWDL' | 'MDNS';
1713
1663
  /**
1714
- * Used to subscribe to receive updates from remote peers about matching
1715
- * documents.
1716
- *
1717
- * While {@link Subscription} objects remain in scope they ensure that
1718
- * documents in the collection specified and that match the query provided will
1719
- * try to be kept up-to-date with the latest changes from remote peers.
1664
+ * Types of connections that can be established between two peers.
1665
+ * @deprecated replaced by {@link ConnectionType}.
1720
1666
  */
1721
- declare class Subscription {
1667
+ type PresenceConnectionType = 'WiFi' | 'WebSocket' | 'AWDL' | 'BLE';
1668
+ /**
1669
+ * A peer object with information about an observed peer.
1670
+ * @deprecated replaced by {@link Peer}.
1671
+ */
1672
+ type RemotePeer = {
1673
+ networkID: string;
1674
+ deviceName: string;
1675
+ rssi?: number;
1676
+ approximateDistanceInMeters?: number;
1677
+ connections: PresenceConnectionType[];
1678
+ };
1679
+ /**
1680
+ * Ditto is the entry point for accessing Ditto-related functionality.
1681
+ */
1682
+ declare class Ditto {
1722
1683
  /**
1723
- * The query that the subscription is based on.
1684
+ * Configure a custom identifier for the current device.
1685
+ *
1686
+ * When using {@link Presence.observe | presence.observe()}, each remote peer is
1687
+ * represented by a short UTF-8 "device name". By default this will be a
1688
+ * truncated version of the device's hostname. It does not need to be unique
1689
+ * among peers. Configure the device name before calling
1690
+ * {@link startSync | startSync()}. If it is too long it may be truncated.
1724
1691
  */
1725
- readonly query: string;
1692
+ deviceName: string;
1693
+ /** Returns a string identifying the version of the Ditto SDK. */
1694
+ get sdkVersion(): string;
1726
1695
  /**
1727
- * Returns `true` if subscription has been explicitly cancelled, `false`
1728
- * otherwise.
1696
+ * The (validated) identity this Ditto instance was initialized with.
1729
1697
  */
1730
- readonly isCancelled = false;
1698
+ readonly identity: Identity;
1731
1699
  /**
1732
- * The name of the collection that the subscription is based on.
1700
+ * The path this Ditto instance was initialized with, if no path was given at
1701
+ * construction time, the default value is returned (see constructor).
1733
1702
  */
1734
- get collectionName(): string;
1703
+ readonly path: string;
1704
+ /**
1705
+ * Provides access to the SDK's store functionality.
1706
+ */
1707
+ readonly store: Store;
1708
+ /**
1709
+ * Provides access to the SDK's presence functionality.
1710
+ */
1711
+ readonly presence: Presence;
1712
+ /**
1713
+ * Provides access to authentication methods for logging on to Ditto Cloud.
1714
+ */
1715
+ readonly auth: Authenticator;
1735
1716
  /**
1736
- * Cancels a subscription and releases all associated resources.
1717
+ * The site ID that the instance of `Ditto` is using as part of its identity.
1737
1718
  */
1738
- cancel(): void;
1739
- /** @internal */
1740
- constructor(collection: Collection, query: string, queryArgsCBOR: Uint8Array | null, orderBys: OrderBy[], limit: number, offset: number);
1719
+ readonly siteID: number | BigInt;
1741
1720
  /**
1742
- * The collection this subscription belongs to.
1743
- * @internal Because not exposed in any of the other SDKs (yet?).
1721
+ * Returns `true` if an offline license token has been set, otherwise returns `false`.
1722
+ *
1723
+ * @see {@link setOfflineOnlyLicenseToken | setOfflineOnlyLicenseToken()}
1744
1724
  */
1745
- readonly collection: Collection;
1725
+ readonly isActivated: boolean;
1746
1726
  /**
1747
- * The corresponding named arguments for {@link query}, if any.
1748
- * @internal Because not exposed in any of the other SDKs (yet?).
1727
+ * `true` once {@link close | Ditto.close()} has been called, otherwise
1728
+ * `false`.
1749
1729
  */
1750
- readonly queryArgsCBOR: Uint8Array | null;
1751
- private static finalizationRegistry;
1752
- private static add;
1753
- private static remove;
1754
- private contextInfo;
1755
- }
1756
-
1757
- /**
1758
- * The type that is returned when calling
1759
- * {@link PendingCursorOperation.observeLocal | observeLocal()} on a
1760
- * {@link PendingCursorOperation} object. It handles the logic for calling the
1761
- * event handler that is provided to `observeLocal()` calls.
1762
- *
1763
- * Ditto will prevent the process from exiting as long as there are active live
1764
- * queries (not relevant when running in the browser).
1765
- *
1766
- * `LiveQuery` objects must be kept in scope for as long as you wish to have
1767
- * your event handler be called when there is an update to a document matching
1768
- * the query you provide. When you no longer want to receive updates about
1769
- * documents matching a query then you must call {@link stop | stop()}.
1770
- */
1771
- declare class LiveQuery {
1772
- /** The query that the live query is based on. */
1773
- readonly query: string;
1774
- /** The arguments belonging to {@link query}. */
1775
- readonly queryArgs: QueryArguments | null;
1776
- /** The name of the collection that the live query is based on. */
1777
- get collectionName(): string;
1778
- /** Returns true if the receiver has been stopped. */
1779
- get isStopped(): boolean;
1730
+ readonly isClosed: boolean;
1780
1731
  /**
1781
- * Stop the live query from delivering updates.
1732
+ * Returns `true` if sync is active, otherwise returns `false`. Use
1733
+ * {@link startSync | startSync()} to activate and {@link stopSync | stopSync()}
1734
+ * to deactivate sync.
1782
1735
  */
1783
- stop(): void;
1784
- /** @internal */
1785
- readonly limit: number;
1786
- /** @internal */
1787
- readonly offset: number;
1788
- /** @internal */
1789
- readonly collection: Collection;
1790
- /** @internal */
1791
- readonly subscription?: Subscription;
1792
- /** @internal */
1793
- readonly handler: QueryObservationHandler;
1794
- /** @internal */
1795
- constructor(query: string, queryArgs: QueryArguments | null, queryArgsCBOR: Uint8Array | null, orderBys: OrderBy[], limit: number, offset: number, collection: Collection, subscription: Subscription | null, handler: QueryObservationHandler);
1796
- /** @internal */
1797
- signalNext(): Promise<void>;
1798
- private orderBys;
1799
- private queryArgsCBOR;
1800
- private liveQueryID;
1801
- }
1802
-
1803
- /**
1804
- * An object that describes how a document's position in a live query's list of
1805
- * matching documents has changed since the previous live query event.
1806
- */
1807
- interface LiveQueryMove {
1736
+ readonly isSyncActive: boolean;
1808
1737
  /**
1809
- * The index of the document in the list of matching documents from the
1810
- * previous live query event.
1738
+ * Initializes a new `Ditto` instance.
1739
+ *
1740
+ * **NOTE**: The `sharedKey` identity is only supported for Node environments,
1741
+ * using this to create a Ditto instance in the web browser will throw an
1742
+ * exception.
1743
+ *
1744
+ * @param identity - Identity for the new Ditto instance, defaults to
1745
+ * `offlinePlayground` with `appID` being the empty string `''`.
1746
+ *
1747
+ * @param path - On Node, `path` corresponds to a real directory
1748
+ * on the file system (intermediate directories are created if needed). In the
1749
+ * browser, `path` is used as an internal namespace for the in-memory storage.
1750
+ * Defaults to `"ditto"`.
1751
+ *
1752
+ * @see {@link Ditto.identity}
1753
+ * @see {@link Ditto.path}
1811
1754
  */
1812
- readonly from: number;
1755
+ constructor(identity?: Identity, path?: string);
1813
1756
  /**
1814
- * The index of the document in the list of matching documents from the new
1815
- * live query event.
1757
+ * Check if the current environment supports running Ditto.
1758
+ *
1759
+ * Required APIs include:
1760
+ *
1761
+ * - `BigInt`
1762
+ * - `FinalizationRegistry`
1763
+ * - `WeakRef`
1764
+ *
1765
+ * Internet Explorer is not supported.
1766
+ *
1767
+ * @returns true if the environment is supported
1816
1768
  */
1817
- readonly to: number;
1818
- }
1819
- /** @internal */
1820
- interface LiveQueryEventUpdateParams {
1821
- oldDocuments: Document[];
1822
- insertions: number[];
1823
- deletions: number[];
1824
- updates: number[];
1825
- moves: LiveQueryMove[];
1826
- }
1827
- /**
1828
- * First event fired immediately after registering a live query without any
1829
- * mutations. All subsequent events are of type {@link LiveQueryEventUpdate}.
1830
- */
1831
- declare class LiveQueryEventInitial {
1769
+ static isEnvironmentSupported(): boolean;
1832
1770
  /**
1833
- * Whether or not this is the initial event being delivered. Always `true`
1834
- * for `LiveQueryEventInitial`.
1771
+ * Activate a `Ditto` instance by setting an offline only license token. You cannot initiate sync
1772
+ * with `Ditto` before you have activated it. The offline license token is only valid for identities
1773
+ * of type `development`, `manual`, `offlinePlayground`, and `sharedKey`.
1774
+ *
1775
+ * @param licenseToken the license token to activate the `Ditto` instance
1776
+ * with. You can find yours on the [Ditto portal](https://portal.ditto.live).
1835
1777
  */
1836
- readonly isInitial = true;
1778
+ setOfflineOnlyLicenseToken(licenseToken: string): void;
1837
1779
  /**
1838
- * Returns a hash that represents the set of matching documents.
1780
+ * Returns the current transport configuration, frozen. If you want to modify
1781
+ * the transport config, make a {@link TransportConfig.copy | copy} first. Or
1782
+ * use the {@link updateTransportConfig | updateTransportConfig()}
1783
+ * convenience method. By default peer-to-peer transports (Bluetooth, WiFi,
1784
+ * and AWDL) are enabled if available in the current environment
1785
+ * (Web, Node, OS, etc.).
1786
+ *
1787
+ * @see {@link setTransportConfig | setTransportConfig()}
1788
+ * @see {@link updateTransportConfig | updateTransportConfig()}
1839
1789
  */
1840
- hash(documents: Document[]): BigInt;
1790
+ readonly transportConfig: TransportConfig;
1841
1791
  /**
1842
- * Returns a pattern of words that together create a mnemonic, which
1843
- * represents the set of matching documents.
1792
+ * Assigns a new transports configuration. By default peer-to-peer transports
1793
+ * (Bluetooth, WiFi, and AWDL) are enabled. You may use this method to alter
1794
+ * the configuration at any time, however sync will not begin until
1795
+ * {@link startSync | startSync()} is called.
1796
+ *
1797
+ * @see {@link transportConfig}
1798
+ * @see {@link updateTransportConfig | updateTransportConfig()}
1844
1799
  */
1845
- hashMnemonic(documents: Document[]): string;
1846
- }
1847
- /**
1848
- * Represents an update event describing all changes that occured for documents
1849
- * covered by a (live) query.
1850
- */
1851
- declare class LiveQueryEventUpdate {
1800
+ setTransportConfig(transportConfig: TransportConfig): void;
1852
1801
  /**
1853
- * Whether or not this is the initial event being delivered. Always `false`
1854
- * for `LiveQueryEventUpdate`.
1802
+ * Convenience method for updating the transport config. Creates a copy of the
1803
+ * current transport config, passes that copy to the `update` closure,
1804
+ * allowing it to mutate as needed, and sets that updated copy afterwards.
1855
1805
  */
1856
- readonly isInitial = false;
1806
+ updateTransportConfig(update: (transportConfig: TransportConfig) => void): Ditto;
1857
1807
  /**
1858
- * The documents that previously matched the query, before the latest event.
1808
+ * Starts the network transports. Ditto will connect to other devices.
1809
+ *
1810
+ * By default Ditto will enable all peer-to-peer transport types. On **Node**,
1811
+ * this means BluetoothLE, WiFi/LAN, and AWDL. On the **Web**, only connecting
1812
+ * via Websockets is supported. The network configuration can be
1813
+ * customized with {@link updateTransportConfig | updateTransportConfig()}
1814
+ * or replaced entirely with {@link setTransportConfig | setTransportConfig()}.
1815
+ *
1816
+ *
1817
+ * Ditto will prevent the process from exiting until sync is stopped (not
1818
+ * relevant when running in the browser).
1819
+ *
1820
+ * **NOTE**: the BluetoothLE transport on Linux is experimental, this
1821
+ * method panics if no BluetoothLE hardware is available. Therefore, contrary
1822
+ * to the above, the BluetoothLE transport is temporarily disabled by default
1823
+ * on Linux.
1824
+ *
1825
+ * @see {@link isSyncActive}
1826
+ * @see {@link stopSync | stopSync()}
1859
1827
  */
1860
- readonly oldDocuments: Document[];
1828
+ startSync(): void;
1861
1829
  /**
1862
- * The indexes in the array of matching documents that accompany this event,
1863
- * which relate to a document that was not in the previous most recent list of
1864
- * matching documents.
1830
+ * Stops all network transports.
1831
+ *
1832
+ * You may continue to use the database locally but no data will sync to or
1833
+ * from other devices.
1834
+ *
1835
+ * @see {@link isSyncActive}
1836
+ * @see {@link startSync | startSync()}
1865
1837
  */
1866
- readonly insertions: number[];
1838
+ stopSync(): void;
1867
1839
  /**
1868
- * The indexes into the array {@link oldDocuments}, which relate to a document
1869
- * that was in the previous most recent list of matching documents but is no
1870
- * longer a matching document.
1840
+ * Registers an observer for info about Ditto peers in range of this device.
1841
+ *
1842
+ * Ditto will prevent the process from exiting as long as there are active
1843
+ * peers observers (not relevant when running in the browser).
1844
+ *
1845
+ * @param callback called immediately with the current state of peers
1846
+ * in range and whenever that state changes. Then it will be invoked
1847
+ * repeatedly when Ditto devices come and go, or the active connections to
1848
+ * them change.
1849
+ *
1850
+ * @deprecated please use {@link Presence.observe | presence.observe()} instead.
1871
1851
  */
1872
- readonly deletions: number[];
1852
+ observePeers(callback: (peersData: RemotePeer[]) => void): Observer;
1873
1853
  /**
1874
- * The indexes in the array of matching documents that accompany this event,
1875
- * which relate to a document that has been updated since the previous live
1876
- * query event.
1854
+ * Register observer for changes of underlying transport conditions.
1855
+ *
1856
+ * Ditto will prevent the process from exiting as long as there are active
1857
+ * transport conditions observers (not relevant when running in the browser).
1858
+ *
1859
+ * @param callback called when underlying transport conditions change with
1860
+ * the changed `condition` and it's `source`.
1877
1861
  */
1878
- readonly updates: number[];
1862
+ observeTransportConditions(callback: (condition: TransportCondition, source: ConditionSource) => void): Observer;
1879
1863
  /**
1880
- * Objects that describe how documents' positions in the list of matching
1881
- * documents have changed since the previous live query event.
1864
+ * Removes all sync metadata for any remote peers which aren't currently
1865
+ * connected. This method shouldn't usually be called. Manually running
1866
+ * garbage collection often will result in slower sync times. Ditto
1867
+ * automatically runs a garbage a collection process in the background at
1868
+ * optimal times.
1869
+ *
1870
+ * Manually running garbage collection is typically only useful during testing
1871
+ * if large amounts of data are being generated. Alternatively, if an entire
1872
+ * data set is to be evicted and it's clear that maintaining this metadata
1873
+ * isn't necessary, then garbage collection could be run after evicting the
1874
+ * old data.
1875
+ *
1876
+ * Only available in Node environments at the moment, no-op in the browser.
1882
1877
  */
1883
- readonly moves: LiveQueryMove[];
1878
+ runGarbageCollection(): void;
1884
1879
  /**
1885
- * Returns a hash that represents the set of matching documents.
1880
+ * Explicitly opt-in to disabling the ability to sync with Ditto peers running
1881
+ * any version of the SDK in the v3 (or lower) series of releases.
1882
+ *
1883
+ * Assuming this succeeds then this peer will only be able to sync with other
1884
+ * peers using SDKs in the v4 (or higher) series of releases. Note that this
1885
+ * disabling of sync spreads to peers that sync with a peer that has disabled,
1886
+ * or has (transitively) had disabled, syncing with v3 SDK peers.
1886
1887
  */
1887
- hash(documents: Document[]): BigInt;
1888
+ disableSyncWithV3(): void;
1888
1889
  /**
1889
- * Returns a pattern of words that together create a mnemonic, which
1890
- * represents the set of matching documents.
1890
+ * Shut down Ditto and release all resources.
1891
+ *
1892
+ * Must be called before recreating a Ditto instance that uses the same
1893
+ * persistence directory.
1891
1894
  */
1892
- hashMnemonic(documents: Document[]): string;
1895
+ close(): Promise<void>;
1893
1896
  /** @internal */
1894
- constructor(params: LiveQueryEventUpdateParams);
1897
+ keepAlive: KeepAlive;
1898
+ /** @internal */
1899
+ liveQueryManager: LiveQueryManager;
1900
+ /**
1901
+ * The number of operations pending before the Ditto instance can be closed.
1902
+ *
1903
+ * For testing purposes only.
1904
+ * @internal */
1905
+ get numPendingOperations(): number;
1906
+ /**
1907
+ * Makes sure that the closure is executed only if the Ditto instance hasn't
1908
+ * been closed yet.
1909
+ *
1910
+ * @param closure the synchronous closure to execute.
1911
+ * @returns the result of the closure.
1912
+ * @throws if the Ditto instance was closed before calling this method.
1913
+ * @internal */
1914
+ deferClose<T>(closure: () => T): T;
1915
+ /**
1916
+ * Makes sure that the closure is executed to completion before the Ditto
1917
+ * instance is closed.
1918
+ *
1919
+ * Any calls to {@link close | `Ditto.close()`} will wait until the closure
1920
+ * has completed before closing the Ditto instance.
1921
+ *
1922
+ * @param closure the asynchronous closure to execute.
1923
+ * @returns the result of the closure.
1924
+ * @throws if the Ditto instance was closed before calling this method.
1925
+ * @internal */
1926
+ deferCloseAsync<T>(closure: () => Promise<T>): Promise<T>;
1927
+ private sync;
1928
+ private deferCloseAllowed;
1929
+ private isWebValid;
1930
+ private isX509Valid;
1931
+ private presenceManager;
1932
+ private transportConditionsManager;
1933
+ /** Set of pending operations that need to complete before the Ditto instance can be closed in a safe manner. */
1934
+ private pendingOperations;
1935
+ private authClientValidityChanged;
1936
+ private validateIdentity;
1937
+ private setSyncActive;
1938
+ private makeDefaultTransportConfig;
1895
1939
  }
1940
+ /** @internal */
1941
+ declare const checkAPIs: (_globalObject?: Window | typeof globalThis) => boolean;
1942
+
1896
1943
  /**
1897
- * Represents events delivered by a {@link LiveQuery}, which can be initial
1898
- * (fired immediately upon registration) or an update (all subsequent events).
1899
- */
1900
- type LiveQueryEvent = LiveQueryEventInitial | LiveQueryEventUpdate;
1901
- /**
1902
- * Provides information about a live query event relating to a single document
1903
- * live query.
1944
+ * Represents an attachment and can be used to insert the associated attachment
1945
+ * into a document at a specific key-path. You can't instantiate an attachment
1946
+ * directly, please use the {@link Collection.newAttachment | newAttachment()}
1947
+ * method of {@link Collection} instead.
1904
1948
  */
1905
- declare class SingleDocumentLiveQueryEvent {
1906
- /**
1907
- * Whether or not this is the initial event being delivered.
1908
- */
1909
- readonly isInitial: boolean;
1910
- /** The old representation of the document with the relveant document ID. */
1911
- readonly oldDocument?: Document;
1949
+ declare class Attachment {
1950
+ /** @internal */
1951
+ readonly ditto: Ditto;
1952
+ /** @internal */
1953
+ readonly token: AttachmentToken;
1954
+ /** The attachment's metadata. */
1955
+ get metadata(): {
1956
+ [key: string]: string;
1957
+ };
1912
1958
  /**
1913
- * Returns a hash that represents the set of matching documents.
1959
+ * Returns the attachment's data.
1914
1960
  */
1915
- hash(document: Document | null): BigInt;
1961
+ getData(): Promise<Uint8Array>;
1916
1962
  /**
1917
- * Returns a pattern of words that together create a mnemonic, which
1918
- * represents the set of matching documents.
1963
+ * Copies the attachment to the specified file path. Node-only,
1964
+ * throws in the browser.
1965
+ *
1966
+ * @param path The path that the attachment should be copied to.
1919
1967
  */
1920
- hashMnemonic(document: Document | null): string;
1968
+ copyToPath(path: string): Promise<void>;
1921
1969
  /** @internal */
1922
- constructor(isInitial: boolean, oldDocument?: Document);
1970
+ constructor(ditto: Ditto, token: AttachmentToken);
1923
1971
  }
1924
1972
 
1925
1973
  /**
1926
- * The closure that is called whenever the documents covered by a live query
1927
- * change.
1974
+ * The types of attachment fetch events that can be delivered to an attachment
1975
+ * fetcher's `callback`.
1928
1976
  */
1929
- type QueryObservationHandler = (documents: Document[], event: LiveQueryEvent, signalNext?: () => void) => void | Promise<void>;
1977
+ type AttachmentFetchEventType = 'Completed' | 'Progress' | 'Deleted';
1930
1978
  /**
1931
- * These objects are returned when using `find`-like functionality on
1932
- * {@link Collection}.
1933
- *
1934
- * They allow chaining of further query-related functions to do things like add
1935
- * a limit to the number of documents you want returned or specify how you want
1936
- * the documents to be sorted and ordered.
1937
- *
1938
- * You can either call {@link exec | exec()} on the object to get an array of
1939
- * {@link Document | documents} as an immediate return value, or you can
1940
- * establish either a live query or a subscription, which both work over time.
1979
+ * An attachment fetch event used when the attachment's download has completed.
1980
+ */
1981
+ type AttachmentFetchEventCompleted = {
1982
+ type: 'Completed';
1983
+ attachment: Attachment;
1984
+ };
1985
+ /**
1986
+ * An attachment fetch event used when the attachment's download progressed but
1987
+ * is not yet complete.
1988
+ */
1989
+ type AttachmentFetchEventProgress = {
1990
+ type: 'Progress';
1991
+ totalBytes: number | BigInt;
1992
+ downloadedBytes: number | BigInt;
1993
+ };
1994
+ /**
1995
+ * An attachment fetch event used when the attachment is deleted.
1996
+ */
1997
+ type AttachmentFetchEventDeleted = {
1998
+ type: 'Deleted';
1999
+ };
2000
+ /**
2001
+ * A representation of the events that can occur in relation to an attachment
2002
+ * fetch.
1941
2003
  *
1942
- * A live query, established by calling
1943
- * {@link PendingCursorOperation.observeLocal | observeLocal()}, will notify you
1944
- * every time there's an update to a document that matches the query you
1945
- * provided in the preceding `find`-like call.
2004
+ * There are three different attachment fetch events: `Completed`, `Progress`,
2005
+ * or `Deleted`.
1946
2006
  *
1947
- * A subscription, established by calling
1948
- * {@link PendingCursorOperation.subscribe | subscribe()}, will act as a signal
1949
- * to other peers that the device connects to that you would like to receive
1950
- * updates from them about documents that match the query you provided in the
1951
- * preceding `find`-like call.
2007
+ * There will be at most one `Completed` or `Deleted` event per attachment
2008
+ * fetch. There can be many `Progress` events delivered for each attachment
2009
+ * fetch.
1952
2010
  *
1953
- * Update and remove functionality is also exposed through this object.
2011
+ * Updates relating to an attachment fetch are delivered by registering an
2012
+ * {@link AttachmentFetcher} through a call to
2013
+ * {@link Collection.fetchAttachment | fetchAttachment()} on a
2014
+ * {@link Collection} instance.
1954
2015
  */
1955
- declare class PendingCursorOperation extends BasePendingCursorOperation {
1956
- sort(propertyPath: string, direction?: SortDirection): PendingCursorOperation;
1957
- offset(offset: number): PendingCursorOperation;
1958
- limit(limit: number): PendingCursorOperation;
1959
- remove(): Promise<DocumentID[]>;
1960
- evict(): Promise<DocumentID[]>;
1961
- update(closure: (documents: MutableDocument[]) => void): Promise<UpdateResultsMap>;
1962
- /**
1963
- * Enables you to subscribe to changes that occur in a collection remotely.
1964
- *
1965
- * Having a subscription acts as a signal to other peers that you are
1966
- * interested in receiving updates when local or remote changes are made to
1967
- * documents that match the query generated by the chain of operations that
1968
- * precedes the call to {@link subscribe | subscribe()}.
1969
- *
1970
- * The returned {@link Subscription} object must be kept in scope for as long
1971
- * as you want to keep receiving updates.
1972
- *
1973
- * @returns A {@link Subscription} object that must be kept in scope for as
1974
- * long as you want to keep receiving updates for documents that match the
1975
- * query specified in the preceding chain.
1976
- */
1977
- subscribe(): Subscription;
2016
+ type AttachmentFetchEvent = AttachmentFetchEventCompleted | AttachmentFetchEventProgress | AttachmentFetchEventDeleted;
2017
+
2018
+ /**
2019
+ * These objects are returned by calls to
2020
+ * {@link Collection.fetchAttachment | fetchAttachment()} on {@link Collection}
2021
+ * and allow you to stop an in-flight attachment fetch.
2022
+ */
2023
+ declare class AttachmentFetcher implements PromiseLike<Attachment | null> {
1978
2024
  /**
1979
- * Enables you to listen for changes that occur in a collection locally.
1980
- *
1981
- * The `handler` block will be called when local changes are
1982
- * made to documents that match the query generated by the chain of operations
1983
- * that precedes the call to {@link PendingCursorOperation.observeLocal | observeLocal()}.
1984
- * The returned {@link LiveQuery} object must be kept in scope for as long as
1985
- * you want the provided `handler` to be called when an update occurs.
1986
- *
1987
- * This won't subscribe to receive changes made remotely by others and so it
1988
- * will only fire updates when a local change is made. If you want to receive
1989
- * remotely performed updates as well, you'll have to create a subscription
1990
- * via {@link PendingCursorOperation.subscribe | subscribe()} with the
1991
- * relevant query. The returned {@link LiveQuery} object must be kept in scope
1992
- * for as long as you want the provided `eventHandler` to be called when an
1993
- * update occurs.
1994
- *
1995
- * @param handler A closure that will be called every time there is a
1996
- * transaction committed to the store that involves modifications to documents
1997
- * matching the query in the collection this method was called on.
1998
- *
1999
- * @return A {@link LiveQuery} object that must be kept in scope for as long
2000
- * as you want to keep receiving updates.
2025
+ * Returns a promise for the attachment that you can `await`. The promise
2026
+ * resolves to either an attachment or `null` if the attachment has been
2027
+ * deleted meanwhile. The promise is rejected if an error occurs during
2028
+ * the fetch. Note that the `AttachmentFetcher` itself implementes
2029
+ * `PromiseLike`, so you can `await` it directly.
2001
2030
  */
2002
- observeLocal(handler: QueryObservationHandler): LiveQuery;
2031
+ readonly attachment: Promise<Attachment | null>;
2003
2032
  /**
2004
- * Enables you to listen for changes that occur in a collection locally and
2005
- * to signal when you are ready for the live query to deliver the next event.
2006
- *
2007
- * The `handler` block will be called when local changes are
2008
- * made to documents that match the query generated by the chain of operations
2009
- * that precedes the call to {@link PendingCursorOperation.observeLocalWithNextSignal | observeLocalWithNextSignal()}.
2010
- * The returned {@link LiveQuery} object must be kept in scope for as long as
2011
- * you want the provided `handler` to be called when an update occurs.
2012
- *
2013
- * This won't subscribe to receive changes made remotely by others and so it
2014
- * will only fire updates when a local change is made. If you want to receive
2015
- * remotely performed updates as well, you'll have to create a subscription
2016
- * via {@link PendingCursorOperation.subscribe | subscribe()} with the
2017
- * relevant query. The returned {@link LiveQuery} object must be kept in scope
2018
- * for as long as you want the provided `eventHandler` to be called when an
2019
- * update occurs.
2020
- *
2021
- * @param handler A closure that will be called every time there is a
2022
- * transaction committed to the store that involves modifications to
2023
- * documents matching the query in the collection that this method was called
2024
- * on.
2033
+ * Stops fetching the associated attachment and cleans up any associated
2034
+ * resources.
2025
2035
  *
2026
- * @return A {@link LiveQuery} object that must be kept in scope for as long
2027
- * as you want to keep receiving updates.
2036
+ * Note that you are not required to call `stop()` once your attachment fetch
2037
+ * operation has finished. The method primarily exists to allow you to cancel
2038
+ * an attachment fetch request while it is ongoing if you no longer wish for
2039
+ * the attachment to be made available locally to the device.
2028
2040
  */
2029
- observeLocalWithNextSignal(handler: QueryObservationHandler): LiveQuery;
2041
+ stop(): void;
2030
2042
  /** @internal */
2031
- constructor(query: string, queryArgs: QueryArguments | null, collection: Collection);
2043
+ then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2>;
2044
+ /** @internal */
2045
+ readonly ditto: Ditto;
2032
2046
  /** @internal */
2033
- _observe(handler: QueryObservationHandler, createSubscription: boolean, waitForNextSignal: boolean): LiveQuery;
2047
+ readonly token: AttachmentToken;
2048
+ /** @internal */
2049
+ readonly eventHandler: (attachmentFetchEvent: AttachmentFetchEvent) => void | null;
2050
+ /** @internal */
2051
+ constructor(ditto: Ditto, token: AttachmentToken, eventHandler?: (attachmentFetchEvent: AttachmentFetchEvent) => void);
2052
+ private cancelTokenPromise;
2053
+ private static finalizationRegistry;
2054
+ private static stopWithContextInfo;
2034
2055
  }
2035
2056
 
2036
2057
  /**
@@ -2185,7 +2206,7 @@ declare class PendingIDSpecificOperation extends BasePendingIDSpecificOperation
2185
2206
  /** @internal */
2186
2207
  constructor(documentID: DocumentID, collection: Collection);
2187
2208
  /** @internal */
2188
- _observe(handler: SingleObservationHandler, createSubscription: boolean, waitForNextSignal: boolean): LiveQuery;
2209
+ _observe(handler: SingleObservationHandler, waitForNextSignal: boolean): LiveQuery;
2189
2210
  }
2190
2211
 
2191
2212
  /**
@@ -2488,7 +2509,7 @@ declare class PendingCollectionsOperation implements PromiseLike<Collection[]> {
2488
2509
  /** @internal */
2489
2510
  then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2>;
2490
2511
  /** @internal */
2491
- _observe(handler: CollectionsObservationHandler, createSubscription: boolean, waitForNextSignal: boolean): LiveQuery;
2512
+ _observe(handler: CollectionsObservationHandler, waitForNextSignal: boolean): LiveQuery;
2492
2513
  private readonly pendingCursorOperation;
2493
2514
  }
2494
2515