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

fix: A(PromiseManyArray) should have no-effect #7802

Merged
merged 1 commit into from
Dec 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { A } from '@ember/array';
import { w } from '@ember/string';

import { module, test } from 'qunit';

import { setupRenderingTest } from 'ember-qunit';

import Model, { attr, hasMany } from '@ember-data/model';

module('PromiseManyArray side-affected by EmberArray', (hooks) => {
setupRenderingTest(hooks);

test('PromiseManyArray is not side-affected by EmberArray', async function (assert) {
const { owner } = this;
class Person extends Model {
@attr('string') name;
}
class Group extends Model {
@hasMany('person', { inverse: null }) members;
}
owner.register('model:person', Person);
owner.register('model:group', Group);
const store = owner.lookup('service:store');
const members = w('Bob John Michael Larry Lucy').map((name) => store.createRecord('person', { name }));
const group = store.createRecord('group', { members });

const replaceFn = group.members.replace;
assert.strictEqual(group.members.length, 5, 'initial length is correct');

group.members.replace(0, 1);
assert.strictEqual(group.members.length, 4, 'updated length is correct');

A(group.members);

assert.strictEqual(replaceFn, group.members.replace, 'we have the same function for replace');
group.members.replace(0, 1);
assert.strictEqual(group.members.length, 3, 'updated length is correct');
});
});
11 changes: 10 additions & 1 deletion packages/model/addon/-private/system/promise-many-array.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ArrayMixin from '@ember/array';
import { assert } from '@ember/debug';
import { dependentKeyCompat } from '@ember/object/compat';
import { tracked } from '@glimmer/tracking';
Expand All @@ -16,7 +17,7 @@ import { DEPRECATE_EVENTED_API_USAGE } from '@ember-data/private-build-infra/dep

A PromiseManyArray is an array-like proxy that also proxies certain method calls
to the underlying ManyArray in addition to being "promisified".

Right now we proxy:

* `reload()`
Expand All @@ -41,6 +42,14 @@ export default class PromiseManyArray {
this._update(promise, content);
this.isDestroyed = false;
this.isDestroying = false;

const meta = Ember.meta(this);
meta.hasMixin = (mixin: Object) => {
if (mixin === ArrayMixin) {
return true;
}
return false;
};
}

//---- Methods/Properties on ArrayProxy that we will keep as our API
Expand Down
1 change: 1 addition & 0 deletions packages/store/types/ember/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export function run(callback: Function);
export const ENV: {
DS_WARN_ON_UNKNOWN_KEYS?: boolean;
};
export function meta(obj: Object): any;