Skip to content

Commit

Permalink
more test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Nov 29, 2022
1 parent 7c69608 commit b5d316e
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 101 deletions.
8 changes: 4 additions & 4 deletions ember-data-types/q/record-data-store-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { SchemaDefinitionService } from './schema-definition-service';
*/

/**
* RecordDataStoreWrapper provides encapsulated API access to the minimal
* subset of the Store's functionality that cache (RecordData) implementations
* CacheStoreWrapper provides encapsulated API access to the minimal
* subset of the Store's functionality that cache implementations
* should interact with. It is provided to the Store's `createRecordDataFor`
* hook.
* and `createCache` hooks.
*
* Cache implementations should not need more than this API provides.
*
* This class cannot be directly instantiated.
*
* @class RecordDataStoreWrapper
* @class CacheStoreWrapper
* @public
*/
export interface LegacyRecordDataStoreWrapper {
Expand Down
2 changes: 1 addition & 1 deletion packages/-ember-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ not wish to use `ember-data`, remove `ember-data` from your project's `package.j
EmberData is organized into primitives that compose together via public APIs.

- [@ember-data/store](https://github.com/emberjs/data/tree/master/packages/store) is the core and handles coordination
- [@ember-data/json-api](https://github.com/emberjs/data/tree/master/packages/json-api) is a resource cache for JSON:API structured data. It integrates with the store via the hook `createRecordDataFor`
- [@ember-data/json-api](https://github.com/emberjs/data/tree/master/packages/json-api) provides a resource cache for JSON:API structured data. It integrates with the store via the hook `createCache`
- [@ember-data/model](https://github.com/emberjs/data/tree/master/packages/model) is a presentation layer, it integrates with the store via the hooks `instantiateRecord` and `teardownRecord`.
- [@ember-data/adapter](https://github.com/emberjs/data/tree/master/packages/adapter) provides various network API integrations for APIS built over specific REST or JSON:API conventions.
- [@ember-data/serializer](https://github.com/emberjs/data/tree/master/packages/serializer) pairs with `@ember-data/adapter` to normalize and serialize data to and from an API format into the `JSON:API` format understood by `@ember-data/json-api`.
Expand Down
2 changes: 1 addition & 1 deletion packages/-ember-data/addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ not wish to use `ember-data`, remove `ember-data` from your project's `package.j
*Ember*‍**Data** is organized into primitives that compose together via public APIs.
- [@ember-data/store](/ember-data/release/modules/@ember-data%2Fstore) is the core and handles coordination
- [@ember-data/json-api](/ember-data/release/modules/@ember-data%2Frecord-data) is a resource cache for JSON:API structured data. It integrates with the store via the hook `createRecordDataFor`
- [@ember-data/json-api](/ember-data/release/modules/@ember-data%2Frecord-data) provides a resource cache for JSON:API structured data. It integrates with the store via the hook `createCache`
- [@ember-data/model](/ember-data/release/modules/@ember-data%2Fmodel) is a presentation layer, it integrates with the store via the hooks `instantiateRecord` and `teardownRecord`.
- [@ember-data/adapter](/ember-data/release/modules/@ember-data%2Fadapter) provides various network API integrations for APIS built over specific REST or JSON:API conventions.
- [@ember-data/serializer](/ember-data/release/modules/@ember-data%2Fserializer) pairs with `@ember-data/adapter` to normalize and serialize data to and from an API format into the `JSON:API` format understood by `@ember-data/json-api`.
Expand Down
64 changes: 35 additions & 29 deletions packages/json-api/addon/-private/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const EMPTY_ITERATOR = {
/**
The default cache implementation used by ember-data. The cache
is configurable and using a different implementation can be
achieved by implementing the store's createRecordDataFor hook.
achieved by implementing the store's createCache hook.
@class RecordDataDefault
@public
Expand Down Expand Up @@ -78,23 +78,28 @@ export default class Cache implements CacheInterface {
}

/**
* Private method used when the store's `createRecordDataFor` hook is called
* to populate an entry for the identifier into the singleton.
* Private method used to populate an entry for the identifier
* into the singleton.
*
* @method createCache
* @private
* @internal
* @param identifier
*/
createCache(identifier: StableRecordIdentifier): void {
_createCache(identifier: StableRecordIdentifier): void {
assert(`Expected no resource data to yet exist in the cache`, !this.__cache.has(identifier));
this.__cache.set(identifier, makeCache());
}

__peek(identifier: StableRecordIdentifier, allowDestroyed = false): CachedResource {
__safePeek(identifier: StableRecordIdentifier, allowDestroyed: boolean): CachedResource | undefined {
let resource = this.__cache.get(identifier);
if (!resource && allowDestroyed) {
resource = this.__destroyedCache.get(identifier);
}
return resource;
}

__peek(identifier: StableRecordIdentifier, allowDestroyed: boolean): CachedResource {
let resource = this.__safePeek(identifier, allowDestroyed);
assert(
`Expected RecordData Cache to have a resource cache for the identifier ${String(identifier)} but none was found`,
resource
Expand All @@ -109,9 +114,9 @@ export default class Cache implements CacheInterface {
): void | string[] {
let changedKeys: string[] | undefined;
if (!this.__cache.has(identifier)) {
this.createCache(identifier);
this._createCache(identifier);
}
const cached = this.__peek(identifier);
const cached = this.__peek(identifier, false);

if (LOG_OPERATIONS) {
try {
Expand Down Expand Up @@ -197,8 +202,8 @@ export default class Cache implements CacheInterface {
console.log(`EmberData | Mutation - clientDidCreate ${identifier.lid}`, options);
}
}
this.createCache(identifier);
const cached = this.__peek(identifier);
this._createCache(identifier);
const cached = this.__peek(identifier, false);
cached.isNew = true;
let createOptions = {};

Expand Down Expand Up @@ -258,12 +263,12 @@ export default class Cache implements CacheInterface {
return createOptions;
}
willCommit(identifier: StableRecordIdentifier): void {
const cached = this.__peek(identifier);
const cached = this.__peek(identifier, false);
cached.inflightAttrs = cached.localAttrs;
cached.localAttrs = null;
}
didCommit(identifier: StableRecordIdentifier, data: JsonApiResource | null): void {
const cached = this.__peek(identifier);
const cached = this.__peek(identifier, false);
if (cached.isDeleted) {
graphFor(this.__storeWrapper).push({
op: 'deleteRecord',
Expand Down Expand Up @@ -305,7 +310,7 @@ export default class Cache implements CacheInterface {
}

commitWasRejected(identifier: StableRecordIdentifier, errors?: JsonApiValidationError[] | undefined): void {
const cached = this.__peek(identifier);
const cached = this.__peek(identifier, false);
if (cached.inflightAttrs) {
let keys = Object.keys(cached.inflightAttrs);
if (keys.length > 0) {
Expand All @@ -325,7 +330,14 @@ export default class Cache implements CacheInterface {
}

unloadRecord(identifier: StableRecordIdentifier): void {
const cached = this.__peek(identifier);
// TODO this is necessary because
// we maintain memebership inside InstanceCache
// for peekAll, so even though we haven't created
// any data we think this exists.
if (!this.__cache.has(identifier)) {
return;
}
const cached = this.__peek(identifier, false);
const storeWrapper = this.__storeWrapper;
graphFor(storeWrapper).unload(identifier);

Expand Down Expand Up @@ -382,7 +394,7 @@ export default class Cache implements CacheInterface {
}
}
setAttr(identifier: StableRecordIdentifier, attr: string, value: unknown): void {
const cached = this.__peek(identifier);
const cached = this.__peek(identifier, false);
const existing =
cached.inflightAttrs && attr in cached.inflightAttrs
? cached.inflightAttrs[attr]
Expand All @@ -403,14 +415,14 @@ export default class Cache implements CacheInterface {
}
changedAttrs(identifier: StableRecordIdentifier): ChangedAttributesHash {
// TODO freeze in dev
return (this.__peek(identifier).changes || Object.create(null)) as ChangedAttributesHash;
return (this.__peek(identifier, false).changes || Object.create(null)) as ChangedAttributesHash;
}
hasChangedAttrs(identifier: StableRecordIdentifier): boolean {
const cached = this.__peek(identifier, true);
return cached.localAttrs !== null && Object.keys(cached.localAttrs).length > 0;
}
rollbackAttrs(identifier: StableRecordIdentifier): string[] {
const cached = this.__peek(identifier);
const cached = this.__peek(identifier, false);
let dirtyKeys: string[] | undefined;
cached.isDeleted = false;

Expand Down Expand Up @@ -454,7 +466,7 @@ export default class Cache implements CacheInterface {
}

setIsDeleted(identifier: StableRecordIdentifier, isDeleted: boolean): void {
const cached = this.__peek(identifier);
const cached = this.__peek(identifier, false);
cached.isDeleted = isDeleted;
if (cached.isNew) {
// TODO can we delete this since we will do this in unload?
Expand All @@ -470,23 +482,17 @@ export default class Cache implements CacheInterface {
return this.__peek(identifier, true).errors || [];
}
isEmpty(identifier: StableRecordIdentifier): boolean {
if (!this.__cache.has(identifier)) {
return true;
}
const cached = this.__peek(identifier, true);
return cached.remoteAttrs === null && cached.inflightAttrs === null && cached.localAttrs === null;
const cached = this.__safePeek(identifier, true);
return cached ? cached.remoteAttrs === null && cached.inflightAttrs === null && cached.localAttrs === null : true;
}
isNew(identifier: StableRecordIdentifier): boolean {
if (!this.__cache.has(identifier)) {
return false;
}
return this.__peek(identifier, true).isNew;
return this.__safePeek(identifier, true)?.isNew || false;
}
isDeleted(identifier: StableRecordIdentifier): boolean {
return this.__peek(identifier, true).isDeleted;
return this.__safePeek(identifier, true)?.isDeleted || false;
}
isDeletionCommitted(identifier: StableRecordIdentifier): boolean {
return this.__peek(identifier, true).isDeletionCommitted;
return this.__safePeek(identifier, true)?.isDeletionCommitted || false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,11 @@ class LegacyWrapper implements LegacyRecordDataStoreWrapper {
? this._store._instanceCache.getRecordData(identifier)
: this._store.cache;

if (!id && !lid) {
cache.clientDidCreate(identifier);
this._store.recordArrayManager.identifierAdded(identifier);
if (DEPRECATE_V1CACHE_STORE_APIS) {
if (!id && !lid && typeof type === 'string') {
cache.clientDidCreate(identifier);
this._store.recordArrayManager.identifierAdded(identifier);
}
}

return cache;
Expand Down
8 changes: 6 additions & 2 deletions packages/store/addon/-private/store-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ export interface CreateRecordProperties {
@extends Ember.Service
*/

interface Store {
createRecordDataFor?(identifier: StableRecordIdentifier, wrapper: CacheStoreWrapper): Cache;
}

class Store extends Service {
declare recordArrayManager: RecordArrayManager;

Expand Down Expand Up @@ -2406,6 +2410,7 @@ class Store extends Service {
* @method createCache (hook)
* @public
* @param storeWrapper
* @returns {Cache}
*/
createCache(storeWrapper: CacheStoreWrapper): Cache {
if (HAS_JSON_API_PACKAGE) {
Expand Down Expand Up @@ -2443,9 +2448,8 @@ class Store extends Service {
* @public
* @param identifier
* @param storeWrapper
* @returns {RecordData}
* @returns {Cache}
*/
declare createRecordDataFor: (identifier: StableRecordIdentifier, wrapper: CacheStoreWrapper) => Cache;

/**
`normalize` converts a json payload into the normalized form that
Expand Down
1 change: 1 addition & 0 deletions tests/docs/fixtures/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ module.exports = {
'(public) @ember-data/store Store#unloadAll',
'(public) @ember-data/store Store#unloadRecord',
'(public) @ember-data/store Store#saveRecord',
'(public) @ember-data/store Store#createCache (hook)',
'(public) @ember-data/store Store#createRecordDataFor (hook)',
'(public) @ember-data/store Store#instantiateRecord (hook)',
'(public) @ember-data/store Store#teardownRecord (hook)',
Expand Down
8 changes: 4 additions & 4 deletions tests/main/tests/integration/record-data/record-data-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ module('integration/record-data - Custom RecordData Implementations', function (
if (identifier.type === 'house') {
return new RelationshipRecordData(storeWrapper, identifier);
} else {
return this._super(identifier, storeWrapper);
return this.cache;
}
},
});
Expand Down Expand Up @@ -638,7 +638,7 @@ module('integration/record-data - Custom RecordData Implementations', function (
if (identifier.type === 'house') {
return new RelationshipRecordData(storeWrapper, identifier);
} else {
return this._super(identifier, storeWrapper);
return this.cache;
}
},
});
Expand Down Expand Up @@ -732,7 +732,7 @@ module('integration/record-data - Custom RecordData Implementations', function (
houseIdentifier = identifier;
return new RelationshipRecordData(storeWrapper, identifier);
} else {
return this._super(identifier, storeWrapper);
return this.cache;
}
},
});
Expand Down Expand Up @@ -874,7 +874,7 @@ module('integration/record-data - Custom RecordData Implementations', function (
if (identifier.type === 'house') {
return new RelationshipRecordData(storeWrapper, identifier);
} else {
return this._super(identifier, storeWrapper);
return this.cache;
}
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ module('integration/store-wrapper - RecordData StoreWrapper tests', function (ho
if (identifier.type === 'house') {
return new RecordDataForTest(identifier, wrapper);
} else {
return this._super(identifier, wrapper);
return this.cache;
}
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ module('RecordData Compatibility', function (hooks) {
const CustomRecordData = DEPRECATE_V1_RECORD_DATA ? V1CustomRecordData : V2CustomRecordData;

test(`store.unloadRecord on a record with default RecordData with relationship to a record with custom RecordData does not error`, async function (assert) {
const originalCreateRecordDataFor = store.createRecordDataFor;
let customCalled = 0,
customCalledFor = [],
originalCalled = 0,
Expand All @@ -186,7 +185,7 @@ module('RecordData Compatibility', function (hooks) {
} else {
originalCalled++;
originalCalledFor.push(identifier);
return originalCreateRecordDataFor.call(this, identifier, storeWrapper);
return this.cache;
}
};

Expand Down
Loading

0 comments on commit b5d316e

Please sign in to comment.