Skip to content

Commit

Permalink
Suggestion for default open behavior
Browse files Browse the repository at this point in the history
* deprecate the static types that were imported from v11
* define the defaults locally in the ProgressRealmPromise
* document what the defaults for these fields are in SyncConfiguration
  • Loading branch information
takameyer committed Jul 7, 2023
1 parent fa9ca54 commit 18ed754
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
15 changes: 12 additions & 3 deletions packages/realm/src/ProgressRealmPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,20 @@ type OpenBehavior = {
timeOutBehavior?: OpenRealmTimeOutBehavior;
};

const defaultLocalOpenRealmConfiguration = {
openBehavior: OpenRealmBehaviorType.OpenImmediately,
};

const defaultSyncOpenRealmConfiguration = {
openBehavior: OpenRealmBehaviorType.DownloadBeforeOpen,
timeOut: 30 * 1000,
timeOutBehavior: OpenRealmTimeOutBehavior.ThrowException,
};

function determineBehavior(config: Configuration, realmExists: boolean): OpenBehavior {
const { sync, openSyncedRealmLocally } = config;
if (!sync || openSyncedRealmLocally) {
return { openBehavior: Realm.App.Sync.defaultLocalOpenRealmConfiguration.type };
return defaultLocalOpenRealmConfiguration;
} else {
const configProperty = realmExists ? "existingRealmFileBehavior" : "newRealmFileBehavior";
const configBehavior = sync[configProperty];
Expand All @@ -52,8 +62,7 @@ function determineBehavior(config: Configuration, realmExists: boolean): OpenBeh
}
return { openBehavior: type, timeOut, timeOutBehavior };
} else {
const { type, timeOut, timeOutBehavior } = Realm.App.Sync.defaultSyncOpenRealmConfiguration;
return { openBehavior: type, timeOut, timeOutBehavior };
return defaultSyncOpenRealmConfiguration;
}
}
}
Expand Down
27 changes: 7 additions & 20 deletions packages/realm/src/app-services/Sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
Logger,
MutableSubscriptionSet,
NumericLogLevel,
OpenRealmBehaviorConfiguration,
OpenRealmBehaviorType,
OpenRealmTimeOutBehavior,
PartitionValue,
Expand Down Expand Up @@ -172,33 +171,21 @@ export class Sync {
}

/**
* The default OpenRealmBehaviorConfiguration used for local realms.
* This is used if the behavior is not specified by the developer when opening the realm.
* @internal
* The default behavior settings if you want to open a synchronized Realm immediately and start working on it.
* If this is the first time you open the Realm, it will be empty while the server data is being downloaded in the background.
* @deprecated since v12
*/
static defaultLocalOpenRealmConfiguration: Readonly<OpenRealmBehaviorConfiguration> = {
static openLocalRealmBehavior = {
type: OpenRealmBehaviorType.OpenImmediately,
};

/**
* The default OpenRealmBehaviorConfiguration used for synced realms.
* This is used if the behavior is not specified by the developer when opening the realm.
* @internal
* The default behavior settings if you want to wait for downloading a synchronized Realm to complete before opening it.
* @deprecated since v12
*/
static defaultSyncOpenRealmConfiguration: Readonly<OpenRealmBehaviorConfiguration> = {
static downloadBeforeOpenBehavior = {
type: OpenRealmBehaviorType.DownloadBeforeOpen,
timeOut: 30 * 1000,
timeOutBehavior: OpenRealmTimeOutBehavior.ThrowException,
};

/**
* The default behavior settings if you want to open a synchronized Realm immediately and start working on it.
* If this is the first time you open the Realm, it will be empty while the server data is being downloaded in the background.
*/
static openLocalRealmBehavior = Sync.defaultLocalOpenRealmConfiguration;

/**
* The default behavior settings if you want to wait for downloading a synchronized Realm to complete before opening it.
*/
static downloadBeforeOpenBehavior = Sync.defaultSyncOpenRealmConfiguration;
}
16 changes: 16 additions & 0 deletions packages/realm/src/app-services/SyncConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,27 @@ export type BaseSyncConfiguration = {
user: AnyUser;
/**
* Whether to create a new file and sync in background or wait for the file to be synced.
* The default behavior is:
*```
* {
* type: OpenRealmBehaviorType.DownloadBeforeOpen,
* timeOut: 30 * 1000,
* timeOutBehavior: OpenRealmTimeOutBehavior.ThrowException,
* }
* ```
*/
newRealmFileBehavior?: OpenRealmBehaviorConfiguration;
/**
* Whether to open existing file and sync in background or wait for the sync of the file to complete and then open.
* If not set, the Realm will be downloaded before opened.
* The default behavior is:
*```
* {
* type: OpenRealmBehaviorType.DownloadBeforeOpen,
* timeOut: 30 * 1000,
* timeOutBehavior: OpenRealmTimeOutBehavior.ThrowException,
* }
* ```
*/
existingRealmFileBehavior?: OpenRealmBehaviorConfiguration;
/**
Expand Down

0 comments on commit 18ed754

Please sign in to comment.