Skip to content

Commit

Permalink
refactor(replicache): merge ReplicacheOptionsInternal into Replicache…
Browse files Browse the repository at this point in the history
…ImplOptions (#1891)
  • Loading branch information
grgbkr authored May 23, 2024
1 parent 6a8a373 commit 61d9d28
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 273 deletions.
7 changes: 2 additions & 5 deletions packages/reflect-client/src/client/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,11 @@ export class Reflect<MD extends MutatorDefs> {
licenseKey: 'reflect-client-static-key',
kvStore,
};
const replicacheInternalOptions = {
const replicacheImplOptions = {
enableLicensing: false,
};

this.#rep = new ReplicacheImpl({
...replicacheOptions,
...replicacheInternalOptions,
});
this.#rep = new ReplicacheImpl(replicacheOptions, replicacheImplOptions);

this.#rep.getAuth = this.#getAuthToken;
this.#onUpdateNeeded = this.#rep.onUpdateNeeded; // defaults to reload.
Expand Down
20 changes: 15 additions & 5 deletions packages/replicache/perf/replicache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
TEST_LICENSE_KEY,
WriteTransaction,
} from '../src/mod.js';
import {ReplicacheImpl as Replicache} from '../src/replicache-impl.js';
import {
ReplicacheImpl as Replicache,
ReplicacheImpl,
} from '../src/replicache-impl.js';
import {uuid} from '../src/uuid.js';
import {
TestDataObject,
Expand All @@ -21,6 +24,7 @@ import {
jsonObjectTestData,
} from './data.js';
import type {Bencher, Benchmark} from './perf.js';
import {restoreMakeImplForTest, setMakeImplForTest} from '../src/replicache.js';

const valSize = 1024;

Expand Down Expand Up @@ -322,14 +326,20 @@ export function benchmarkRebase(opts: {

class ReplicachePerfTest<MD extends MutatorDefs> extends Replicache<MD> {
constructor(options: Omit<ReplicacheOptions<MD>, 'licenseKey'>) {
setMakeImplForTest(
<M extends MutatorDefs>(ops: ReplicacheOptions<M>) =>
new ReplicacheImpl<M>(ops, {
enableLicensing: false,
enableMutationRecovery: false,
enableScheduledRefresh: false,
enableScheduledPersist: false,
}),
);
super({
...options,
licenseKey: TEST_LICENSE_KEY,
enableLicensing: false,
enableMutationRecovery: false,
enableScheduledRefresh: false,
enableScheduledPersist: false,
});
restoreMakeImplForTest();
}
}

Expand Down
10 changes: 7 additions & 3 deletions packages/replicache/src/replicache-collect-idb-databases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ test('collect IDB databases', async () => {
await clock.tickAsync(ONE_WEEK);

// Open one more database and keep it open long enough to trigger the collection.
const rep3 = await replicacheForTesting('collect-idb-databases-3', {
enableLicensing: false,
});
const rep3 = await replicacheForTesting(
'collect-idb-databases-3',
undefined,
{
enableLicensing: false,
},
);
await clock.tickAsync(FIVE_MINUTES);
await rep3.close();

Expand Down
39 changes: 30 additions & 9 deletions packages/replicache/src/replicache-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ import {refresh} from './persist/refresh.js';
import {ProcessScheduler} from './process-scheduler.js';
import type {Puller} from './puller.js';
import {Pusher, PushError} from './pusher.js';
import type {
ReplicacheInternalOptions,
ReplicacheOptions,
} from './replicache-options.js';
import type {ReplicacheOptions} from './replicache-options.js';
import {
getKVStoreProvider,
httpStatusUnauthorized,
Expand Down Expand Up @@ -166,6 +163,32 @@ const defaultMakeSubscriptionsManager: MakeSubscriptionsManager = (
) => new SubscriptionsManagerImpl(queryInternal, lc);

export interface ReplicacheImplOptions {
/**
* Defaults to true.
* Does not use a symbol because it is used by reflect.
*/
enableLicensing?: boolean | undefined;

/**
* Defaults to true.
*/
enableMutationRecovery?: boolean | undefined;

/**
* Defaults to true.
*/
enableScheduledPersist?: boolean | undefined;

/**
* Defaults to true.
*/
enableScheduledRefresh?: boolean | undefined;

/**
* Defaults to true.
*/
enablePullAndPushInOpen?: boolean | undefined;

/**
* Default is `defaultMakeSubscriptionsManager`.
*/
Expand Down Expand Up @@ -378,7 +401,7 @@ export class ReplicacheImpl<MD extends MutatorDefs = {}> {
onRecoverMutations = (r: Promise<boolean>) => r;

constructor(
options: ReplicacheOptions<MD> & ReplicacheInternalOptions,
options: ReplicacheOptions<MD>,
implOptions: ReplicacheImplOptions = {},
) {
const {
Expand All @@ -397,15 +420,13 @@ export class ReplicacheImpl<MD extends MutatorDefs = {}> {
pusher,
licenseKey,
indexes = {},
} = options;
const {
enableMutationRecovery = true,
enableLicensing = true,
enableScheduledPersist = true,

enableScheduledRefresh = true,

enablePullAndPushInOpen = true,
} = options;
const {
makeSubscriptionsManager = defaultMakeSubscriptionsManager,
enableClientGroupForking = true,
} = implOptions;
Expand Down
22 changes: 15 additions & 7 deletions packages/replicache/src/replicache-mutation-recovery-dd31.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,7 @@ suite('DD31', () => {
{
pullURL: 'https://diff.com/pull',
},
undefined,
{
useDefaultURLs: false,
},
Expand All @@ -1528,10 +1529,13 @@ suite('DD31', () => {
});

test('mutation recovery returns early when internal option enableMutationRecovery is false', async () => {
const rep = await replicacheForTesting('mutation-recovery-startup', {
pullURL: 'https://diff.com/pull',
...disableAllBackgroundProcesses,
});
const rep = await replicacheForTesting(
'mutation-recovery-startup',
{
pullURL: 'https://diff.com/pull',
},
disableAllBackgroundProcesses,
);
expect(rep.recoverMutationsFake.callCount).to.equal(1);
expect(await rep.recoverMutationsFake.firstCall.returnValue).to.equal(
false,
Expand Down Expand Up @@ -1574,9 +1578,13 @@ suite('DD31', () => {
});

test('mutation recovery is invoked on 5 minute interval', async () => {
const rep = await replicacheForTesting('mutation-recovery-startup-dd31-4', {
enableLicensing: false,
});
const rep = await replicacheForTesting(
'mutation-recovery-startup-dd31-4',
undefined,
{
enableLicensing: false,
},
);
expect(rep.recoverMutationsFake.callCount).to.equal(1);
await clock.tickAsync(5 * 60 * 1000);
expect(rep.recoverMutationsFake.callCount).to.equal(2);
Expand Down
13 changes: 9 additions & 4 deletions packages/replicache/src/replicache-mutation-recovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ suite('SDD', () => {
{
pullURL: 'https://diff.com/pull',
},
undefined,
{useDefaultURLs: false},
);
expect(rep.recoverMutationsFake.callCount).to.equal(1);
Expand All @@ -1082,8 +1083,8 @@ suite('SDD', () => {
'mutation-recovery-startup',
{
pullURL: 'https://diff.com/pull',
...disableAllBackgroundProcesses,
},
disableAllBackgroundProcesses,
{useDefaultURLs: false},
);
expect(rep.recoverMutationsFake.callCount).to.equal(1);
Expand Down Expand Up @@ -1128,9 +1129,13 @@ suite('SDD', () => {
});

test('mutation recovery is invoked on 5 minute interval', async () => {
const rep = await replicacheForTesting('mutation-recovery-startup', {
enableLicensing: false,
});
const rep = await replicacheForTesting(
'mutation-recovery-startup',
undefined,
{
enableLicensing: false,
},
);
expect(rep.recoverMutationsFake.callCount).to.equal(1);
await clock.tickAsync(5 * 60 * 1000);
expect(rep.recoverMutationsFake.callCount).to.equal(2);
Expand Down
4 changes: 4 additions & 0 deletions packages/replicache/src/replicache-on-update-needed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ suite('onUpdateNeeded', () => {
{
mutators: {test1: () => undefined},
},
undefined,
{useUniqueName: false},
);
let onUpdateNeededReason;
Expand All @@ -26,6 +27,7 @@ suite('onUpdateNeeded', () => {
{
mutators: {test2: () => undefined},
},
undefined,
{useUniqueName: false},
);

Expand All @@ -43,6 +45,7 @@ suite('onUpdateNeeded', () => {
mutators: {test1: () => undefined},
schemaVersion: '1',
},
undefined,
{useUniqueName: false},
);
let onUpdateNeededReason;
Expand All @@ -55,6 +58,7 @@ suite('onUpdateNeeded', () => {
mutators: {test1: () => undefined},
schemaVersion: '2',
},
undefined,
{useUniqueName: false},
);
expect(onUpdateNeededReason).to.deep.equal({
Expand Down
28 changes: 0 additions & 28 deletions packages/replicache/src/replicache-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,31 +217,3 @@ export interface ReplicacheOptions<MD extends MutatorDefs> {
*/
readonly indexes?: IndexDefinitions | undefined;
}

export type ReplicacheInternalOptions = {
/**
* Defaults to true.
* Does not use a symbol because it is used by reflect.
*/
enableLicensing?: boolean | undefined;

/**
* Defaults to true.
*/
enableMutationRecovery?: boolean | undefined;

/**
* Defaults to true.
*/
enableScheduledPersist?: boolean | undefined;

/**
* Defaults to true.
*/
enableScheduledRefresh?: boolean | undefined;

/**
* Defaults to true.
*/
enablePullAndPushInOpen?: boolean | undefined;
};
31 changes: 21 additions & 10 deletions packages/replicache/src/replicache-persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ test('basic persist & load', async () => {
{
pullURL,
},
undefined,
{useUniqueName: false},
);
await rep2.query(async tx => {
Expand Down Expand Up @@ -182,10 +183,13 @@ suite('onClientStateNotFound', () => {
const mutators = {
addData,
};
const rep = await replicacheForTesting(name, {
mutators,
...disableAllBackgroundProcesses,
});
const rep = await replicacheForTesting(
name,
{
mutators,
},
disableAllBackgroundProcesses,
);

await rep.mutate.addData({foo: 'bar'});
await rep.persist();
Expand All @@ -202,10 +206,12 @@ suite('onClientStateNotFound', () => {
rep.name,
{
mutators,
...disableAllBackgroundProcesses,
},
{
// To ensure query has to go to perdag, prevent pull from happening and
// populating the lazy store cache.
enablePullAndPushInOpen: false,
...disableAllBackgroundProcesses,
},
// Use same idb and client group as above rep.
{useUniqueName: false},
Expand Down Expand Up @@ -249,10 +255,13 @@ suite('onClientStateNotFound', () => {
},
};

const rep = await replicacheForTesting(name, {
mutators,
...disableAllBackgroundProcesses,
});
const rep = await replicacheForTesting(
name,
{
mutators,
},
disableAllBackgroundProcesses,
);

await rep.mutate.addData({foo: 'bar'});
await rep.persist();
Expand All @@ -264,6 +273,8 @@ suite('onClientStateNotFound', () => {
rep.name,
{
mutators,
},
{
...disableAllBackgroundProcesses,
// To ensure mutate has to go to perdag, prevent pull from happening and
// populating the lazy store cache.
Expand Down Expand Up @@ -306,8 +317,8 @@ test('Persist throws if idb dropped', async () => {
'called-in-persist-dropped',
{
mutators: {addData},
...disableAllBackgroundProcesses,
},
disableAllBackgroundProcesses,
{useUniqueName: false},
);

Expand Down
Loading

0 comments on commit 61d9d28

Please sign in to comment.