Skip to content

Commit

Permalink
Update API + docs + tests + TS defs
Browse files Browse the repository at this point in the history
  • Loading branch information
kneth committed Oct 21, 2022
1 parent ecc06f7 commit faa7532
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 84 deletions.
6 changes: 4 additions & 2 deletions docs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
* This describes the options to configure client reset.
* @typedef {Object} Realm.App.Sync~ClientResetConfiguration
* @property {string} mode - Either "manual" (deprecated, see also `Realm.App.Sync.initiateClientReset()`), "discardUnsyncedChanges" (download a fresh copy from the server), "recoverUnsyncedChanges" (merged remote and local, unsynced changes), or "recoverOrDiscardUnsyncedChanges" (download a fresh copy from the server if recovery of unsynced changes is not possible)
* @property {callback(realm)|null} [clientResetBefore] - called before sync initiates a client reset.
* @property {callback(beforeRealm, afterRealm)|null} [clientResetAfter] - called after client reset has been executed; `beforeRealm` and `afterRealm` are instances of the Realm before and after the client reset.
* @property {callback(realm)|null} [onBefore] - called before sync initiates a client reset (only for "discardUnsyncedChanges", "recoverUnsyncedChanges" or "recoverOrDiscardUnsyncedChanges" modes).
* @property {callback(beforeRealm, afterRealm)|null} [onAfter] - called after client reset has been executed; `beforeRealm` and `afterRealm` are instances of the Realm before and after the client reset (only for "discardUnsyncedChanges", "recoverUnsyncedChanges" or "recoverOrDiscardUnsyncedChanges" modes).
* @property {callback(session, path)|null} [onFallback] - called if recovery or discard fail (only for "recoverUnsyncedChanges" or "recoverOrDiscardUnsyncedChanges" modes).
* @property {callback(session, path)|null} [onManual] - perform manual client reset - see also `Realm.App.Sync.initiateClientReset()` (only "manual" mode).
* @since {10.11.0}
*/

Expand Down
44 changes: 20 additions & 24 deletions integration-tests/tests/src/tests/sync/client-reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { ObjectId, UUID } from "bson";
import { expect } from "chai";
import Realm, { ClientResetDidRecover, ClientResetMode, SessionStopPolicy } from "realm";
import Realm, { ClientResetMode, SessionStopPolicy } from "realm";
import { authenticateUserBefore, importAppBefore } from "../../hooks";
import { DogSchema, IPerson, PersonSchema } from "../../schemas/person-and-dog-with-object-ids";
import { expectClientResetError } from "../../utils/expect-sync-error";
Expand Down Expand Up @@ -68,14 +68,14 @@ async function waitServerSideClientResetDiscardUnsyncedChangesCallbacks(
...(useFlexibleSync ? { flexible: true } : { partitionValue: getPartitionValue() }),
clientReset: {
mode: ClientResetMode.DiscardUnsyncedChanges,
clientResetAfter: (before: Realm, after: Realm) => {
onAfter: (before: Realm, after: Realm) => {
afterCalled = true;
actionAfter(before, after);
if (beforeCalled) {
resetHandle.resolve();
}
},
clientResetBefore: (realm: Realm) => {
onBefore: (realm: Realm) => {
beforeCalled = true;
actionBefore(realm);
if (afterCalled) {
Expand Down Expand Up @@ -103,7 +103,7 @@ async function waitServerSideClientResetRecoveryCallbacks(
app: Realm.App,
user: Realm.User,
actionBefore: (realm: Realm) => void,
actionAfter: (beforeRealm: Realm, afterRealm: Realm, didRecover: ClientResetDidRecover) => void,
actionAfter: (beforeRealm: Realm, afterRealm: Realm) => void,
): Promise<void> {
const resetHandle = createPromiseHandle();
let afterCalled = false;
Expand All @@ -117,14 +117,14 @@ async function waitServerSideClientResetRecoveryCallbacks(
...(useFlexibleSync ? { flexible: true } : { partitionValue: getPartitionValue() }),
clientReset: {
mode: ClientResetMode.RecoverUnsyncedChanges,
clientResetAfter: (before: Realm, after: Realm, didRecover: ClientResetDidRecover) => {
onAfter: (before: Realm, after: Realm) => {
afterCalled = true;
actionAfter(before, after, didRecover);
actionAfter(before, after);
if (beforeCalled) {
resetHandle.resolve();
}
},
clientResetBefore: (realm: Realm) => {
onBefore: (realm: Realm) => {
beforeCalled = true;
actionBefore(realm);
if (afterCalled) {
Expand Down Expand Up @@ -165,14 +165,14 @@ async function waitSimulatedClientResetDiscardUnsyncedChangesCallbacks(
...(useFlexibleSync ? { flexible: true } : { partitionValue: getPartitionValue() }),
clientReset: {
mode: ClientResetMode.DiscardUnsyncedChanges,
clientResetAfter: (before: Realm, after: Realm) => {
onAfter: (before: Realm, after: Realm) => {
afterCalled = true;
actionAfter(before, after);
if (beforeCalled) {
resolve();
}
},
clientResetBefore: (realm: Realm) => {
onBefore: (realm: Realm) => {
beforeCalled = true;
actionBefore(realm);
if (afterCalled) {
Expand Down Expand Up @@ -203,7 +203,7 @@ async function waitSimulatedClientResetRecoverCallbacks(
schema: Realm.ObjectSchema[],
user: Realm.User,
actionBefore: (realm: Realm) => void,
actionAfter: (beforeRealm: Realm, afterRealm: Realm, didRecover: Realm.ClientResetDidRecover) => void,
actionAfter: (beforeRealm: Realm, afterRealm: Realm) => void,
): Promise<void> {
return new Promise((resolve) => {
let afterCalled = false;
Expand All @@ -217,14 +217,14 @@ async function waitSimulatedClientResetRecoverCallbacks(
...(useFlexibleSync ? { flexible: true } : { partitionValue: getPartitionValue() }),
clientReset: {
mode: ClientResetMode.RecoverUnsyncedChanges,
clientResetAfter: (before: Realm, after: Realm, recover: Realm.ClientResetDidRecover) => {
onAfter: (before: Realm, after: Realm) => {
afterCalled = true;
actionAfter(before, after, recover);
actionAfter(before, after);
if (beforeCalled) {
resolve();
}
},
clientResetBefore: (realm: Realm) => {
onBefore: (realm: Realm) => {
beforeCalled = true;
actionBefore(realm);
if (afterCalled) {
Expand Down Expand Up @@ -349,7 +349,7 @@ function getSchema(useFlexibleSync: boolean) {
user: this.user,
clientReset: {
mode: ClientResetMode.Manual,
clientResetAfter: (session, path) => {
onManual: (session, path) => {
expect(session).to.be.not.null;
expect(path).to.not.empty;
resolve();
Expand Down Expand Up @@ -385,7 +385,7 @@ function getSchema(useFlexibleSync: boolean) {
},
clientReset: {
mode: ClientResetMode.Manual,
clientResetAfter: (session, path) => {
onManual: (session, path) => {
expect(session).to.be.not.null;
expect(path).to.not.empty;
resolve();
Expand Down Expand Up @@ -420,10 +420,10 @@ function getSchema(useFlexibleSync: boolean) {
},
clientReset: {
mode: ClientResetMode.DiscardUnsyncedChanges,
clientResetBefore: () => {
onBefore: () => {
reject();
},
clientResetAfter: () => {
onAfter: () => {
reject();
},
},
Expand Down Expand Up @@ -472,9 +472,7 @@ function getSchema(useFlexibleSync: boolean) {
const clientResetBefore = (realm: Realm): void => {
expect(realm.objects(DogSchema.name).length).to.equal(1);
};
const clientResetAfter = (beforeRealm: Realm, afterRealm: Realm, didRecover: ClientResetDidRecover) => {
// FIXME: the assert should be: expect(didRecover).to.be(ClientResetDidRecover.Yes);
expect(didRecover).to.equal("yes");
const clientResetAfter = (beforeRealm: Realm, afterRealm: Realm) => {
expect(beforeRealm.objects(DogSchema.name).length).to.equal(1);
expect(afterRealm.objects(DogSchema.name).length).to.equal(1);
};
Expand Down Expand Up @@ -514,7 +512,7 @@ function getSchema(useFlexibleSync: boolean) {
);
});

it.only(`handles recovery client reset with ${getPartialTestTitle(
it(`handles recovery client reset with ${getPartialTestTitle(
useFlexibleSync,
)} sync enabled`, async function (this: RealmContext) {
// (i) using a client reset in "Recovery" mode, a fresh copy
Expand All @@ -525,9 +523,7 @@ function getSchema(useFlexibleSync: boolean) {
const clientResetBefore = (realm: Realm) => {
expect(realm.objects(DogSchema.name).length).to.equal(1);
};
const clientResetAfter = (beforeRealm: Realm, afterRealm: Realm, didRecover: ClientResetDidRecover) => {
// FIXME: the assert should be: expect(didRecover).to.be(ClientResetDidRecover.Yes);
expect(didRecover).to.equal("yes");
const clientResetAfter = (beforeRealm: Realm, afterRealm: Realm) => {
expect(beforeRealm.objects(DogSchema.name).length).to.equal(1);
expect(afterRealm.objects(DogSchema.name).length).to.equal(1);
};
Expand Down
Loading

0 comments on commit faa7532

Please sign in to comment.