Skip to content

Commit

Permalink
Merge pull request #868 from orbitjs/record-transform-buffer-refactor
Browse files Browse the repository at this point in the history
Extract RecordTransformBuffer interface
  • Loading branch information
dgeb authored Jul 18, 2021
2 parents 2dc44ef + 902ebfd commit b57cfcb
Show file tree
Hide file tree
Showing 10 changed files with 586 additions and 328 deletions.
20 changes: 19 additions & 1 deletion packages/@orbit/indexeddb/src/indexeddb-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
AsyncRecordCacheSettings,
RecordCacheQueryOptions,
RecordCacheTransformOptions,
RecordCacheUpdateDetails
RecordCacheUpdateDetails,
RecordTransformBuffer,
SimpleRecordTransformBuffer
} from '@orbit/record-cache';
import { supportsIndexedDB } from './lib/indexeddb';
import { RequestOptions } from '@orbit/data';
Expand Down Expand Up @@ -564,6 +566,22 @@ export class IndexedDBCache<
// Protected methods
/////////////////////////////////////////////////////////////////////////////

/**
* Override `_getTransformBuffer` on base `AsyncRecordCache` to provide a
* `transformBuffer` if a custom one hasn't been provided via the constructor
* setting.
*/
protected _getTransformBuffer(): RecordTransformBuffer {
if (this._transformBuffer === undefined) {
const { schema, keyMap } = this;
this._transformBuffer = new SimpleRecordTransformBuffer({
schema,
keyMap
});
}
return this._transformBuffer;
}

protected async _getAllRecords(): Promise<InitializedRecord[]> {
if (!this._db) return Promise.reject(DB_NOT_OPEN);

Expand Down
22 changes: 22 additions & 0 deletions packages/@orbit/local-storage/src/local-storage-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
RecordCacheTransformOptions,
RecordCacheUpdateDetails,
RecordRelationshipIdentity,
RecordTransformBuffer,
SimpleRecordTransformBuffer,
SyncRecordCache,
SyncRecordCacheSettings
} from '@orbit/record-cache';
Expand Down Expand Up @@ -261,4 +263,24 @@ export class LocalStorageCache<
processor.upgrade();
}
}

/////////////////////////////////////////////////////////////////////////////
// Protected methods
/////////////////////////////////////////////////////////////////////////////

/**
* Override `_getTransformBuffer` on base `SyncRecordCache` to provide a
* `transformBuffer` if a custom one hasn't been provided via the constructor
* setting.
*/
protected _getTransformBuffer(): RecordTransformBuffer {
if (this._transformBuffer === undefined) {
const { schema, keyMap } = this;
this._transformBuffer = new SimpleRecordTransformBuffer({
schema,
keyMap
});
}
return this._transformBuffer;
}
}
18 changes: 18 additions & 0 deletions packages/@orbit/memory/src/memory-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
RecordCacheTransformOptions,
RecordCacheUpdateDetails,
RecordRelationshipIdentity,
RecordTransformBuffer,
SimpleRecordTransformBuffer,
SyncRecordCache,
SyncRecordCacheSettings
} from '@orbit/record-cache';
Expand Down Expand Up @@ -256,6 +258,22 @@ export class MemoryCache<
// Protected methods
/////////////////////////////////////////////////////////////////////////////

/**
* Override `_getTransformBuffer` on base `SyncRecordCache` to provide a
* `transformBuffer` if a custom one hasn't been provided via the constructor
* setting.
*/
protected _getTransformBuffer(): RecordTransformBuffer {
if (this._transformBuffer === undefined) {
const { schema, keyMap } = this;
this._transformBuffer = new SimpleRecordTransformBuffer({
schema,
keyMap
});
}
return this._transformBuffer;
}

protected _resetInverseRelationships(
base?: MemoryCache<QO, TO, QB, TB, QRD, TRD>
): void {
Expand Down
26 changes: 8 additions & 18 deletions packages/@orbit/record-cache/src/async-record-cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Orbit } from '@orbit/core';
import { Assertion, Orbit } from '@orbit/core';
import {
buildQuery,
buildTransform,
Expand Down Expand Up @@ -60,12 +60,8 @@ import {
RecordCacheSettings,
RecordCacheTransformOptions
} from './record-cache';
import {
RecordTransformBuffer,
RecordTransformBufferClass
} from './record-transform-buffer';
import { RecordTransformBuffer } from './record-transform-buffer';
import { PatchResult, RecordCacheUpdateDetails } from './response';
import { SyncRecordCacheSettings } from './sync-record-cache';

const { assert, deprecate } = Orbit;

Expand All @@ -80,8 +76,7 @@ export interface AsyncRecordCacheSettings<
transformOperators?: Dict<AsyncTransformOperator>;
inverseTransformOperators?: Dict<AsyncInverseTransformOperator>;
debounceLiveQueries?: boolean;
transformBufferClass?: RecordTransformBufferClass;
transformBufferSettings?: SyncRecordCacheSettings<QO, TO>;
transformBuffer?: RecordTransformBuffer;
}

export abstract class AsyncRecordCache<
Expand All @@ -103,8 +98,6 @@ export abstract class AsyncRecordCache<
protected _inverseTransformOperators: Dict<AsyncInverseTransformOperator>;
protected _debounceLiveQueries: boolean;
protected _transformBuffer?: RecordTransformBuffer;
protected _transformBufferClass?: RecordTransformBufferClass;
protected _transformBufferSettings?: SyncRecordCacheSettings<QO, TO>;

constructor(settings: AsyncRecordCacheSettings<QO, TO, QB, TB>) {
super(settings);
Expand All @@ -115,6 +108,7 @@ export abstract class AsyncRecordCache<
this._inverseTransformOperators =
settings.inverseTransformOperators ?? AsyncInverseTransformOperators;
this._debounceLiveQueries = settings.debounceLiveQueries !== false;
this._transformBuffer = settings.transformBuffer;

const processors: AsyncOperationProcessorClass[] = settings.processors
? settings.processors
Expand Down Expand Up @@ -494,14 +488,9 @@ export abstract class AsyncRecordCache<

protected _getTransformBuffer(): RecordTransformBuffer {
if (this._transformBuffer === undefined) {
let transformBufferClass =
this._transformBufferClass ?? RecordTransformBuffer;
let settings = this._transformBufferSettings ?? { schema: this.schema };
settings.schema = settings.schema ?? this.schema;
settings.keyMap = settings.keyMap ?? this.keyMap;
this._transformBuffer = new transformBufferClass(settings);
} else {
this._transformBuffer.reset();
throw new Assertion(
'transformBuffer must be provided to cache via constructor settings'
);
}
return this._transformBuffer;
}
Expand All @@ -520,6 +509,7 @@ export abstract class AsyncRecordCache<
const relatedRecords = inverseRelationships.map((ir) => ir.record);
Array.prototype.push.apply(records, relatedRecords);

buffer.resetState();
buffer.setRecordsSync(await this.getRecordsAsync(records));
buffer.addInverseRelationshipsSync(inverseRelationships);

Expand Down
1 change: 1 addition & 0 deletions packages/@orbit/record-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './response';
export * from './record-accessor';
export * from './record-cache';
export * from './record-transform-buffer';
export * from './simple-record-transform-buffer';

export * from './async-record-cache';
export * from './async-operation-processor';
Expand Down
Loading

0 comments on commit b57cfcb

Please sign in to comment.