@dittolive/ditto 4.0.3-alpha.linux-ble-fixes → 4.1.0

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
@@ -18,8 +18,62 @@ declare class KeepAlive {
18
18
  }
19
19
 
20
20
  /** @internal */
21
- interface ObserverRemoving {
22
- removeObserver(token: any): any;
21
+ type ObserverToken = string;
22
+ /** @internal */
23
+ type ObserverManagerConstructorOptions = {
24
+ keepAlive?: KeepAlive;
25
+ register?: (callback: (...args: any[]) => void) => void;
26
+ unregister?: () => void;
27
+ process?: (...args: any[]) => any[];
28
+ };
29
+ /** @internal */
30
+ declare class ObserverManager {
31
+ /** @internal */
32
+ readonly id: string;
33
+ /** @internal */
34
+ readonly keepAlive: KeepAlive | null;
35
+ /** @internal */
36
+ constructor(id: string, options?: ObserverManagerConstructorOptions);
37
+ /** @internal */
38
+ addObserver(callback: any): ObserverToken;
39
+ /** @internal */
40
+ removeObserver(token: ObserverToken): void;
41
+ hasObserver(token: ObserverToken): boolean;
42
+ /** @internal */
43
+ notify(...args: any[]): void;
44
+ /** @internal */
45
+ close(): void;
46
+ /**
47
+ * Can be injected and replaced via constructor options.
48
+ *
49
+ * @abstract
50
+ */
51
+ protected register(callback: (...args: any[]) => void): void;
52
+ /**
53
+ * Can be injected and replaced via constructor options.
54
+ *
55
+ * @abstract
56
+ */
57
+ protected unregister(): void;
58
+ /**
59
+ * Can be injected and replaced via constructor options.
60
+ *
61
+ * @abstract
62
+ */
63
+ protected process(...args: any[]): any[];
64
+ private isClosed;
65
+ private isRegistered;
66
+ private callbacksByToken;
67
+ private constructorOptions;
68
+ private hasObservers;
69
+ private registerIfNeeded;
70
+ private unregisterIfNeeded;
71
+ }
72
+
73
+ /** @internal */
74
+ interface ObserverManaging {
75
+ hasObserver(token: ObserverToken): any;
76
+ removeObserver(token: ObserverToken): any;
23
77
  }
24
78
  /** @internal */
25
79
  type ObserverOptions = {
@@ -33,13 +87,13 @@ type ObserverOptions = {
33
87
  */
34
88
  declare class Observer {
35
89
  /** @internal */
36
- readonly observerManager: ObserverRemoving;
90
+ readonly observerManager: ObserverManaging;
37
91
  /** @internal */
38
- readonly token?: any;
92
+ readonly token?: ObserverToken;
39
93
  /** @internal */
40
94
  readonly options?: ObserverOptions;
41
95
  /** @internal */
42
- constructor(observerManager: ObserverRemoving, token: any, options?: ObserverOptions);
96
+ constructor(observerManager: ObserverManaging, token: any, options?: ObserverOptions);
43
97
  /**
44
98
  * Returns `true` if the observer has been explicitly stopped via the `stop()`
45
99
  * method. Otherwise returns `false`.
@@ -203,8 +257,6 @@ type Pointer<Type> = {
203
257
  /** @internal */
204
258
  type FFIWriteTransaction = 'CWriteTransaction_t';
205
259
  /** @internal */
206
- type FFIAuthClient = 'CAuthClient_t';
207
- /** @internal */
208
260
  type OrderBy = {
209
261
  query: string;
210
262
  direction: 'Ascending' | 'Descending';
@@ -869,56 +921,6 @@ declare class TransportConfig {
869
921
  static areListenHTTPsEqual(left: TransportConfigListenHTTP, right: TransportConfigListenHTTP): boolean;
870
922
  }
871
923
 
872
- /** @internal */
873
- type ObserverToken = string;
874
- /** @internal */
875
- type ObserverManagerConstructorOptions = {
876
- keepAlive?: KeepAlive;
877
- register?: (callback: (...args: any[]) => void) => void;
878
- unregister?: () => void;
879
- process?: (...args: any[]) => any[];
880
- };
881
- /** @internal */
882
- declare class ObserverManager {
883
- /** @internal */
884
- readonly id: string;
885
- /** @internal */
886
- readonly keepAlive: KeepAlive | null;
887
- /** @internal */
888
- constructor(id: string, options?: ObserverManagerConstructorOptions);
889
- /** @internal */
890
- addObserver(callback: any): ObserverToken;
891
- /** @internal */
892
- removeObserver(token: ObserverToken): void;
893
- /** @internal */
894
- notify(...args: any[]): void;
895
- /**
896
- * Can be injected and replaced via constructor options.
897
- *
898
- * @abstract
899
- */
900
- protected register(callback: (...args: any[]) => void): void;
901
- /**
902
- * Can be injected and replaced via constructor options.
903
- *
904
- * @abstract
905
- */
906
- protected unregister(): void;
907
- /**
908
- * Can be injected and replaced via constructor options.
909
- *
910
- * @abstract
911
- */
912
- protected process(...args: any[]): any[];
913
- private isRegistered;
914
- private callbacksByToken;
915
- private constructorOptions;
916
- private hasObservers;
917
- private registerIfNeeded;
918
- private unregisterIfNeeded;
919
- private finalize;
920
- }
921
-
922
924
  /**
923
925
  * Provides info about the authentication status.
924
926
  */
@@ -1019,6 +1021,8 @@ declare class Authenticator {
1019
1021
  /** @internal */
1020
1022
  '@ditto.authClientValidityChanged'(isWebValid: boolean, isX509Valid: boolean): void;
1021
1023
  /** @internal */
1024
+ close(): void;
1025
+ /** @internal */
1022
1026
  protected keepAlive: KeepAlive;
1023
1027
  /** @internal */
1024
1028
  protected observerManager: ObserverManager;
@@ -1028,15 +1032,12 @@ declare class OnlineAuthenticator extends Authenticator {
1028
1032
  loginWithToken(token: string, provider: string): Promise<void>;
1029
1033
  loginWithUsernameAndPassword(username: string, password: string, provider: string): Promise<void>;
1030
1034
  logout(cleanupFn?: (Ditto: any) => void): Promise<void>;
1031
- readonly authClientPointer: Pointer<FFIAuthClient>;
1032
1035
  readonly authenticationHandler: AuthenticationHandler;
1033
1036
  private ditto;
1034
- constructor(keepAlive: KeepAlive, authClientPointer: Pointer<FFIAuthClient>, ditto: Ditto, authenticationHandler: AuthenticationHandler);
1037
+ constructor(keepAlive: KeepAlive, ditto: Ditto, authenticationHandler: AuthenticationHandler);
1035
1038
  '@ditto.authenticationExpiring'(secondsRemaining: number): void;
1036
1039
  '@ditto.authClientValidityChanged'(isWebValid: boolean, isX509Valid: boolean): void;
1037
1040
  private updateAndNotify;
1038
- private static finalizationRegistry;
1039
- private static finalize;
1040
1041
  }
1041
1042
  /** @internal */
1042
1043
  declare class NotAvailableAuthenticator extends Authenticator {
@@ -1300,277 +1301,398 @@ declare class Presence {
1300
1301
  observe(didChangeHandler: (presenceGraph: PresenceGraph) => void): Observer;
1301
1302
  /** @internal */
1302
1303
  constructor(ditto: Ditto);
1304
+ /** @internal */
1305
+ close(): void;
1303
1306
  private observerManager;
1304
1307
  }
1305
1308
 
1306
- /** Types of connections that can be established between two peers. */
1307
- type TransportCondition = 'Unknown' | 'OK' | 'GenericFailure' | 'AppInBackground' | 'MDNSFailure' | 'TCPListenFailure' | 'NoBLECentralPermission' | 'NoBLEPeripheralPermission' | 'CannotEstablishConnection' | 'BLEDisabled' | 'NoBLEHardware' | 'WiFiDisabled' | 'TemporarilyUnavailable';
1308
- /** The source for a transport condition. */
1309
- type ConditionSource = 'BLE' | 'TCP' | 'AWDL' | 'MDNS';
1310
- /**
1311
- * Types of connections that can be established between two peers.
1312
- * @deprecated replaced by {@link ConnectionType}.
1313
- */
1314
- type PresenceConnectionType = 'WiFi' | 'WebSocket' | 'AWDL' | 'BLE';
1315
- /**
1316
- * A peer object with information about an observed peer.
1317
- * @deprecated replaced by {@link Peer}.
1318
- */
1319
- type RemotePeer = {
1320
- networkID: string;
1321
- deviceName: string;
1322
- rssi?: number;
1323
- approximateDistanceInMeters?: number;
1324
- connections: PresenceConnectionType[];
1309
+ /** @internal */
1310
+ type SubscriptionContextInfo = {
1311
+ id: string;
1312
+ collectionName: string;
1313
+ query: string;
1314
+ queryArgsCBOR: Uint8Array | null;
1315
+ orderBys: OrderBy[];
1316
+ limit: number;
1317
+ offset: number;
1325
1318
  };
1326
1319
  /**
1327
- * Ditto is the entry point for accessing Ditto-related functionality.
1320
+ * Tracks `Subscription` instances in order to remove them when Ditto is
1321
+ * closed.
1322
+ *
1323
+ * @internal
1328
1324
  */
1329
- declare class Ditto {
1325
+ declare class SubscriptionManager {
1326
+ /** @internal */
1327
+ constructor(ditto: Ditto);
1330
1328
  /**
1331
- * Configure a custom identifier for the current device.
1329
+ * Begin tracking a subscription instance and start it.
1332
1330
  *
1333
- * When using {@link Presence.observe | presence.observe()}, each remote peer is
1334
- * represented by a short UTF-8 "device name". By default this will be a
1335
- * truncated version of the device's hostname. It does not need to be unique
1336
- * among peers. Configure the device name before calling
1337
- * {@link startSync | startSync()}. If it is too long it may be truncated.
1331
+ * @internal */
1332
+ add(subscription: Subscription): void;
1333
+ /**
1334
+ * Stop tracking a subscription instance and cancel it.
1335
+ *
1336
+ * @internal */
1337
+ remove(subscription: Subscription): void;
1338
+ /**
1339
+ * Stop tracking all subscriptions and cancel them.
1340
+ *
1341
+ * @internal */
1342
+ close(): void;
1343
+ private ditto;
1344
+ private subscriptions;
1345
+ private finalizationRegistry;
1346
+ /**
1347
+ * Remove tracked subscription without unregistering from finalization
1348
+ * registry.
1349
+ *
1350
+ * @internal */
1351
+ private removeWithContextinfo;
1352
+ }
1353
+
1354
+ /**
1355
+ * Used to subscribe to receive updates from remote peers about matching
1356
+ * documents.
1357
+ *
1358
+ * While {@link Subscription} objects remain in scope they ensure that
1359
+ * documents in the collection specified and that match the query provided will
1360
+ * try to be kept up-to-date with the latest changes from remote peers.
1361
+ */
1362
+ declare class Subscription {
1363
+ /**
1364
+ * The query that the subscription is based on.
1338
1365
  */
1339
- deviceName: string;
1340
- /** Returns a string identifying the version of the Ditto SDK. */
1341
- get sdkVersion(): string;
1366
+ readonly query: string;
1342
1367
  /**
1343
- * The (validated) identity this Ditto instance was initialized with.
1368
+ * Returns `true` if subscription has been explicitly cancelled, `false`
1369
+ * otherwise.
1344
1370
  */
1345
- readonly identity: Identity;
1371
+ readonly isCancelled = false;
1346
1372
  /**
1347
- * The path this Ditto instance was initialized with, if no path was given at
1348
- * construction time, the default value is returned (see constructor).
1373
+ * The name of the collection that the subscription is based on.
1349
1374
  */
1350
- readonly path: string;
1375
+ get collectionName(): string;
1351
1376
  /**
1352
- * Provides access to the SDK's store functionality.
1377
+ * Cancels a subscription and releases all associated resources.
1353
1378
  */
1354
- readonly store: Store;
1379
+ cancel(): void;
1380
+ /** @internal */
1381
+ constructor(collection: Collection, query: string, queryArgsCBOR: Uint8Array | null, orderBys: OrderBy[], limit: number, offset: number);
1355
1382
  /**
1356
- * Provides access to the SDK's presence functionality.
1383
+ * The collection this subscription belongs to.
1384
+ * @internal Because not exposed in any of the other SDKs (yet?).
1357
1385
  */
1358
- readonly presence: Presence;
1386
+ readonly collection: Collection;
1387
+ /** @internal */
1388
+ private readonly manager;
1359
1389
  /**
1360
- * Provides access to authentication methods for logging on to Ditto Cloud.
1390
+ * The corresponding named arguments for {@link query}, if any.
1391
+ * @internal Because not exposed in any of the other SDKs (yet?).
1361
1392
  */
1362
- readonly auth: Authenticator;
1393
+ readonly queryArgsCBOR: Uint8Array | null;
1394
+ /** @internal */
1395
+ readonly contextInfo: SubscriptionContextInfo;
1396
+ }
1397
+
1398
+ /**
1399
+ * An object that describes how a document's position in a live query's list of
1400
+ * matching documents has changed since the previous live query event.
1401
+ */
1402
+ interface LiveQueryMove {
1363
1403
  /**
1364
- * The site ID that the instance of `Ditto` is using as part of its identity.
1404
+ * The index of the document in the list of matching documents from the
1405
+ * previous live query event.
1365
1406
  */
1366
- readonly siteID: number | BigInt;
1407
+ readonly from: number;
1367
1408
  /**
1368
- * Returns `true` if an offline license token has been set, otherwise returns `false`.
1369
- *
1370
- * @see {@link setOfflineOnlyLicenseToken | setOfflineOnlyLicenseToken()}
1409
+ * The index of the document in the list of matching documents from the new
1410
+ * live query event.
1371
1411
  */
1372
- readonly isActivated: boolean;
1412
+ readonly to: number;
1413
+ }
1414
+ /** @internal */
1415
+ interface LiveQueryEventUpdateParams {
1416
+ oldDocuments: Document[];
1417
+ insertions: number[];
1418
+ deletions: number[];
1419
+ updates: number[];
1420
+ moves: LiveQueryMove[];
1421
+ }
1422
+ /**
1423
+ * First event fired immediately after registering a live query without any
1424
+ * mutations. All subsequent events are of type {@link LiveQueryEventUpdate}.
1425
+ */
1426
+ declare class LiveQueryEventInitial {
1373
1427
  /**
1374
- * Returns `true` if sync is active, otherwise returns `false`. Use
1375
- * {@link startSync | startSync()} to activate and {@link stopSync | stopSync()}
1376
- * to deactivate sync.
1428
+ * Whether or not this is the initial event being delivered. Always `true`
1429
+ * for `LiveQueryEventInitial`.
1377
1430
  */
1378
- readonly isSyncActive: boolean;
1431
+ readonly isInitial = true;
1379
1432
  /**
1380
- * Initializes a new `Ditto` instance.
1381
- *
1382
- * **NOTE**: The `sharedKey` identity is only supported for Node environments,
1383
- * using this to create a Ditto instance in the web browser will throw an
1384
- * exception.
1385
- *
1386
- * @param identity - Identity for the new Ditto instance, defaults to
1387
- * `offlinePlayground` with `appID` being the empty string `''`.
1388
- *
1389
- * @param path - On Node, `path` corresponds to a real directory
1390
- * on the file system (intermediate directories are created if needed). In the
1391
- * browser, `path` is used as an internal namespace for the in-memory storage.
1392
- * Defaults to `"ditto"`.
1393
- *
1394
- * @see {@link Ditto.identity}
1395
- * @see {@link Ditto.path}
1433
+ * Returns a hash that represents the set of matching documents.
1396
1434
  */
1397
- constructor(identity?: Identity, path?: string);
1435
+ hash(documents: Document[]): BigInt;
1398
1436
  /**
1399
- * Check if the current environment supports running Ditto.
1400
- *
1401
- * Required APIs include:
1402
- *
1403
- * - `BigInt`
1404
- * - `FinalizationRegistry`
1405
- * - `WeakRef`
1406
- *
1407
- * Internet Explorer is not supported.
1408
- *
1409
- * @returns true if the environment is supported
1437
+ * Returns a pattern of words that together create a mnemonic, which
1438
+ * represents the set of matching documents.
1410
1439
  */
1411
- static isEnvironmentSupported(): boolean;
1440
+ hashMnemonic(documents: Document[]): string;
1441
+ }
1442
+ /**
1443
+ * Represents an update event describing all changes that occured for documents
1444
+ * covered by a (live) query.
1445
+ */
1446
+ declare class LiveQueryEventUpdate {
1412
1447
  /**
1413
- * Activate a `Ditto` instance by setting an offline only license token. You cannot initiate sync
1414
- * with `Ditto` before you have activated it. The offline license token is only valid for identities
1415
- * of type `development`, `manual`, `offlinePlayground`, and `sharedKey`.
1416
- *
1417
- * @param licenseToken the license token to activate the `Ditto` instance
1418
- * with. You can find yours on the [Ditto portal](https://portal.ditto.live).
1448
+ * Whether or not this is the initial event being delivered. Always `false`
1449
+ * for `LiveQueryEventUpdate`.
1419
1450
  */
1420
- setOfflineOnlyLicenseToken(licenseToken: string): void;
1451
+ readonly isInitial = false;
1421
1452
  /**
1422
- * Returns the current transport configuration, frozen. If you want to modify
1423
- * the transport config, make a {@link TransportConfig.copy | copy} first. Or
1424
- * use the {@link updateTransportConfig | updateTransportConfig()}
1425
- * convenience method. By default peer-to-peer transports (Bluetooth, WiFi,
1426
- * and AWDL) are enabled if available in the current environment
1427
- * (Web, Node, OS, etc.).
1428
- *
1429
- * @see {@link setTransportConfig | setTransportConfig()}
1430
- * @see {@link updateTransportConfig | updateTransportConfig()}
1453
+ * The documents that previously matched the query, before the latest event.
1431
1454
  */
1432
- readonly transportConfig: TransportConfig;
1455
+ readonly oldDocuments: Document[];
1433
1456
  /**
1434
- * Assigns a new transports configuration. By default peer-to-peer transports
1435
- * (Bluetooth, WiFi, and AWDL) are enabled. You may use this method to alter
1436
- * the configuration at any time, however sync will not begin until
1437
- * {@link startSync | startSync()} is called.
1438
- *
1439
- * @see {@link transportConfig}
1440
- * @see {@link updateTransportConfig | updateTransportConfig()}
1457
+ * The indexes in the array of matching documents that accompany this event,
1458
+ * which relate to a document that was not in the previous most recent list of
1459
+ * matching documents.
1441
1460
  */
1442
- setTransportConfig(transportConfig: TransportConfig): void;
1461
+ readonly insertions: number[];
1443
1462
  /**
1444
- * Convenience method for updating the transport config. Creates a copy of the
1445
- * current transport config, passes that copy to the `update` closure,
1446
- * allowing it to mutate as needed, and sets that updated copy afterwards.
1463
+ * The indexes into the array {@link oldDocuments}, which relate to a document
1464
+ * that was in the previous most recent list of matching documents but is no
1465
+ * longer a matching document.
1447
1466
  */
1448
- updateTransportConfig(update: (transportConfig: TransportConfig) => void): Ditto;
1467
+ readonly deletions: number[];
1449
1468
  /**
1450
- * Starts the network transports. Ditto will connect to other devices.
1451
- *
1452
- * By default Ditto will enable all peer-to-peer transport types. On **Node**,
1453
- * this means BluetoothLE, WiFi/LAN, and AWDL. On the **Web**, only connecting
1454
- * via Websockets is supported. The network configuration can be
1455
- * customized with {@link updateTransportConfig | updateTransportConfig()}
1456
- * or replaced entirely with {@link setTransportConfig | setTransportConfig()}.
1457
- *
1458
- *
1459
- * Ditto will prevent the process from exiting until sync is stopped (not
1460
- * relevant when running in the browser).
1461
- *
1462
- * **NOTE**: the BluetoothLE transport on Linux is experimental, this
1463
- * method panics if no BluetoothLE hardware is available. Therefore, contrary
1464
- * to the above, the BluetoothLE transport is temporarily disabled by default
1465
- * on Linux.
1466
- *
1467
- * @see {@link isSyncActive}
1468
- * @see {@link stopSync | stopSync()}
1469
+ * The indexes in the array of matching documents that accompany this event,
1470
+ * which relate to a document that has been updated since the previous live
1471
+ * query event.
1469
1472
  */
1470
- startSync(): void;
1473
+ readonly updates: number[];
1471
1474
  /**
1472
- * Stops all network transports.
1473
- *
1474
- * You may continue to use the database locally but no data will sync to or
1475
- * from other devices.
1476
- *
1477
- * @see {@link isSyncActive}
1478
- * @see {@link startSync | startSync()}
1475
+ * Objects that describe how documents' positions in the list of matching
1476
+ * documents have changed since the previous live query event.
1479
1477
  */
1480
- stopSync(): void;
1478
+ readonly moves: LiveQueryMove[];
1481
1479
  /**
1482
- * Registers an observer for info about Ditto peers in range of this device.
1480
+ * Returns a hash that represents the set of matching documents.
1481
+ */
1482
+ hash(documents: Document[]): BigInt;
1483
+ /**
1484
+ * Returns a pattern of words that together create a mnemonic, which
1485
+ * represents the set of matching documents.
1486
+ */
1487
+ hashMnemonic(documents: Document[]): string;
1488
+ /** @internal */
1489
+ constructor(params: LiveQueryEventUpdateParams);
1490
+ }
1491
+ /**
1492
+ * Represents events delivered by a {@link LiveQuery}, which can be initial
1493
+ * (fired immediately upon registration) or an update (all subsequent events).
1494
+ */
1495
+ type LiveQueryEvent = LiveQueryEventInitial | LiveQueryEventUpdate;
1496
+ /**
1497
+ * Provides information about a live query event relating to a single document
1498
+ * live query.
1499
+ */
1500
+ declare class SingleDocumentLiveQueryEvent {
1501
+ /**
1502
+ * Whether or not this is the initial event being delivered.
1503
+ */
1504
+ readonly isInitial: boolean;
1505
+ /** The old representation of the document with the relveant document ID. */
1506
+ readonly oldDocument?: Document;
1507
+ /**
1508
+ * Returns a hash that represents the set of matching documents.
1509
+ */
1510
+ hash(document: Document | null): BigInt;
1511
+ /**
1512
+ * Returns a pattern of words that together create a mnemonic, which
1513
+ * represents the set of matching documents.
1514
+ */
1515
+ hashMnemonic(document: Document | null): string;
1516
+ /** @internal */
1517
+ constructor(isInitial: boolean, oldDocument?: Document);
1518
+ }
1519
+
1520
+ /**
1521
+ * The closure that is called whenever the documents covered by a live query
1522
+ * change.
1523
+ */
1524
+ type QueryObservationHandler = (documents: Document[], event: LiveQueryEvent, signalNext?: () => void) => void | Promise<void>;
1525
+ /**
1526
+ * These objects are returned when using `find`-like functionality on
1527
+ * {@link Collection}.
1528
+ *
1529
+ * They allow chaining of further query-related functions to do things like add
1530
+ * a limit to the number of documents you want returned or specify how you want
1531
+ * the documents to be sorted and ordered.
1532
+ *
1533
+ * You can either call {@link exec | exec()} on the object to get an array of
1534
+ * {@link Document | documents} as an immediate return value, or you can
1535
+ * establish either a live query or a subscription, which both work over time.
1536
+ *
1537
+ * A live query, established by calling
1538
+ * {@link PendingCursorOperation.observeLocal | observeLocal()}, will notify you
1539
+ * every time there's an update to a document that matches the query you
1540
+ * provided in the preceding `find`-like call.
1541
+ *
1542
+ * A subscription, established by calling
1543
+ * {@link PendingCursorOperation.subscribe | subscribe()}, will act as a signal
1544
+ * to other peers that the device connects to that you would like to receive
1545
+ * updates from them about documents that match the query you provided in the
1546
+ * preceding `find`-like call.
1547
+ *
1548
+ * Update and remove functionality is also exposed through this object.
1549
+ */
1550
+ declare class PendingCursorOperation extends BasePendingCursorOperation {
1551
+ sort(propertyPath: string, direction?: SortDirection): PendingCursorOperation;
1552
+ offset(offset: number): PendingCursorOperation;
1553
+ limit(limit: number): PendingCursorOperation;
1554
+ remove(): Promise<DocumentID[]>;
1555
+ evict(): Promise<DocumentID[]>;
1556
+ update(closure: (documents: MutableDocument[]) => void): Promise<UpdateResultsMap>;
1557
+ /**
1558
+ * Enables you to subscribe to changes that occur in a collection remotely.
1483
1559
  *
1484
- * Ditto will prevent the process from exiting as long as there are active
1485
- * peers observers (not relevant when running in the browser).
1560
+ * Having a subscription acts as a signal to other peers that you are
1561
+ * interested in receiving updates when local or remote changes are made to
1562
+ * documents that match the query generated by the chain of operations that
1563
+ * precedes the call to {@link subscribe | subscribe()}.
1486
1564
  *
1487
- * @param callback called immediately with the current state of peers
1488
- * in range and whenever that state changes. Then it will be invoked
1489
- * repeatedly when Ditto devices come and go, or the active connections to
1490
- * them change.
1565
+ * The returned {@link Subscription} object must be kept in scope for as long
1566
+ * as you want to keep receiving updates.
1491
1567
  *
1492
- * @deprecated please use {@link Presence.observe | presence.observe()} instead.
1568
+ * @returns A {@link Subscription} object that must be kept in scope for as
1569
+ * long as you want to keep receiving updates for documents that match the
1570
+ * query specified in the preceding chain.
1493
1571
  */
1494
- observePeers(callback: (peersData: RemotePeer[]) => void): Observer;
1572
+ subscribe(): Subscription;
1495
1573
  /**
1496
- * Register observer for changes of underlying transport conditions.
1574
+ * Enables you to listen for changes that occur in a collection locally.
1497
1575
  *
1498
- * Ditto will prevent the process from exiting as long as there are active
1499
- * transport conditions observers (not relevant when running in the browser).
1576
+ * The `handler` block will be called when local changes are
1577
+ * made to documents that match the query generated by the chain of operations
1578
+ * that precedes the call to {@link PendingCursorOperation.observeLocal | observeLocal()}.
1579
+ * The returned {@link LiveQuery} object must be kept in scope for as long as
1580
+ * you want the provided `handler` to be called when an update occurs.
1500
1581
  *
1501
- * @param callback called when underlying transport conditions change with
1502
- * the changed `condition` and it's `source`.
1503
- */
1504
- observeTransportConditions(callback: (condition: TransportCondition, source: ConditionSource) => void): Observer;
1505
- /**
1506
- * Removes all sync metadata for any remote peers which aren't currently
1507
- * connected. This method shouldn't usually be called. Manually running
1508
- * garbage collection often will result in slower sync times. Ditto
1509
- * automatically runs a garbage a collection process in the background at
1510
- * optimal times.
1582
+ * This won't subscribe to receive changes made remotely by others and so it
1583
+ * will only fire updates when a local change is made. If you want to receive
1584
+ * remotely performed updates as well, you'll have to create a subscription
1585
+ * via {@link PendingCursorOperation.subscribe | subscribe()} with the
1586
+ * relevant query. The returned {@link LiveQuery} object must be kept in scope
1587
+ * for as long as you want the provided `eventHandler` to be called when an
1588
+ * update occurs.
1511
1589
  *
1512
- * Manually running garbage collection is typically only useful during testing
1513
- * if large amounts of data are being generated. Alternatively, if an entire
1514
- * data set is to be evicted and it's clear that maintaining this metadata
1515
- * isn't necessary, then garbage collection could be run after evicting the
1516
- * old data.
1590
+ * @param handler A closure that will be called every time there is a
1591
+ * transaction committed to the store that involves modifications to documents
1592
+ * matching the query in the collection this method was called on.
1517
1593
  *
1518
- * Only available in Node environments at the moment, no-op in the browser.
1594
+ * @return A {@link LiveQuery} object that must be kept in scope for as long
1595
+ * as you want to keep receiving updates.
1519
1596
  */
1520
- runGarbageCollection(): void;
1597
+ observeLocal(handler: QueryObservationHandler): LiveQuery;
1521
1598
  /**
1522
- * Explicitly opt-in to disabling the ability to sync with Ditto peers running
1523
- * any version of the SDK in the v3 (or lower) series of releases.
1599
+ * Enables you to listen for changes that occur in a collection locally and
1600
+ * to signal when you are ready for the live query to deliver the next event.
1524
1601
  *
1525
- * Assuming this succeeds then this peer will only be able to sync with other
1526
- * peers using SDKs in the v4 (or higher) series of releases. Note that this
1527
- * disabling of sync spreads to peers that sync with a peer that has disabled,
1528
- * or has (transitively) had disabled, syncing with v3 SDK peers.
1602
+ * The `handler` block will be called when local changes are
1603
+ * made to documents that match the query generated by the chain of operations
1604
+ * that precedes the call to {@link PendingCursorOperation.observeLocalWithNextSignal | observeLocalWithNextSignal()}.
1605
+ * The returned {@link LiveQuery} object must be kept in scope for as long as
1606
+ * you want the provided `handler` to be called when an update occurs.
1607
+ *
1608
+ * This won't subscribe to receive changes made remotely by others and so it
1609
+ * will only fire updates when a local change is made. If you want to receive
1610
+ * remotely performed updates as well, you'll have to create a subscription
1611
+ * via {@link PendingCursorOperation.subscribe | subscribe()} with the
1612
+ * relevant query. The returned {@link LiveQuery} object must be kept in scope
1613
+ * for as long as you want the provided `eventHandler` to be called when an
1614
+ * update occurs.
1615
+ *
1616
+ * @param handler A closure that will be called every time there is a
1617
+ * transaction committed to the store that involves modifications to
1618
+ * documents matching the query in the collection that this method was called
1619
+ * on.
1620
+ *
1621
+ * @return A {@link LiveQuery} object that must be kept in scope for as long
1622
+ * as you want to keep receiving updates.
1529
1623
  */
1530
- disableSyncWithV3(): void;
1624
+ observeLocalWithNextSignal(handler: QueryObservationHandler): LiveQuery;
1531
1625
  /** @internal */
1532
- keepAlive: KeepAlive;
1533
- private sync;
1534
- private isWebValid;
1535
- private isX509Valid;
1536
- private presenceManager;
1537
- private transportConditionsManager;
1538
- private authClientValidityChanged;
1539
- private validateIdentity;
1540
- private setSyncActive;
1541
- private makeDefaultTransportConfig;
1626
+ constructor(query: string, queryArgs: QueryArguments | null, collection: Collection);
1627
+ /** @internal */
1628
+ _observe(handler: QueryObservationHandler, waitForNextSignal: boolean): LiveQuery;
1542
1629
  }
1543
- /** @internal */
1544
- declare const checkAPIs: (_globalObject?: Window | typeof globalThis) => boolean;
1545
1630
 
1546
1631
  /**
1547
- * Represents an attachment and can be used to insert the associated attachment
1548
- * into a document at a specific key-path. You can't instantiate an attachment
1549
- * directly, please use the {@link Collection.newAttachment | newAttachment()}
1550
- * method of {@link Collection} instead.
1632
+ * The type that is returned when calling
1633
+ * {@link PendingCursorOperation.observeLocal | observeLocal()} on a
1634
+ * {@link PendingCursorOperation} object. It handles the logic for calling the
1635
+ * event handler that is provided to `observeLocal()` calls.
1636
+ *
1637
+ * Ditto will prevent the process from exiting as long as there are active live
1638
+ * queries (not relevant when running in the browser).
1639
+ *
1640
+ * `LiveQuery` objects must be kept in scope for as long as you wish to have
1641
+ * your event handler be called when there is an update to a document matching
1642
+ * the query you provide. When you no longer want to receive updates about
1643
+ * documents matching a query then you must call {@link stop | stop()}.
1551
1644
  */
1552
- declare class Attachment {
1553
- /** @internal */
1554
- readonly ditto: Ditto;
1555
- /** @internal */
1556
- readonly token: AttachmentToken;
1557
- /** The attachment's metadata. */
1558
- get metadata(): {
1559
- [key: string]: string;
1560
- };
1561
- /**
1562
- * Returns the attachment's data.
1563
- */
1564
- getData(): Promise<Uint8Array>;
1645
+ declare class LiveQuery {
1646
+ /** The query that the live query is based on. */
1647
+ readonly query: string;
1648
+ /** The arguments belonging to {@link query}. */
1649
+ readonly queryArgs: QueryArguments | null;
1650
+ /** The name of the collection that the live query is based on. */
1651
+ get collectionName(): string;
1652
+ /** Returns true if the receiver has been stopped. */
1653
+ get isStopped(): boolean;
1565
1654
  /**
1566
- * Copies the attachment to the specified file path. Node-only,
1567
- * throws in the browser.
1568
- *
1569
- * @param path The path that the attachment should be copied to.
1655
+ * Stop the live query from delivering updates.
1570
1656
  */
1571
- copyToPath(path: string): Promise<void>;
1657
+ stop(): void;
1658
+ /** @internal */
1659
+ orderBys: OrderBy[];
1660
+ /** @internal */
1661
+ queryArgsCBOR: Uint8Array | null;
1662
+ /** @internal */
1663
+ readonly limit: number;
1664
+ /** @internal */
1665
+ readonly offset: number;
1666
+ /** @internal */
1667
+ readonly collection: Collection;
1668
+ /** @internal */
1669
+ readonly handler: QueryObservationHandler;
1670
+ /** @internal */
1671
+ liveQueryManager: LiveQueryManager | null;
1672
+ /** @internal */
1673
+ readonly liveQueryID: number;
1674
+ /** @internal */
1675
+ constructor(query: string, queryArgs: QueryArguments | null, queryArgsCBOR: Uint8Array | null, orderBys: OrderBy[], limit: number, offset: number, collection: Collection, handler: QueryObservationHandler);
1676
+ /** @internal */
1677
+ signalNext(): Promise<void>;
1678
+ }
1679
+
1680
+ /** @internal */
1681
+ declare class LiveQueryManager {
1682
+ readonly ditto: Ditto;
1683
+ readonly keepAlive: KeepAlive;
1572
1684
  /** @internal */
1573
- constructor(ditto: Ditto, token: AttachmentToken);
1685
+ constructor(ditto: Ditto, keepAlive: KeepAlive);
1686
+ /** @internal */
1687
+ startLiveQuery(liveQuery: LiveQuery): void;
1688
+ /** @internal */
1689
+ stopLiveQuery(liveQuery: LiveQuery): void;
1690
+ /** @internal */
1691
+ close(): void;
1692
+ private liveQueriesByID;
1693
+ private finalizationRegistry;
1694
+ private stopLiveQueryWithID;
1695
+ private finalize;
1574
1696
  }
1575
1697
 
1576
1698
  /**
@@ -1643,341 +1765,379 @@ declare class AttachmentFetcher implements PromiseLike<Attachment | null> {
1643
1765
  */
1644
1766
  stop(): void;
1645
1767
  /** @internal */
1646
- then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2>;
1768
+ readonly cancelTokenPromise: Promise<unknown | null> | null;
1647
1769
  /** @internal */
1648
1770
  readonly ditto: Ditto;
1649
1771
  /** @internal */
1772
+ readonly id: string;
1773
+ /** @internal */
1774
+ readonly manager: AttachmentFetcherManager;
1775
+ /** @internal */
1650
1776
  readonly token: AttachmentToken;
1651
1777
  /** @internal */
1652
- readonly eventHandler: (attachmentFetchEvent: AttachmentFetchEvent) => void | null;
1778
+ then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2>;
1653
1779
  /** @internal */
1654
- constructor(ditto: Ditto, token: AttachmentToken, eventHandler?: (attachmentFetchEvent: AttachmentFetchEvent) => void);
1655
- private cancelTokenPromise;
1656
- private static finalizationRegistry;
1657
- private static stopWithContextInfo;
1780
+ constructor(ditto: Ditto, token: AttachmentToken, manager: AttachmentFetcherManager, eventHandler?: (attachmentFetchEvent: AttachmentFetchEvent) => void);
1781
+ /**
1782
+ * This function is defined while a fetch is in progress and is used to reject
1783
+ * the promise `this.attachment` when the fetch is canceled.
1784
+ *
1785
+ * @internal
1786
+ */
1787
+ private rejectPendingFetch;
1658
1788
  }
1659
1789
 
1660
1790
  /**
1661
- * Used to subscribe to receive updates from remote peers about matching
1662
- * documents.
1791
+ * Manages attachment fetchers to make sure we free all resources when the
1792
+ * fetcher is garbage collected and to allow us to wait for freeing of
1793
+ * ressources to be finished before the ditto instance is closed.
1663
1794
  *
1664
- * While {@link Subscription} objects remain in scope they ensure that
1665
- * documents in the collection specified and that match the query provided will
1666
- * try to be kept up-to-date with the latest changes from remote peers.
1667
- */
1668
- declare class Subscription {
1669
- /**
1670
- * The query that the subscription is based on.
1671
- */
1672
- readonly query: string;
1673
- /**
1674
- * Returns `true` if subscription has been explicitly cancelled, `false`
1675
- * otherwise.
1676
- */
1677
- readonly isCancelled = false;
1795
+ * @internal */
1796
+ declare class AttachmentFetcherManager {
1797
+ readonly ditto: Ditto;
1798
+ /** @internal */
1799
+ constructor(ditto: Ditto);
1678
1800
  /**
1679
- * The name of the collection that the subscription is based on.
1680
- */
1681
- get collectionName(): string;
1801
+ * Start an attachment fetcher.
1802
+ *
1803
+ * @internal */
1804
+ startAttachmentFetcher(token: AttachmentToken, eventHandler?: (attachmentFetchEvent: AttachmentFetchEvent) => void): AttachmentFetcher;
1682
1805
  /**
1683
- * Cancels a subscription and releases all associated resources.
1684
- */
1685
- cancel(): void;
1686
- /** @internal */
1687
- constructor(collection: Collection, query: string, queryArgsCBOR: Uint8Array | null, orderBys: OrderBy[], limit: number, offset: number);
1806
+ * Stop an attachment fetcher and wait for it to be stopped.
1807
+ *
1808
+ * @internal */
1809
+ stopAttachmentFetcher(attachmentFetcher: AttachmentFetcher): Promise<void>;
1688
1810
  /**
1689
- * The collection this subscription belongs to.
1690
- * @internal Because not exposed in any of the other SDKs (yet?).
1811
+ * Closing the manager will cancel all attachment fetchers.
1812
+ *
1813
+ * @internal
1691
1814
  */
1692
- readonly collection: Collection;
1815
+ close(): void;
1816
+ private contextInfoByID;
1817
+ private finalizationRegistry;
1693
1818
  /**
1694
- * The corresponding named arguments for {@link query}, if any.
1695
- * @internal Because not exposed in any of the other SDKs (yet?).
1819
+ * Stop the attachment fetcher without unregistering it from the finalization
1820
+ * registry.
1696
1821
  */
1697
- readonly queryArgsCBOR: Uint8Array | null;
1698
- private static finalizationRegistry;
1699
- private static add;
1700
- private static remove;
1701
- private contextInfo;
1822
+ private stopWithContextInfo;
1702
1823
  }
1703
1824
 
1825
+ /** Types of connections that can be established between two peers. */
1826
+ type TransportCondition = 'Unknown' | 'OK' | 'GenericFailure' | 'AppInBackground' | 'MDNSFailure' | 'TCPListenFailure' | 'NoBLECentralPermission' | 'NoBLEPeripheralPermission' | 'CannotEstablishConnection' | 'BLEDisabled' | 'NoBLEHardware' | 'WiFiDisabled' | 'TemporarilyUnavailable';
1827
+ /** The source for a transport condition. */
1828
+ type ConditionSource = 'BLE' | 'TCP' | 'AWDL' | 'MDNS';
1704
1829
  /**
1705
- * The type that is returned when calling
1706
- * {@link PendingCursorOperation.observeLocal | observeLocal()} on a
1707
- * {@link PendingCursorOperation} object. It handles the logic for calling the
1708
- * event handler that is provided to `observeLocal()` calls.
1709
- *
1710
- * Ditto will prevent the process from exiting as long as there are active live
1711
- * queries (not relevant when running in the browser).
1712
- *
1713
- * `LiveQuery` objects must be kept in scope for as long as you wish to have
1714
- * your event handler be called when there is an update to a document matching
1715
- * the query you provide. When you no longer want to receive updates about
1716
- * documents matching a query then you must call {@link stop | stop()}.
1830
+ * Types of connections that can be established between two peers.
1831
+ * @deprecated replaced by {@link ConnectionType}.
1717
1832
  */
1718
- declare class LiveQuery {
1719
- /** The query that the live query is based on. */
1720
- readonly query: string;
1721
- /** The arguments belonging to {@link query}. */
1722
- readonly queryArgs: QueryArguments | null;
1723
- /** The name of the collection that the live query is based on. */
1724
- get collectionName(): string;
1725
- /** Returns true if the receiver has been stopped. */
1726
- get isStopped(): boolean;
1727
- /**
1728
- * Stop the live query from delivering updates.
1729
- */
1730
- stop(): void;
1731
- /** @internal */
1732
- readonly limit: number;
1733
- /** @internal */
1734
- readonly offset: number;
1735
- /** @internal */
1736
- readonly collection: Collection;
1737
- /** @internal */
1738
- readonly subscription?: Subscription;
1739
- /** @internal */
1740
- readonly handler: QueryObservationHandler;
1741
- /** @internal */
1742
- constructor(query: string, queryArgs: QueryArguments | null, queryArgsCBOR: Uint8Array | null, orderBys: OrderBy[], limit: number, offset: number, collection: Collection, subscription: Subscription | null, handler: QueryObservationHandler);
1743
- /** @internal */
1744
- signalNext(): Promise<void>;
1745
- private orderBys;
1746
- private queryArgsCBOR;
1747
- private liveQueryID;
1748
- }
1749
-
1833
+ type PresenceConnectionType = 'WiFi' | 'WebSocket' | 'AWDL' | 'BLE';
1750
1834
  /**
1751
- * An object that describes how a document's position in a live query's list of
1752
- * matching documents has changed since the previous live query event.
1835
+ * A peer object with information about an observed peer.
1836
+ * @deprecated replaced by {@link Peer}.
1753
1837
  */
1754
- interface LiveQueryMove {
1838
+ type RemotePeer = {
1839
+ networkID: string;
1840
+ deviceName: string;
1841
+ rssi?: number;
1842
+ approximateDistanceInMeters?: number;
1843
+ connections: PresenceConnectionType[];
1844
+ };
1845
+ /**
1846
+ * Ditto is the entry point for accessing Ditto-related functionality.
1847
+ */
1848
+ declare class Ditto {
1755
1849
  /**
1756
- * The index of the document in the list of matching documents from the
1757
- * previous live query event.
1850
+ * Configure a custom identifier for the current device.
1851
+ *
1852
+ * When using {@link Presence.observe | presence.observe()}, each remote peer is
1853
+ * represented by a short UTF-8 "device name". By default this will be a
1854
+ * truncated version of the device's hostname. It does not need to be unique
1855
+ * among peers. Configure the device name before calling
1856
+ * {@link startSync | startSync()}. If it is too long it may be truncated.
1758
1857
  */
1759
- readonly from: number;
1858
+ deviceName: string;
1859
+ /** Returns a string identifying the version of the Ditto SDK. */
1860
+ get sdkVersion(): string;
1760
1861
  /**
1761
- * The index of the document in the list of matching documents from the new
1762
- * live query event.
1862
+ * The (validated) identity this Ditto instance was initialized with.
1763
1863
  */
1764
- readonly to: number;
1765
- }
1766
- /** @internal */
1767
- interface LiveQueryEventUpdateParams {
1768
- oldDocuments: Document[];
1769
- insertions: number[];
1770
- deletions: number[];
1771
- updates: number[];
1772
- moves: LiveQueryMove[];
1773
- }
1774
- /**
1775
- * First event fired immediately after registering a live query without any
1776
- * mutations. All subsequent events are of type {@link LiveQueryEventUpdate}.
1777
- */
1778
- declare class LiveQueryEventInitial {
1864
+ readonly identity: Identity;
1779
1865
  /**
1780
- * Whether or not this is the initial event being delivered. Always `true`
1781
- * for `LiveQueryEventInitial`.
1866
+ * The path this Ditto instance was initialized with, if no path was given at
1867
+ * construction time, the default value is returned (see constructor).
1782
1868
  */
1783
- readonly isInitial = true;
1869
+ readonly path: string;
1784
1870
  /**
1785
- * Returns a hash that represents the set of matching documents.
1871
+ * Provides access to the SDK's store functionality.
1786
1872
  */
1787
- hash(documents: Document[]): BigInt;
1873
+ readonly store: Store;
1788
1874
  /**
1789
- * Returns a pattern of words that together create a mnemonic, which
1790
- * represents the set of matching documents.
1875
+ * Provides access to the SDK's presence functionality.
1791
1876
  */
1792
- hashMnemonic(documents: Document[]): string;
1793
- }
1794
- /**
1795
- * Represents an update event describing all changes that occured for documents
1796
- * covered by a (live) query.
1797
- */
1798
- declare class LiveQueryEventUpdate {
1877
+ readonly presence: Presence;
1799
1878
  /**
1800
- * Whether or not this is the initial event being delivered. Always `false`
1801
- * for `LiveQueryEventUpdate`.
1879
+ * Provides access to authentication methods for logging on to Ditto Cloud.
1802
1880
  */
1803
- readonly isInitial = false;
1881
+ readonly auth: Authenticator;
1804
1882
  /**
1805
- * The documents that previously matched the query, before the latest event.
1883
+ * The site ID that the instance of `Ditto` is using as part of its identity.
1806
1884
  */
1807
- readonly oldDocuments: Document[];
1885
+ readonly siteID: number | BigInt;
1808
1886
  /**
1809
- * The indexes in the array of matching documents that accompany this event,
1810
- * which relate to a document that was not in the previous most recent list of
1811
- * matching documents.
1887
+ * Returns `true` if an offline license token has been set, otherwise returns `false`.
1888
+ *
1889
+ * @see {@link setOfflineOnlyLicenseToken | setOfflineOnlyLicenseToken()}
1812
1890
  */
1813
- readonly insertions: number[];
1891
+ readonly isActivated: boolean;
1814
1892
  /**
1815
- * The indexes into the array {@link oldDocuments}, which relate to a document
1816
- * that was in the previous most recent list of matching documents but is no
1817
- * longer a matching document.
1893
+ * `true` once {@link close | Ditto.close()} has been called, otherwise
1894
+ * `false`.
1818
1895
  */
1819
- readonly deletions: number[];
1896
+ readonly isClosed: boolean;
1820
1897
  /**
1821
- * The indexes in the array of matching documents that accompany this event,
1822
- * which relate to a document that has been updated since the previous live
1823
- * query event.
1898
+ * Returns `true` if sync is active, otherwise returns `false`. Use
1899
+ * {@link startSync | startSync()} to activate and {@link stopSync | stopSync()}
1900
+ * to deactivate sync.
1824
1901
  */
1825
- readonly updates: number[];
1902
+ readonly isSyncActive: boolean;
1826
1903
  /**
1827
- * Objects that describe how documents' positions in the list of matching
1828
- * documents have changed since the previous live query event.
1904
+ * Initializes a new `Ditto` instance.
1905
+ *
1906
+ * **NOTE**: The `sharedKey` identity is only supported for Node environments,
1907
+ * using this to create a Ditto instance in the web browser will throw an
1908
+ * exception.
1909
+ *
1910
+ * @param identity - Identity for the new Ditto instance, defaults to
1911
+ * `offlinePlayground` with `appID` being the empty string `''`.
1912
+ *
1913
+ * @param path - On Node, `path` corresponds to a real directory
1914
+ * on the file system (intermediate directories are created if needed). In the
1915
+ * browser, `path` is used as an internal namespace for the in-memory storage.
1916
+ * Defaults to `"ditto"`.
1917
+ *
1918
+ * @see {@link Ditto.identity}
1919
+ * @see {@link Ditto.path}
1920
+ */
1921
+ constructor(identity?: Identity, path?: string);
1922
+ /**
1923
+ * Check if the current environment supports running Ditto.
1924
+ *
1925
+ * Required APIs include:
1926
+ *
1927
+ * - `BigInt`
1928
+ * - `FinalizationRegistry`
1929
+ * - `WeakRef`
1930
+ *
1931
+ * Internet Explorer is not supported.
1932
+ *
1933
+ * @returns true if the environment is supported
1934
+ */
1935
+ static isEnvironmentSupported(): boolean;
1936
+ /**
1937
+ * Activate a `Ditto` instance by setting an offline only license token. You cannot initiate sync
1938
+ * with `Ditto` before you have activated it. The offline license token is only valid for identities
1939
+ * of type `development`, `manual`, `offlinePlayground`, and `sharedKey`.
1940
+ *
1941
+ * @param licenseToken the license token to activate the `Ditto` instance
1942
+ * with. You can find yours on the [Ditto portal](https://portal.ditto.live).
1943
+ */
1944
+ setOfflineOnlyLicenseToken(licenseToken: string): void;
1945
+ /**
1946
+ * Returns the current transport configuration, frozen. If you want to modify
1947
+ * the transport config, make a {@link TransportConfig.copy | copy} first. Or
1948
+ * use the {@link updateTransportConfig | updateTransportConfig()}
1949
+ * convenience method. By default peer-to-peer transports (Bluetooth, WiFi,
1950
+ * and AWDL) are enabled if available in the current environment
1951
+ * (Web, Node, OS, etc.).
1952
+ *
1953
+ * @see {@link setTransportConfig | setTransportConfig()}
1954
+ * @see {@link updateTransportConfig | updateTransportConfig()}
1829
1955
  */
1830
- readonly moves: LiveQueryMove[];
1956
+ readonly transportConfig: TransportConfig;
1831
1957
  /**
1832
- * Returns a hash that represents the set of matching documents.
1958
+ * Assigns a new transports configuration. By default peer-to-peer transports
1959
+ * (Bluetooth, WiFi, and AWDL) are enabled. You may use this method to alter
1960
+ * the configuration at any time, however sync will not begin until
1961
+ * {@link startSync | startSync()} is called.
1962
+ *
1963
+ * @see {@link transportConfig}
1964
+ * @see {@link updateTransportConfig | updateTransportConfig()}
1833
1965
  */
1834
- hash(documents: Document[]): BigInt;
1966
+ setTransportConfig(transportConfig: TransportConfig): void;
1835
1967
  /**
1836
- * Returns a pattern of words that together create a mnemonic, which
1837
- * represents the set of matching documents.
1968
+ * Convenience method for updating the transport config. Creates a copy of the
1969
+ * current transport config, passes that copy to the `update` closure,
1970
+ * allowing it to mutate as needed, and sets that updated copy afterwards.
1838
1971
  */
1839
- hashMnemonic(documents: Document[]): string;
1840
- /** @internal */
1841
- constructor(params: LiveQueryEventUpdateParams);
1842
- }
1843
- /**
1844
- * Represents events delivered by a {@link LiveQuery}, which can be initial
1845
- * (fired immediately upon registration) or an update (all subsequent events).
1846
- */
1847
- type LiveQueryEvent = LiveQueryEventInitial | LiveQueryEventUpdate;
1848
- /**
1849
- * Provides information about a live query event relating to a single document
1850
- * live query.
1851
- */
1852
- declare class SingleDocumentLiveQueryEvent {
1972
+ updateTransportConfig(update: (transportConfig: TransportConfig) => void): Ditto;
1853
1973
  /**
1854
- * Whether or not this is the initial event being delivered.
1974
+ * Starts the network transports. Ditto will connect to other devices.
1975
+ *
1976
+ * By default Ditto will enable all peer-to-peer transport types. On **Node**,
1977
+ * this means BluetoothLE, WiFi/LAN, and AWDL. On the **Web**, only connecting
1978
+ * via Websockets is supported. The network configuration can be
1979
+ * customized with {@link updateTransportConfig | updateTransportConfig()}
1980
+ * or replaced entirely with {@link setTransportConfig | setTransportConfig()}.
1981
+ *
1982
+ *
1983
+ * Ditto will prevent the process from exiting until sync is stopped (not
1984
+ * relevant when running in the browser).
1985
+ *
1986
+ * **NOTE**: the BluetoothLE transport on Linux is experimental, this
1987
+ * method panics if no BluetoothLE hardware is available. Therefore, contrary
1988
+ * to the above, the BluetoothLE transport is temporarily disabled by default
1989
+ * on Linux.
1990
+ *
1991
+ * @see {@link isSyncActive}
1992
+ * @see {@link stopSync | stopSync()}
1855
1993
  */
1856
- readonly isInitial: boolean;
1857
- /** The old representation of the document with the relveant document ID. */
1858
- readonly oldDocument?: Document;
1994
+ startSync(): void;
1859
1995
  /**
1860
- * Returns a hash that represents the set of matching documents.
1996
+ * Stops all network transports.
1997
+ *
1998
+ * You may continue to use the database locally but no data will sync to or
1999
+ * from other devices.
2000
+ *
2001
+ * @see {@link isSyncActive}
2002
+ * @see {@link startSync | startSync()}
1861
2003
  */
1862
- hash(document: Document | null): BigInt;
2004
+ stopSync(): void;
1863
2005
  /**
1864
- * Returns a pattern of words that together create a mnemonic, which
1865
- * represents the set of matching documents.
2006
+ * Registers an observer for info about Ditto peers in range of this device.
2007
+ *
2008
+ * Ditto will prevent the process from exiting as long as there are active
2009
+ * peers observers (not relevant when running in the browser).
2010
+ *
2011
+ * @param callback called immediately with the current state of peers
2012
+ * in range and whenever that state changes. Then it will be invoked
2013
+ * repeatedly when Ditto devices come and go, or the active connections to
2014
+ * them change.
2015
+ *
2016
+ * @deprecated please use {@link Presence.observe | presence.observe()} instead.
1866
2017
  */
1867
- hashMnemonic(document: Document | null): string;
1868
- /** @internal */
1869
- constructor(isInitial: boolean, oldDocument?: Document);
1870
- }
1871
-
1872
- /**
1873
- * The closure that is called whenever the documents covered by a live query
1874
- * change.
1875
- */
1876
- type QueryObservationHandler = (documents: Document[], event: LiveQueryEvent, signalNext?: () => void) => void | Promise<void>;
1877
- /**
1878
- * These objects are returned when using `find`-like functionality on
1879
- * {@link Collection}.
1880
- *
1881
- * They allow chaining of further query-related functions to do things like add
1882
- * a limit to the number of documents you want returned or specify how you want
1883
- * the documents to be sorted and ordered.
1884
- *
1885
- * You can either call {@link exec | exec()} on the object to get an array of
1886
- * {@link Document | documents} as an immediate return value, or you can
1887
- * establish either a live query or a subscription, which both work over time.
1888
- *
1889
- * A live query, established by calling
1890
- * {@link PendingCursorOperation.observeLocal | observeLocal()}, will notify you
1891
- * every time there's an update to a document that matches the query you
1892
- * provided in the preceding `find`-like call.
1893
- *
1894
- * A subscription, established by calling
1895
- * {@link PendingCursorOperation.subscribe | subscribe()}, will act as a signal
1896
- * to other peers that the device connects to that you would like to receive
1897
- * updates from them about documents that match the query you provided in the
1898
- * preceding `find`-like call.
1899
- *
1900
- * Update and remove functionality is also exposed through this object.
1901
- */
1902
- declare class PendingCursorOperation extends BasePendingCursorOperation {
1903
- sort(propertyPath: string, direction?: SortDirection): PendingCursorOperation;
1904
- offset(offset: number): PendingCursorOperation;
1905
- limit(limit: number): PendingCursorOperation;
1906
- remove(): Promise<DocumentID[]>;
1907
- evict(): Promise<DocumentID[]>;
1908
- update(closure: (documents: MutableDocument[]) => void): Promise<UpdateResultsMap>;
2018
+ observePeers(callback: (peersData: RemotePeer[]) => void): Observer;
1909
2019
  /**
1910
- * Enables you to subscribe to changes that occur in a collection remotely.
1911
- *
1912
- * Having a subscription acts as a signal to other peers that you are
1913
- * interested in receiving updates when local or remote changes are made to
1914
- * documents that match the query generated by the chain of operations that
1915
- * precedes the call to {@link subscribe | subscribe()}.
2020
+ * Register observer for changes of underlying transport conditions.
1916
2021
  *
1917
- * The returned {@link Subscription} object must be kept in scope for as long
1918
- * as you want to keep receiving updates.
2022
+ * Ditto will prevent the process from exiting as long as there are active
2023
+ * transport conditions observers (not relevant when running in the browser).
1919
2024
  *
1920
- * @returns A {@link Subscription} object that must be kept in scope for as
1921
- * long as you want to keep receiving updates for documents that match the
1922
- * query specified in the preceding chain.
2025
+ * @param callback called when underlying transport conditions change with
2026
+ * the changed `condition` and it's `source`.
1923
2027
  */
1924
- subscribe(): Subscription;
2028
+ observeTransportConditions(callback: (condition: TransportCondition, source: ConditionSource) => void): Observer;
1925
2029
  /**
1926
- * Enables you to listen for changes that occur in a collection locally.
2030
+ * Removes all sync metadata for any remote peers which aren't currently
2031
+ * connected. This method shouldn't usually be called. Manually running
2032
+ * garbage collection often will result in slower sync times. Ditto
2033
+ * automatically runs a garbage a collection process in the background at
2034
+ * optimal times.
1927
2035
  *
1928
- * The `handler` block will be called when local changes are
1929
- * made to documents that match the query generated by the chain of operations
1930
- * that precedes the call to {@link PendingCursorOperation.observeLocal | observeLocal()}.
1931
- * The returned {@link LiveQuery} object must be kept in scope for as long as
1932
- * you want the provided `handler` to be called when an update occurs.
2036
+ * Manually running garbage collection is typically only useful during testing
2037
+ * if large amounts of data are being generated. Alternatively, if an entire
2038
+ * data set is to be evicted and it's clear that maintaining this metadata
2039
+ * isn't necessary, then garbage collection could be run after evicting the
2040
+ * old data.
1933
2041
  *
1934
- * This won't subscribe to receive changes made remotely by others and so it
1935
- * will only fire updates when a local change is made. If you want to receive
1936
- * remotely performed updates as well, you'll have to create a subscription
1937
- * via {@link PendingCursorOperation.subscribe | subscribe()} with the
1938
- * relevant query. The returned {@link LiveQuery} object must be kept in scope
1939
- * for as long as you want the provided `eventHandler` to be called when an
1940
- * update occurs.
2042
+ * Only available in Node environments at the moment, no-op in the browser.
2043
+ */
2044
+ runGarbageCollection(): void;
2045
+ /**
2046
+ * Explicitly opt-in to disabling the ability to sync with Ditto peers running
2047
+ * any version of the SDK in the v3 (or lower) series of releases.
1941
2048
  *
1942
- * @param handler A closure that will be called every time there is a
1943
- * transaction committed to the store that involves modifications to documents
1944
- * matching the query in the collection this method was called on.
2049
+ * Assuming this succeeds then this peer will only be able to sync with other
2050
+ * peers using SDKs in the v4 (or higher) series of releases. Note that this
2051
+ * disabling of sync spreads to peers that sync with a peer that has disabled,
2052
+ * or has (transitively) had disabled, syncing with v3 SDK peers.
2053
+ */
2054
+ disableSyncWithV3(): void;
2055
+ /**
2056
+ * Shut down Ditto and release all resources.
1945
2057
  *
1946
- * @return A {@link LiveQuery} object that must be kept in scope for as long
1947
- * as you want to keep receiving updates.
2058
+ * Must be called before recreating a Ditto instance that uses the same
2059
+ * persistence directory.
1948
2060
  */
1949
- observeLocal(handler: QueryObservationHandler): LiveQuery;
2061
+ close(): Promise<void>;
2062
+ /** @internal */
2063
+ keepAlive: KeepAlive;
2064
+ /** @internal */
2065
+ liveQueryManager: LiveQueryManager;
2066
+ /** @internal */
2067
+ subscriptionManager: SubscriptionManager;
2068
+ /** @internal */
2069
+ attachmentFetcherManager: AttachmentFetcherManager;
1950
2070
  /**
1951
- * Enables you to listen for changes that occur in a collection locally and
1952
- * to signal when you are ready for the live query to deliver the next event.
2071
+ * The number of operations pending before the Ditto instance can be closed.
1953
2072
  *
1954
- * The `handler` block will be called when local changes are
1955
- * made to documents that match the query generated by the chain of operations
1956
- * that precedes the call to {@link PendingCursorOperation.observeLocalWithNextSignal | observeLocalWithNextSignal()}.
1957
- * The returned {@link LiveQuery} object must be kept in scope for as long as
1958
- * you want the provided `handler` to be called when an update occurs.
2073
+ * For testing purposes only.
2074
+ * @internal */
2075
+ get numPendingOperations(): number;
2076
+ /**
2077
+ * Makes sure that the closure is executed only if the Ditto instance hasn't
2078
+ * been closed yet.
1959
2079
  *
1960
- * This won't subscribe to receive changes made remotely by others and so it
1961
- * will only fire updates when a local change is made. If you want to receive
1962
- * remotely performed updates as well, you'll have to create a subscription
1963
- * via {@link PendingCursorOperation.subscribe | subscribe()} with the
1964
- * relevant query. The returned {@link LiveQuery} object must be kept in scope
1965
- * for as long as you want the provided `eventHandler` to be called when an
1966
- * update occurs.
2080
+ * @param closure the synchronous closure to execute.
2081
+ * @returns the result of the closure.
2082
+ * @throws if the Ditto instance was closed before calling this method.
2083
+ * @internal */
2084
+ deferClose<T>(closure: () => T): T;
2085
+ /**
2086
+ * Makes sure that the closure is executed to completion before the Ditto
2087
+ * instance is closed.
1967
2088
  *
1968
- * @param handler A closure that will be called every time there is a
1969
- * transaction committed to the store that involves modifications to
1970
- * documents matching the query in the collection that this method was called
1971
- * on.
2089
+ * Any calls to {@link close | `Ditto.close()`} will wait until the closure
2090
+ * has completed before closing the Ditto instance.
1972
2091
  *
1973
- * @return A {@link LiveQuery} object that must be kept in scope for as long
1974
- * as you want to keep receiving updates.
1975
- */
1976
- observeLocalWithNextSignal(handler: QueryObservationHandler): LiveQuery;
2092
+ * @param closure the asynchronous closure to execute.
2093
+ * @returns the result of the closure.
2094
+ * @throws if the Ditto instance was closed before calling this method.
2095
+ * @internal */
2096
+ deferCloseAsync<T>(closure: () => Promise<T>): Promise<T>;
2097
+ private sync;
2098
+ private deferCloseAllowed;
2099
+ private isWebValid;
2100
+ private isX509Valid;
2101
+ private presenceManager;
2102
+ private transportConditionsManager;
2103
+ /** Set of pending operations that need to complete before the Ditto instance can be closed in a safe manner. */
2104
+ private pendingOperations;
2105
+ private authClientValidityChanged;
2106
+ private validateIdentity;
2107
+ private setSyncActive;
2108
+ private makeDefaultTransportConfig;
2109
+ }
2110
+ /** @internal */
2111
+ declare const checkAPIs: (_globalObject?: Window | typeof globalThis) => boolean;
2112
+
2113
+ /**
2114
+ * Represents an attachment and can be used to insert the associated attachment
2115
+ * into a document at a specific key-path. You can't instantiate an attachment
2116
+ * directly, please use the {@link Collection.newAttachment | newAttachment()}
2117
+ * method of {@link Collection} instead.
2118
+ */
2119
+ declare class Attachment {
1977
2120
  /** @internal */
1978
- constructor(query: string, queryArgs: QueryArguments | null, collection: Collection);
2121
+ readonly ditto: Ditto;
2122
+ /** @internal */
2123
+ readonly token: AttachmentToken;
2124
+ /** The attachment's metadata. */
2125
+ get metadata(): {
2126
+ [key: string]: string;
2127
+ };
2128
+ /**
2129
+ * Returns the attachment's data.
2130
+ */
2131
+ getData(): Promise<Uint8Array>;
2132
+ /**
2133
+ * Copies the attachment to the specified file path. Node-only,
2134
+ * throws in the browser.
2135
+ *
2136
+ * @param path The path that the attachment should be copied to.
2137
+ */
2138
+ copyToPath(path: string): Promise<void>;
1979
2139
  /** @internal */
1980
- _observe(handler: QueryObservationHandler, createSubscription: boolean, waitForNextSignal: boolean): LiveQuery;
2140
+ constructor(ditto: Ditto, token: AttachmentToken);
1981
2141
  }
1982
2142
 
1983
2143
  /**
@@ -2132,7 +2292,7 @@ declare class PendingIDSpecificOperation extends BasePendingIDSpecificOperation
2132
2292
  /** @internal */
2133
2293
  constructor(documentID: DocumentID, collection: Collection);
2134
2294
  /** @internal */
2135
- _observe(handler: SingleObservationHandler, createSubscription: boolean, waitForNextSignal: boolean): LiveQuery;
2295
+ _observe(handler: SingleObservationHandler, waitForNextSignal: boolean): LiveQuery;
2136
2296
  }
2137
2297
 
2138
2298
  /**
@@ -2240,7 +2400,7 @@ declare class Collection implements CollectionInterface {
2240
2400
  * fetch request to proceed and for you to be notified about the attachment's
2241
2401
  * fetch status changes.
2242
2402
  */
2243
- fetchAttachment(token: AttachmentToken, eventHandler?: (AttachmentFetchEvent: any) => void): AttachmentFetcher;
2403
+ fetchAttachment(token: AttachmentToken, eventHandler?: (event: AttachmentFetchEvent) => void): AttachmentFetcher;
2244
2404
  /** @internal */
2245
2405
  constructor(name: string, store: Store);
2246
2406
  /** @internal */
@@ -2435,7 +2595,7 @@ declare class PendingCollectionsOperation implements PromiseLike<Collection[]> {
2435
2595
  /** @internal */
2436
2596
  then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2>;
2437
2597
  /** @internal */
2438
- _observe(handler: CollectionsObservationHandler, createSubscription: boolean, waitForNextSignal: boolean): LiveQuery;
2598
+ _observe(handler: CollectionsObservationHandler, waitForNextSignal: boolean): LiveQuery;
2439
2599
  private readonly pendingCursorOperation;
2440
2600
  }
2441
2601
 
@@ -2467,6 +2627,8 @@ declare class ExperimentalStore {
2467
2627
  * @returns a promise for an array of Ditto documents matching the query
2468
2628
  **/
2469
2629
  execute(query: string): Promise<Document[]>;
2630
+ /** @internal */
2631
+ close(): void;
2470
2632
  }
2471
2633
 
2472
2634
  /**
@@ -2670,6 +2832,8 @@ declare class Store {
2670
2832
  write(callback: (transaction: WriteTransaction) => Promise<void>): Promise<WriteTransactionResult[]>;
2671
2833
  /** @internal */
2672
2834
  constructor(ditto: Ditto);
2835
+ /** @internal */
2836
+ close(): void;
2673
2837
  /**
2674
2838
  * Private method, used only by the Portal https://github.com/getditto/ditto/pull/3652
2675
2839
  * @internal