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

feat: deprecate v1 cache #8129

Merged
merged 14 commits into from
Aug 29, 2022
2 changes: 2 additions & 0 deletions @types/ember-data-qunit-asserts/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare global {
}

interface Assert {
expectDeprecation(options: { id: string; count: number; until?: string }): void;
expectDeprecation(callback: () => unknown, options: DeprecationConfig | string | RegExp): Promise<void>;
expectNoDeprecation(callback: () => unknown): Promise<void>;
expectWarning(callback: () => unknown, options: WarningConfig | string | RegExp): Promise<void>;
Expand All @@ -25,6 +26,7 @@ declare global {

namespace QUnit {
export interface Assert {
expectDeprecation(options: { id: string; count: number; until?: string }): void;
expectDeprecation(callback: () => unknown, options: DeprecationConfig | string | RegExp): Promise<void>;
expectNoDeprecation(callback: () => unknown): Promise<void>;
expectWarning(callback: () => unknown, options: WarningConfig | string | RegExp): Promise<void>;
Expand Down
13 changes: 1 addition & 12 deletions ember-data-types/q/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ export interface RecordData {

unloadRecord(identifier: StableRecordIdentifier): void;

update(operation: LocalRelationshipOperation): void;

// Attrs
// =====

Expand All @@ -92,16 +90,7 @@ export interface RecordData {
identifier: StableRecordIdentifier,
propertyName: string
): SingleResourceRelationship | CollectionResourceRelationship;

setBelongsTo(identifier: StableRecordIdentifier, propertyName: string, value: StableRecordIdentifier | null): void;
setHasMany(identifier: StableRecordIdentifier, propertyName: string, value: StableRecordIdentifier[]): void;
addToHasMany(
identifier: StableRecordIdentifier,
propertyName: string,
value: StableRecordIdentifier[],
idx?: number
): void;
removeFromHasMany(identifier: StableRecordIdentifier, propertyName: string, value: StableRecordIdentifier[]): void;
update(operation: LocalRelationshipOperation): void;

// State
// =============
Expand Down
6 changes: 1 addition & 5 deletions packages/-ember-data/node-tests/fixtures/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ module.exports = {
'(private) @ember-data/model Model#create',
'(private) @ember-data/model Model#currentState',
'(private) @ember-data/record-data RecordDataDefault#createCache',
'(private) @ember-data/record-data RecordDataDefault#_initRecordCreateOptions',
'(private) @ember-data/record-data RecordDataDefault#changedAttributes',
'(private) @ember-data/record-data RecordDataDefault#updateChangedAttributes',
'(private) @ember-data/serializer/json JSONSerializer#_canSerialize',
'(private) @ember-data/serializer/json JSONSerializer#_getMappedKey',
'(private) @ember-data/serializer/json JSONSerializer#_mustSerialize',
Expand Down Expand Up @@ -337,13 +334,12 @@ module.exports = {
"(public) @ember-data/store RecordDataManager#rollbackAttributes",
"(public) @ember-data/store RecordDataManager#rollbackAttrs",
"(public) @ember-data/store RecordDataManager#setAttr",
"(public) @ember-data/store RecordDataManager#setBelongsTo",
"(public) @ember-data/store RecordDataManager#setDirtyAttribute",
"(public) @ember-data/store RecordDataManager#setDirtyBelongsTo",
"(public) @ember-data/store RecordDataManager#setDirtyHasMany",
"(public) @ember-data/store RecordDataManager#setHasMany",
"(public) @ember-data/store RecordDataManager#setIsDeleted",
"(public) @ember-data/store RecordDataManager#unloadRecord",
"(public) @ember-data/store RecordDataManager#update",
"(public) @ember-data/store RecordDataManager#willCommit",
'(public) @ember-data/store RecordReference#id',
'(public) @ember-data/store RecordReference#identifier',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,96 +102,6 @@ module('integration/record_array_manager', function (hooks) {
assert.true(adapterPopulated.isDestroyed, 'adapterPopulated.willDestroy called once');
});

if (!gte('4.0.0')) {
test('batch liveRecordArray changes', async function (assert) {
let cars = store.peekAll('car');
let arrayContentWillChangeCount = 0;

cars.arrayContentWillChange = function (startIndex, removeCount, addedCount) {
arrayContentWillChangeCount++;
assert.strictEqual(startIndex, 0, 'expected 0 startIndex');
assert.strictEqual(removeCount, 0, 'expected 0 removed');
assert.strictEqual(addedCount, 2, 'expected 2 added');
};

assert.deepEqual(cars.slice(), []);
assert.strictEqual(arrayContentWillChangeCount, 0, 'expected NO arrayChangeEvents yet');

store.push({
data: [
{
type: 'car',
id: '1',
attributes: {
make: 'BMC',
model: 'Mini Cooper',
},
},
{
type: 'car',
id: '2',
attributes: {
make: 'Jeep',
model: 'Wrangler',
},
},
],
});
await settled();

assert.strictEqual(arrayContentWillChangeCount, 1, 'expected ONE array change event');

assert.deepEqual(cars.slice(), [store.peekRecord('car', 1), store.peekRecord('car', 2)]);

store.peekRecord('car', 1).set('model', 'Mini');

assert.strictEqual(arrayContentWillChangeCount, 1, 'expected ONE array change event');

cars.arrayContentWillChange = function (startIndex, removeCount, addedCount) {
arrayContentWillChangeCount++;
assert.strictEqual(startIndex, 2, 'expected a start index of TWO');
assert.strictEqual(removeCount, 0, 'expected no removes');
assert.strictEqual(addedCount, 1, 'expected ONE add');
};

arrayContentWillChangeCount = 0;

store.push({
data: [
{
type: 'car',
id: '2', // this ID is already present, array wont need to change
attributes: {
make: 'Tesla',
model: 'S',
},
},
],
});
await settled();

assert.strictEqual(arrayContentWillChangeCount, 0, 'expected NO array change events');

store.push({
data: [
{
type: 'car',
id: '3',
attributes: {
make: 'Tesla',
model: 'S',
},
},
],
});
await settled();

assert.strictEqual(arrayContentWillChangeCount, 1, 'expected ONE array change event');
// reset function so it doesn't execute after test finishes and store is torn down
cars.arrayContentWillChange = function () {};
});
}

test('#GH-4041 store#query AdapterPopulatedRecordArrays are removed from their managers instead of retained when #destroy is called', async function (assert) {
store.push({
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Promise } from 'rsvp';
import { setupTest } from 'ember-qunit';

import { InvalidError } from '@ember-data/adapter/error';
import { V2CACHE_SINGLETON_MANAGER } from '@ember-data/canary-features';
import Model, { attr } from '@ember-data/model';
import { DEPRECATE_V1_RECORD_DATA } from '@ember-data/private-build-infra/deprecations';
import { LocalRelationshipOperation } from '@ember-data/record-data/-private/graph/-operations';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import Store, { recordIdentifierFor } from '@ember-data/store';
Expand All @@ -19,7 +19,7 @@ import type { JsonApiResource, JsonApiValidationError } from '@ember-data/types/
import type { RecordDataStoreWrapper } from '@ember-data/types/q/record-data-store-wrapper';
import { Dict } from '@ember-data/types/q/utils';

if (V2CACHE_SINGLETON_MANAGER) {
if (!DEPRECATE_V1_RECORD_DATA) {
class Person extends Model {
@attr declare firstName: string;
@attr declare lastName: string;
Expand Down Expand Up @@ -70,12 +70,6 @@ if (V2CACHE_SINGLETON_MANAGER) {
): SingleResourceRelationship | CollectionResourceRelationship {
throw new Error('Method not implemented.');
}
setBelongsTo(identifier: StableRecordIdentifier, propertyName: string, value: StableRecordIdentifier | null): void {
throw new Error('Method not implemented.');
}
setHasMany(identifier: StableRecordIdentifier, propertyName: string, value: StableRecordIdentifier[]): void {
throw new Error('Method not implemented.');
}
addToHasMany(
identifier: StableRecordIdentifier,
propertyName: string,
Expand Down Expand Up @@ -420,7 +414,7 @@ if (V2CACHE_SINGLETON_MANAGER) {
});

test('Record Data invalid errors', async function (assert) {
assert.expect(2);
assert.expect(3);

const personHash = {
type: 'person',
Expand Down Expand Up @@ -474,14 +468,15 @@ if (V2CACHE_SINGLETON_MANAGER) {
data: [personHash],
});
let person = store.peekRecord('person', '1');
person.save().then(
await person.save().then(
() => {},
(err) => {}
);
assert.expectDeprecation({ id: 'ember-data:deprecate-v1-cache', count: 1 });
});

test('Record Data adapter errors', async function (assert) {
assert.expect(1);
assert.expect(2);
const personHash = {
type: 'person',
id: '1',
Expand Down Expand Up @@ -523,10 +518,11 @@ if (V2CACHE_SINGLETON_MANAGER) {
() => {},
(err) => {}
);
assert.expectDeprecation({ id: 'ember-data:deprecate-v1-cache', count: 1 });
});

test('Getting errors from Record Data shows up on the record', async function (assert) {
assert.expect(7);
assert.expect(8);
let storeWrapper;
const personHash = {
type: 'person',
Expand Down Expand Up @@ -593,6 +589,7 @@ if (V2CACHE_SINGLETON_MANAGER) {
assert.strictEqual(person.errors.errorsFor('name').length, 0, 'no errors on name');
let lastNameError = person.errors.errorsFor('lastName').at(0);
assert.strictEqual(lastNameError.attribute, 'lastName', 'error shows up on lastName');
assert.expectDeprecation({ id: 'ember-data:deprecate-v1-cache', count: 1 });
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Promise } from 'rsvp';

import { setupTest } from 'ember-qunit';

import { V2CACHE_SINGLETON_MANAGER } from '@ember-data/canary-features';
import Model, { attr } from '@ember-data/model';
import { DEPRECATE_V1_RECORD_DATA } from '@ember-data/private-build-infra/deprecations';
import { LocalRelationshipOperation } from '@ember-data/record-data/-private/graph/-operations';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import Store, { recordIdentifierFor } from '@ember-data/store';
Expand Down Expand Up @@ -159,12 +159,6 @@ class V2TestRecordData implements RecordData {
): SingleResourceRelationship | CollectionResourceRelationship {
throw new Error('Method not implemented.');
}
setBelongsTo(identifier: StableRecordIdentifier, propertyName: string, value: StableRecordIdentifier | null): void {
throw new Error('Method not implemented.');
}
setHasMany(identifier: StableRecordIdentifier, propertyName: string, value: StableRecordIdentifier[]): void {
throw new Error('Method not implemented.');
}
addToHasMany(
identifier: StableRecordIdentifier,
propertyName: string,
Expand Down Expand Up @@ -196,7 +190,7 @@ class V2TestRecordData implements RecordData {
return false;
}
}
const TestRecordData = V2CACHE_SINGLETON_MANAGER ? V2TestRecordData : V1TestRecordData;
const TestRecordData = DEPRECATE_V1_RECORD_DATA ? V1TestRecordData : V2TestRecordData;

const CustomStore = Store.extend({
createRecordDataFor(identifier: StableRecordIdentifier, wrapper: RecordDataStoreWrapper) {
Expand All @@ -219,7 +213,7 @@ module('integration/record-data - Record Data State', function (hooks) {
});

test('Record Data state saving', async function (assert) {
assert.expect(3);
assert.expect(DEPRECATE_V1_RECORD_DATA ? 4 : 3);

let isDeleted, isNew, isDeletionCommitted;
let calledDelete = false;
Expand Down Expand Up @@ -300,10 +294,13 @@ module('integration/record-data - Record Data State', function (hooks) {

await person.save();
assert.true(calledUpdate, 'called update if record isnt deleted or new');
if (DEPRECATE_V1_RECORD_DATA) {
assert.expectDeprecation({ id: 'ember-data:deprecate-v1-cache', count: 1 });
}
});

test('Record Data state record flags', async function (assert) {
assert.expect(13);
assert.expect(DEPRECATE_V1_RECORD_DATA ? 14 : 13);
let isDeleted, isNew, isDeletionCommitted;
let calledSetIsDeleted = false;
let storeWrapper;
Expand Down Expand Up @@ -395,5 +392,8 @@ module('integration/record-data - Record Data State', function (hooks) {
storeWrapper.notifyChange(personIdentifier, 'state');
await settled();
assert.strictEqual(people.length, 0, 'commiting a deletion updates the live array');
if (DEPRECATE_V1_RECORD_DATA) {
assert.expectDeprecation({ id: 'ember-data:deprecate-v1-cache', count: 1 });
}
});
});
Loading