Skip to content
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

Allow record-cache-based sources to define a custom cacheClass #827

Merged
merged 1 commit into from Mar 15, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/@orbit/indexeddb/src/indexeddb-cache.ts
Expand Up @@ -30,6 +30,10 @@ export interface IndexedDBCacheSettings extends AsyncRecordCacheSettings {
namespace?: string;
}

export interface IndexedDBCacheClass {
new (settings: IndexedDBCacheSettings): IndexedDBCache;
}

/**
* A cache used to access records in an IndexedDB database.
*
Expand Down
13 changes: 10 additions & 3 deletions packages/@orbit/indexeddb/src/indexeddb-source.ts
Expand Up @@ -30,13 +30,18 @@ import {
RecordUpdatable,
UpdateRecordOperation
} from '@orbit/records';
import { IndexedDBCache, IndexedDBCacheSettings } from './indexeddb-cache';
import {
IndexedDBCache,
IndexedDBCacheClass,
IndexedDBCacheSettings
} from './indexeddb-cache';
import { supportsIndexedDB } from './lib/indexeddb';

const { assert } = Orbit;

export interface IndexedDBSourceSettings extends RecordSourceSettings {
namespace?: string;
cacheClass?: IndexedDBCacheClass;
cacheSettings?: Partial<IndexedDBCacheSettings>;
}

Expand Down Expand Up @@ -73,7 +78,7 @@ export class IndexedDBSource extends RecordSource {

super(settings);

let cacheSettings: Partial<IndexedDBCacheSettings> =
const cacheSettings: Partial<IndexedDBCacheSettings> =
settings.cacheSettings ?? {};
cacheSettings.schema = settings.schema;
cacheSettings.keyMap = settings.keyMap;
Expand All @@ -87,7 +92,9 @@ export class IndexedDBSource extends RecordSource {
cacheSettings.defaultTransformOptions =
cacheSettings.defaultTransformOptions ?? settings.defaultTransformOptions;

this._cache = new IndexedDBCache(cacheSettings as IndexedDBCacheSettings);
const cacheClass = settings.cacheClass ?? IndexedDBCache;
this._cache = new cacheClass(cacheSettings as IndexedDBCacheSettings);

if (autoActivate) {
this.activate();
}
Expand Down
17 changes: 17 additions & 0 deletions packages/@orbit/indexeddb/test/indexeddb-source-test.ts
Expand Up @@ -5,6 +5,7 @@ import {
RecordKeyMap,
RecordSchema
} from '@orbit/records';
import { IndexedDBCache } from '../src';
import { IndexedDBSource } from '../src/indexeddb-source';
import { getRecordFromIndexedDB } from './support/indexeddb';

Expand Down Expand Up @@ -76,6 +77,22 @@ module('IndexedDBSource', function (hooks) {
);
});

test('can be assigned a custom `cacheClass`', function (assert) {
class CustomCache extends IndexedDBCache {
custom = true;
}

source = new IndexedDBSource({
schema,
autoActivate: false,
cacheClass: CustomCache
});
assert.ok(
(source.cache as CustomCache).custom,
'custom cacheClass has been instantiated'
);
});

test('shares its `transformBuilder` and `queryBuilder` with its cache', function (assert) {
source = new IndexedDBSource({ schema, keyMap, autoActivate: false });
assert.strictEqual(
Expand Down
4 changes: 4 additions & 0 deletions packages/@orbit/local-storage/src/local-storage-cache.ts
Expand Up @@ -12,6 +12,10 @@ export interface LocalStorageCacheSettings extends SyncRecordCacheSettings {
namespace?: string;
}

export interface LocalStorageCacheClass {
new (settings: LocalStorageCacheSettings): LocalStorageCache;
}

/**
* A cache used to access records in local storage.
*
Expand Down
7 changes: 4 additions & 3 deletions packages/@orbit/local-storage/src/local-storage-source.ts
Expand Up @@ -35,6 +35,7 @@ import {
import { supportsLocalStorage } from './lib/local-storage';
import {
LocalStorageCache,
LocalStorageCacheClass,
LocalStorageCacheSettings
} from './local-storage-cache';

Expand All @@ -43,6 +44,7 @@ const { assert } = Orbit;
export interface LocalStorageSourceSettings extends RecordSourceSettings {
delimiter?: string;
namespace?: string;
cacheClass?: LocalStorageCacheClass;
cacheSettings?: Partial<LocalStorageCacheSettings>;
}

Expand Down Expand Up @@ -95,9 +97,8 @@ export class LocalStorageSource extends RecordSource {
cacheSettings.namespace = cacheSettings.namespace ?? settings.namespace;
cacheSettings.delimiter = cacheSettings.delimiter ?? settings.delimiter;

this._cache = new LocalStorageCache(
cacheSettings as LocalStorageCacheSettings
);
const cacheClass = settings.cacheClass ?? LocalStorageCache;
this._cache = new cacheClass(cacheSettings as LocalStorageCacheSettings);
}

get cache(): LocalStorageCache {
Expand Down
21 changes: 18 additions & 3 deletions packages/@orbit/local-storage/test/local-storage-source-test.ts
Expand Up @@ -7,13 +7,12 @@ import { buildTransform } from '@orbit/data';
import {
AddRecordOperation,
Record,
RecordIdentity,
RecordSchema,
RecordSource,
RecordKeyMap,
RecordTransform
RecordKeyMap
} from '@orbit/records';
import { LocalStorageSource } from '../src/local-storage-source';
import { LocalStorageCache } from '../src/local-storage-cache';

const { module, test } = QUnit;

Expand Down Expand Up @@ -70,6 +69,22 @@ module('LocalStorageSource', function (hooks) {
assert.equal(source.delimiter, '/', 'delimiter is `/` by default');
});

test('can be assigned a custom `cacheClass`', function (assert) {
class CustomCache extends LocalStorageCache {
custom = true;
}

source = new LocalStorageSource({
schema,
autoActivate: false,
cacheClass: CustomCache
});
assert.ok(
(source.cache as CustomCache).custom,
'custom cacheClass has been instantiated'
);
});

test('shares its `transformBuilder` and `queryBuilder` with its cache', function (assert) {
assert.strictEqual(
source.cache.transformBuilder,
Expand Down
4 changes: 4 additions & 0 deletions packages/@orbit/memory/src/memory-cache.ts
Expand Up @@ -11,6 +11,10 @@ export interface MemoryCacheSettings extends SyncRecordCacheSettings {
base?: MemoryCache;
}

export interface MemoryCacheClass {
new (settings: MemoryCacheSettings): MemoryCache;
}

/**
* A cache used to access records in memory.
*
Expand Down
10 changes: 8 additions & 2 deletions packages/@orbit/memory/src/memory-source.ts
Expand Up @@ -26,10 +26,15 @@ import {
} from '@orbit/data';
import { ResponseHints } from '@orbit/data';
import { Dict } from '@orbit/utils';
import { MemoryCache, MemoryCacheSettings } from './memory-cache';
import {
MemoryCache,
MemoryCacheClass,
MemoryCacheSettings
} from './memory-cache';

export interface MemorySourceSettings extends RecordSourceSettings {
base?: MemorySource;
cacheClass?: MemoryCacheClass;
cacheSettings?: Partial<MemoryCacheSettings>;
}

Expand Down Expand Up @@ -87,7 +92,8 @@ export class MemorySource extends RecordSource {
cacheSettings.base = this._base.cache;
}

this._cache = new MemoryCache(cacheSettings as MemoryCacheSettings);
const cacheClass = settings.cacheClass ?? MemoryCache;
this._cache = new cacheClass(cacheSettings as MemoryCacheSettings);
}

get cache(): MemoryCache {
Expand Down
17 changes: 17 additions & 0 deletions packages/@orbit/memory/test/memory-source-test.ts
Expand Up @@ -14,6 +14,7 @@ import {
RecordTransformBuilder
} from '@orbit/records';
import { clone } from '@orbit/utils';
import { MemoryCache } from '../src/memory-cache';
import { MemorySource } from '../src/memory-source';

const { module, test } = QUnit;
Expand Down Expand Up @@ -100,6 +101,22 @@ module('MemorySource', function (hooks) {
assert.equal(source.cache.processors.length, 2, 'cache has 2 processors');
});

test('can be assigned a custom `cacheClass`', function (assert) {
class CustomCache extends MemoryCache {
custom = true;
}

const source = new MemorySource({
schema,
autoActivate: false,
cacheClass: CustomCache
});
assert.ok(
(source.cache as CustomCache).custom,
'custom cacheClass has been instantiated'
);
});

test('shares its `transformBuilder` and `queryBuilder` with its cache', function (assert) {
const source = new MemorySource({ schema, keyMap });
assert.strictEqual(
Expand Down