UNPKG

realm

Version:

Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores

109 lines (108 loc) 4.64 kB
import { AnyResults, BaseSubscriptionSet, Subscription, binding } from "../internal"; /** * Behavior when waiting for subscribed objects to be synchronized/downloaded. */ export declare enum WaitForSync { /** * Waits until the objects have been downloaded from the server * the first time the subscription is created. If the subscription * already exists, the `Results` is returned immediately. */ FirstTime = "first-time", /** * Always waits until the objects have been downloaded from the server. */ Always = "always", /** * Never waits for the download to complete, but keeps downloading the * objects in the background. */ Never = "never" } /** * Options for {@link MutableSubscriptionSet.add}. */ export type SubscriptionOptions = { /** * Sets the name of the subscription being added. This allows you to later refer * to the subscription by name, e.g. when calling {@link MutableSubscriptionSet.removeByName}. */ name?: string; /** * By default, adding a subscription with the same name as an existing one * but a different query will update the existing subscription with the new * query. If `throwOnUpdate` is set to true, adding a subscription with the * same name but a different query will instead throw an exception. * Adding a subscription with the same name and query is always a no-op. */ throwOnUpdate?: boolean; /** * Specifies how to wait or not wait for subscribed objects to be downloaded. * @default WaitForSync.FirstTime */ behavior?: WaitForSync; /** * The maximum time (in milliseconds) to wait for objects to be downloaded. * If the time exceeds this limit, the `Results` is returned and the download * continues in the background. */ timeout?: number; }; /** * The mutable version of a given SubscriptionSet. The {@link MutableSubscriptionSet} * instance can only be used from inside the {@link SubscriptionSet.update} callback. */ export declare class MutableSubscriptionSet extends BaseSubscriptionSet { /** @internal */ protected internal: binding.MutableSyncSubscriptionSet; /** @internal */ constructor(/** @internal */ internal: binding.MutableSyncSubscriptionSet); /** * Add a query to the set of active subscriptions. The query will be joined via * an `OR` operator with any existing queries for the same type. * * A query is represented by a {@link Results} instance returned from {@link Realm.objects}, * for example: `mutableSubs.add(realm.objects("Cat").filtered("age > 10"));`. * @param query - A {@link Results} instance representing the query to subscribe to. * @param options - An optional {@link SubscriptionOptions} object containing options to * use when adding this subscription (e.g. to give the subscription a name). * @returns A `Subscription` instance for the new subscription. */ add(query: AnyResults, options?: SubscriptionOptions): Subscription; /** * Remove a subscription with the given query from the SubscriptionSet. * @param query - A {@link Results} instance representing the query to remove a subscription to. * @returns `true` if the subscription was removed, `false` if it was not found. */ remove(query: AnyResults): boolean; /** * Remove a subscription with the given name from the SubscriptionSet. * @param name - The name of the subscription to remove. * @returns `true` if the subscription was removed, `false` if it was not found. */ removeByName(name: string): boolean; /** * Remove the specified subscription from the SubscriptionSet. * @param subscription - The {@link Subscription} instance to remove. * @returns `true` if the subscription was removed, `false` if it was not found. */ removeSubscription(subscription: Subscription): boolean; /** * Remove all subscriptions for the specified object type from the SubscriptionSet. * @param objectType - The string name of the object type to remove all subscriptions for. * @returns The number of subscriptions removed. */ removeByObjectType(objectType: string): number; /** * Remove all subscriptions from the SubscriptionSet. * @returns The number of subscriptions removed. */ removeAll(): number; /** * Remove all unnamed/anonymous subscriptions from the SubscriptionSet. * @returns The number of subscriptions removed. */ removeUnnamed(): number; /** @internal */ private removeByPredicate; }