Skip to content

Commit

Permalink
Merge 7b9b4ea into 47c4a3e
Browse files Browse the repository at this point in the history
  • Loading branch information
papafe committed Jun 30, 2023
2 parents 47c4a3e + 7b9b4ea commit 3e92673
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
24 changes: 7 additions & 17 deletions integration-tests/tests/src/tests/sync/open-behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////
import { expect } from "chai";
import Realm, { BSON } from "realm";
import Realm, { BSON, OpenRealmBehaviorType, OpenRealmTimeOutBehavior } from "realm";
import { generatePartition, randomVerifiableEmail } from "../../utils/generators";
import { importAppBefore } from "../../hooks";
import { sleep } from "../../utils/sleep";
Expand All @@ -33,7 +33,7 @@ const DogForSyncSchema = {
},
};

async function getRegisteredEmailPassCredentials(app: Realm.App) {
async function getRegisteredEmailPassCredentials(app: Realm.App<any, any>) {
if (!app) {
throw new Error("No app supplied to 'getRegisteredEmailPassCredentials'");
}
Expand All @@ -52,8 +52,11 @@ describe("OpenBehaviour", function () {
afterEach(() => Realm.clearTestState());

it("static references are defined", () => {
expect(Realm.App.Sync.openLocalRealmBehavior).to.not.be.undefined;
expect(Realm.App.Sync.downloadBeforeOpenBehavior).to.not.be.undefined;
expect(Realm.App.Sync.openLocalRealmBehavior.type).to.equal(OpenRealmBehaviorType.OpenImmediately);

expect(Realm.App.Sync.downloadBeforeOpenBehavior.type).to.equal(OpenRealmBehaviorType.DownloadBeforeOpen);
expect(Realm.App.Sync.downloadBeforeOpenBehavior.timeOut).to.equal(30000);
expect(Realm.App.Sync.downloadBeforeOpenBehavior.timeOutBehavior).to.equal(OpenRealmTimeOutBehavior.ThrowException);
});

it("open synced realm with localRealmBehaviour", async function (this: AppContext) {
Expand Down Expand Up @@ -126,7 +129,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error const enum is removed at runtime, should either remove const or leave this as is.
_sessionStopPolicy: "immediately",
//@ts-expect-error const enum is removed at runtime, should either remove const or leave this as is.
newRealmFileBehavior: { type: "downloadBeforeOpen" },
Expand Down Expand Up @@ -185,7 +187,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
existingRealmFileBehavior: { type: "downloadBeforeOpen" },
Expand All @@ -206,7 +207,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
newRealmFileBehavior: {
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
Expand Down Expand Up @@ -252,7 +252,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
existingRealmFileBehavior: {
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
Expand Down Expand Up @@ -315,7 +314,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
newRealmFileBehavior: {
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
Expand Down Expand Up @@ -396,7 +394,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
existingRealmFileBehavior: {
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
Expand Down Expand Up @@ -463,7 +460,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
existingRealmFileBehavior: {
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
Expand Down Expand Up @@ -492,7 +488,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
newRealmFileBehavior: { type: "downloadBeforeOpen" },
Expand Down Expand Up @@ -527,7 +522,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
newRealmFileBehavior: { type: "downloadBeforeOpen" },
Expand All @@ -550,7 +544,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
newRealmFileBehavior: { type: "downloadBeforeOpen" },
Expand Down Expand Up @@ -592,7 +585,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
//@ts-expect-error testing invalid value for newRealmFileBehaviour
newRealmFileBehavior: { type: "foo" },
Expand All @@ -606,7 +598,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
//@ts-expect-error testing invalid value for newRealmFileBehaviour
newRealmFileBehavior: { type: "openLocalRealm", timeOutBehavior: "foo" },
Expand All @@ -620,7 +611,6 @@ describe("OpenBehaviour", function () {
sync: {
user,
partitionValue,
//@ts-expect-error TYPEBUG: cannot access const enum at runtime
_sessionStopPolicy: "immediately",
//@ts-expect-error testing invalid value for newRealmFileBehaviour
newRealmFileBehavior: { type: "openLocalRealm", timeOut: "bar" },
Expand Down
5 changes: 3 additions & 2 deletions packages/realm/src/ProgressRealmPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type OpenBehavior = {
function determineBehavior(config: Configuration, realmExists: boolean): OpenBehavior {
const { sync, openSyncedRealmLocally } = config;
if (!sync || openSyncedRealmLocally) {
return { openBehavior: OpenRealmBehaviorType.OpenImmediately };
return { openBehavior: Realm.App.Sync.defaultLocalOpenRealmConfiguration.type };
} else {
const configProperty = realmExists ? "existingRealmFileBehavior" : "newRealmFileBehavior";
const configBehavior = sync[configProperty];
Expand All @@ -52,7 +52,8 @@ function determineBehavior(config: Configuration, realmExists: boolean): OpenBeh
}
return { openBehavior: type, timeOut, timeOutBehavior };
} else {
return { openBehavior: OpenRealmBehaviorType.DownloadBeforeOpen }; // Default is downloadBeforeOpen
const { type, timeOut, timeOutBehavior } = Realm.App.Sync.defaultSyncOpenRealmConfiguration;
return { openBehavior: type, timeOut, timeOutBehavior };
}
}
}
Expand Down
24 changes: 19 additions & 5 deletions packages/realm/src/app-services/Sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,33 @@ export class Sync {
}

/**
* 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.
* The default OpenRealmBehaviorConfiguration used for local realms.
* This is used if the behavior is not specified by the developer when opening the realm.
* @internal
*/
static openLocalRealmBehavior: Readonly<OpenRealmBehaviorConfiguration> = {
static defaultLocalOpenRealmConfiguration: Readonly<OpenRealmBehaviorConfiguration> = {
type: OpenRealmBehaviorType.OpenImmediately,
};

/**
* The default behavior settings if you want to wait for downloading a synchronized Realm to complete before opening it.
* The default OpenRealmBehaviorConfiguration used for synced realms.
* This is used if the behavior is not specified by the developer when opening the realm.
* @internal
*/
static downloadBeforeOpenBehavior: Readonly<OpenRealmBehaviorConfiguration> = {
static defaultSyncOpenRealmConfiguration: Readonly<OpenRealmBehaviorConfiguration> = {
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 = this.defaultLocalOpenRealmConfiguration;

/**
* The default behavior settings if you want to wait for downloading a synchronized Realm to complete before opening it.
*/
static downloadBeforeOpenBehavior = this.defaultSyncOpenRealmConfiguration;
}

0 comments on commit 3e92673

Please sign in to comment.