Skip to content

Commit

Permalink
fix: A(PromiseManyArray) should have no-effect
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Dec 15, 2021
1 parent 61c61be commit 28e59e8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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;

0 comments on commit 28e59e8

Please sign in to comment.