Skip to content

Make MemoryLru tree-shakeable #2767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions packages/firestore/src/local/memory_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,17 @@ export class MemoryPersistence implements Persistence {

private _started = false;

readonly referenceDelegate: MemoryLruDelegate | MemoryEagerDelegate;

static createLruPersistence(
clientId: ClientId,
params: LruParams
): MemoryPersistence {
const factory = (p: MemoryPersistence): MemoryLruDelegate =>
new MemoryLruDelegate(p, params);
return new MemoryPersistence(clientId, factory);
}

static createEagerPersistence(clientId: ClientId): MemoryPersistence {
const factory = (p: MemoryPersistence): MemoryEagerDelegate =>
new MemoryEagerDelegate(p);
return new MemoryPersistence(clientId, factory);
}

readonly referenceDelegate: MemoryReferenceDelegate;

/**
* The constructor accepts a factory for creating a reference delegate. This
* allows both the delegate and this instance to have strong references to
* each other without having nullable fields that would then need to be
* checked or asserted on every access.
*/
private constructor(
constructor(
private readonly clientId: ClientId,
referenceDelegateFactory: (
p: MemoryPersistence
) => MemoryLruDelegate | MemoryEagerDelegate
referenceDelegateFactory: (p: MemoryPersistence) => MemoryReferenceDelegate
) {
this._started = true;
this.referenceDelegate = referenceDelegateFactory(this);
Expand Down Expand Up @@ -226,7 +209,13 @@ export class MemoryTransaction extends PersistenceTransaction {
}
}

export class MemoryEagerDelegate implements ReferenceDelegate {
export interface MemoryReferenceDelegate extends ReferenceDelegate {
documentSize(doc: MaybeDocument): number;
onTransactionStarted(): void;
onTransactionCommitted(txn: PersistenceTransaction): PersistencePromise<void>;
}

export class MemoryEagerDelegate implements MemoryReferenceDelegate {
private inMemoryPins: ReferenceSet | null = null;
private _orphanedDocuments: Set<DocumentKey> | null = null;

Expand Down Expand Up @@ -545,7 +534,10 @@ export class MemoryPersistenceProvider implements PersistenceProvider {

getPersistence(): Persistence {
assert(!!this.clientId, 'initialize() not called');
return MemoryPersistence.createEagerPersistence(this.clientId);
return new MemoryPersistence(
this.clientId,
p => new MemoryEagerDelegate(p)
);
}

getSharedClientState(): SharedClientState {
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/test/unit/generate_spec_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function main(args) {
var testName = specName.replace(/^specs\//, '');
var filename = testName.replace(/[^A-Za-z\d]/g, '_') + '.json';
var outputFile = outputPath + '/' + filename;
console.log("Generating " + outputFile);
console.log('Generating ' + outputFile);
writeToJSON(testFiles[i], outputFile);
}

Expand Down
13 changes: 10 additions & 3 deletions packages/firestore/test/unit/local/persistence_test_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import {
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
import { LocalSerializer } from '../../../src/local/local_serializer';
import { LruParams } from '../../../src/local/lru_garbage_collector';
import { MemoryPersistence } from '../../../src/local/memory_persistence';
import {
MemoryEagerDelegate,
MemoryLruDelegate,
MemoryPersistence
} from '../../../src/local/memory_persistence';
import {
ClientId,
WebStorageSharedClientState
Expand Down Expand Up @@ -122,13 +126,16 @@ export async function testIndexedDbPersistence(

/** Creates and starts a MemoryPersistence instance for testing. */
export async function testMemoryEagerPersistence(): Promise<MemoryPersistence> {
return MemoryPersistence.createEagerPersistence(AutoId.newId());
return new MemoryPersistence(AutoId.newId(), p => new MemoryEagerDelegate(p));
}

export async function testMemoryLruPersistence(
params: LruParams = LruParams.DEFAULT
): Promise<MemoryPersistence> {
return MemoryPersistence.createLruPersistence(AutoId.newId(), params);
return new MemoryPersistence(
AutoId.newId(),
p => new MemoryLruDelegate(p, params)
);
}

/** Clears the persistence in tests */
Expand Down
21 changes: 13 additions & 8 deletions packages/firestore/test/unit/specs/spec_test_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ import {
} from '../../../src/local/indexeddb_schema';
import { LocalStore } from '../../../src/local/local_store';
import { LruParams } from '../../../src/local/lru_garbage_collector';
import { MemoryPersistence } from '../../../src/local/memory_persistence';
import {
MemoryEagerDelegate,
MemoryLruDelegate,
MemoryPersistence
} from '../../../src/local/memory_persistence';
import { Persistence } from '../../../src/local/persistence';
import {
ClientId,
Expand Down Expand Up @@ -1151,7 +1155,9 @@ abstract class TestRunner {
expect(actualTarget.query).to.deep.equal(expectedTarget.query);
expect(actualTarget.targetId).to.equal(expectedTarget.targetId);
expect(actualTarget.readTime).to.equal(expectedTarget.readTime);
expect(actualTarget.resumeToken || '').to.equal(expectedTarget.resumeToken || '');
expect(actualTarget.resumeToken || '').to.equal(
expectedTarget.resumeToken || ''
);
delete actualTargets[targetId];
});
expect(obj.size(actualTargets)).to.equal(
Expand Down Expand Up @@ -1236,12 +1242,11 @@ class MemoryTestRunner extends TestRunner {
gcEnabled: boolean
): Promise<Persistence> {
return Promise.resolve(
gcEnabled
? MemoryPersistence.createEagerPersistence(this.clientId)
: MemoryPersistence.createLruPersistence(
this.clientId,
LruParams.DEFAULT
)
new MemoryPersistence(this.clientId, p =>
gcEnabled
? new MemoryEagerDelegate(p)
: new MemoryLruDelegate(p, LruParams.DEFAULT)
)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/test/util/test_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class TestPlatform implements Platform {
}

/** Returns true if we are running under Node. */
export function isNode() : boolean {
export function isNode(): boolean {
return (
typeof process !== 'undefined' &&
process.title !== undefined &&
Expand Down