Skip to content

Commit

Permalink
schedule watchTypeIfUnseen to prevent loop (#8008)
Browse files Browse the repository at this point in the history
* schedule watchTypeIfUnseen to prevent loop

fixes #8006

* Update debug-adapter-test.js

* Update index.js

* Update index.js

* fix test

* Update debug-adapter-test.js

* keep Watching Model Types with store.push

* add comment

* fix test
  • Loading branch information
patricklx authored and runspired committed Sep 12, 2022
1 parent 0956c49 commit 2878466
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 23 additions & 4 deletions packages/-ember-data/tests/integration/debug-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ if (has('@ember-data/debug')) {
});

test('Watching Model Types', async function (assert) {
assert.expect(5);
assert.expect(4);
let { owner } = this;
let debugAdapter = owner.lookup('data-adapter:main');

function added(types) {
assert.equal(types.length, 1, 'added one type');
assert.equal(types[0].name, 'post', 'the type is post');
assert.equal(types[0].count, 0, 'we added zero posts');
assert.strictEqual(types.length, 1, 'added one type');
assert.strictEqual(types[0].name, 'post', 'the type is post');
assert.strictEqual(types[0].count, 1, 'we added one post');
assert.strictEqual(types[0].object, store.modelFor('post'), 'we received the ModelClass for post');
}

Expand All @@ -67,6 +67,25 @@ if (has('@ember-data/debug')) {
});
});

test('Watching Model Types On first-create', async function (assert) {
assert.expect(4);
let { owner } = this;
let debugAdapter = owner.lookup('data-adapter:main');

function added(types) {
assert.strictEqual(types.length, 1, 'added one type');
assert.strictEqual(types[0].name, 'post', 'the type is post');
assert.strictEqual(types[0].count, 1, 'we added one posts');
assert.strictEqual(types[0].object, store.modelFor('post'), 'we received the ModelClass for post');
}

debugAdapter.watchModelTypes(added, () => null);

store.createRecord('post', {
title: 'Post Title',
});
});

test('Watching Records', async function (assert) {
let { owner } = this;
let debugAdapter = owner.lookup('data-adapter:main');
Expand Down
4 changes: 3 additions & 1 deletion packages/debug/addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { get } from '@ember/object';
import { addObserver, removeObserver } from '@ember/object/observers';
import { inject as service } from '@ember/service';
import { capitalize, underscore } from '@ember/string';
import { next } from '@ember/runloop';

import { typesMapFor } from './setup';

Expand Down Expand Up @@ -91,7 +92,8 @@ export default DataAdapter.extend({

// Overwrite _createRecordData so newly added models will get added to the list
store._createRecordData = (identifier) => {
this.watchTypeIfUnseen(store, discoveredTypes, identifier.type, typesAdded, typesUpdated, _releaseMethods);
// defer to ensure first-create does not result in an infinite loop, see https://github.com/emberjs/data/issues/8006
next(() => this.watchTypeIfUnseen(store, discoveredTypes, identifier.type, typesAdded, typesUpdated, _releaseMethods));
return __createRecordData.call(store, identifier);
};

Expand Down

0 comments on commit 2878466

Please sign in to comment.