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

[BUGFIX] Allow setting length on ArrayProxy. #16783

Merged
merged 1 commit into from
Jun 28, 2018
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
23 changes: 23 additions & 0 deletions packages/ember-runtime/lib/system/array_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PROPERTY_DID_CHANGE,
addArrayObserver,
removeArrayObserver,
replace,
} from 'ember-metal';
import EmberObject from './object';
import { isArray, MutableArray } from '../mixins/array';
Expand Down Expand Up @@ -193,6 +194,28 @@ export default class ArrayProxy extends EmberObject {
return this._length;
}

set length(value) {
let length = this.length;
let removedCount = length - value;
let added;

if (removedCount === 0) {
return;
} else if (removedCount < 0) {
added = new Array(-removedCount);
removedCount = 0;
}

let content = get(this, 'content');
if (content) {
replace(content, value, removedCount, added);

this._lengthDirty = true;
this._objectsDirtyIndex = 0;
this._objects = null;
}
}

[PROPERTY_DID_CHANGE](key) {
if (key === 'arrangedContent') {
let oldLength = this._objects === null ? 0 : this._objects.length;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
import { run } from '@ember/runloop';
import { get, set, changeProperties } from 'ember-metal';
import { not } from '@ember/object/computed';
import { set, changeProperties } from 'ember-metal';
import ArrayProxy from '../../../lib/system/array_proxy';
import { A as emberA } from '../../../lib/mixins/array';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

moduleFor(
'ArrayProxy - content change',
class extends AbstractTestCase {
['@test should update length for null content'](assert) {
let proxy = ArrayProxy.create({
content: emberA([1, 2, 3]),
});

assert.equal(proxy.get('length'), 3, 'precond - length is 3');

proxy.set('content', null);

assert.equal(proxy.get('length'), 0, 'length updates');
}

['@test should update length for null content when there is a computed property watching length'](
assert
) {
let proxy = ArrayProxy.extend({
isEmpty: not('length'),
}).create({
content: emberA([1, 2, 3]),
});

assert.equal(proxy.get('length'), 3, 'precond - length is 3');

// Consume computed property that depends on length
proxy.get('isEmpty');

// update content
proxy.set('content', null);

assert.equal(proxy.get('length'), 0, 'length updates');
}

["@test The ArrayProxy doesn't explode when assigned a destroyed object"](assert) {
let proxy1 = ArrayProxy.create();
let proxy2 = ArrayProxy.create();
Expand Down Expand Up @@ -92,32 +59,6 @@ moduleFor(
assert.deepEqual(indexes, [2, 3, 4]);
}

['@test getting length does not recompute the object cache'](assert) {
let indexes = [];

let proxy = ArrayProxy.extend({
objectAtContent(index) {
indexes.push(index);
return this.content[index];
},
}).create({
content: emberA([1, 2, 3, 4, 5]),
});

assert.equal(get(proxy, 'length'), 5);
assert.deepEqual(indexes, []);

indexes.length = 0;
proxy.set('content', emberA([6, 7, 8]));
assert.equal(get(proxy, 'length'), 3);
assert.deepEqual(indexes, []);

indexes.length = 0;
proxy.content.replace(1, 0, [1, 2, 3]);
assert.equal(get(proxy, 'length'), 6);
assert.deepEqual(indexes, []);
}

['@test negative indexes are handled correctly'](assert) {
let indexes = [];

Expand Down
128 changes: 127 additions & 1 deletion packages/ember-runtime/tests/system/array_proxy/length_test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,139 @@
import ArrayProxy from '../../../lib/system/array_proxy';
import EmberObject from '../../../lib/system/object';
import { observer } from 'ember-metal';
import { oneWay as reads } from '@ember/object/computed';
import { oneWay as reads, not } from '@ember/object/computed';
import { A as a } from '../../../lib/mixins/array';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { set, get } from 'ember-metal';

moduleFor(
'Ember.ArrayProxy - content change (length)',
class extends AbstractTestCase {
['@test should update length for null content'](assert) {
let proxy = ArrayProxy.create({
content: a([1, 2, 3]),
});

assert.equal(proxy.get('length'), 3, 'precond - length is 3');

proxy.set('content', null);

assert.equal(proxy.get('length'), 0, 'length updates');
}

['@test should update length for null content when there is a computed property watching length'](
assert
) {
let proxy = ArrayProxy.extend({
isEmpty: not('length'),
}).create({
content: a([1, 2, 3]),
});

assert.equal(proxy.get('length'), 3, 'precond - length is 3');

// Consume computed property that depends on length
proxy.get('isEmpty');

// update content
proxy.set('content', null);

assert.equal(proxy.get('length'), 0, 'length updates');
}

['@test getting length does not recompute the object cache'](assert) {
let indexes = [];

let proxy = ArrayProxy.extend({
objectAtContent(index) {
indexes.push(index);
return this.content[index];
},
}).create({
content: a([1, 2, 3, 4, 5]),
});

assert.equal(get(proxy, 'length'), 5);
assert.deepEqual(indexes, []);

indexes.length = 0;
proxy.set('content', a([6, 7, 8]));
assert.equal(get(proxy, 'length'), 3);
assert.deepEqual(indexes, []);

indexes.length = 0;
proxy.content.replace(1, 0, [1, 2, 3]);
assert.equal(get(proxy, 'length'), 6);
assert.deepEqual(indexes, []);
}

'@test accessing length after content set to null'(assert) {
let obj = ArrayProxy.create({ content: ['foo', 'bar'] });

assert.equal(obj.length, 2, 'precond');

set(obj, 'content', null);

assert.equal(obj.length, 0, 'length is 0 without content');
assert.deepEqual(obj.content, null, 'content was updated');
}

'@test setting length to 0'(assert) {
let obj = ArrayProxy.create({ content: ['foo', 'bar'] });

assert.equal(obj.length, 2, 'precond');

set(obj, 'length', 0);

assert.equal(obj.length, 0, 'length was updated');
assert.deepEqual(obj.content, [], 'content length was truncated');
}

'@test setting length to smaller value'(assert) {
let obj = ArrayProxy.create({ content: ['foo', 'bar'] });

assert.equal(obj.length, 2, 'precond');

set(obj, 'length', 1);

assert.equal(obj.length, 1, 'length was updated');
assert.deepEqual(obj.content, ['foo'], 'content length was truncated');
}

'@test setting length to larger value'(assert) {
let obj = ArrayProxy.create({ content: ['foo', 'bar'] });

assert.equal(obj.length, 2, 'precond');

set(obj, 'length', 3);

assert.equal(obj.length, 3, 'length was updated');
assert.deepEqual(obj.content, ['foo', 'bar', undefined], 'content length was updated');
}

'@test setting length after content set to null'(assert) {
let obj = ArrayProxy.create({ content: ['foo', 'bar'] });

assert.equal(obj.length, 2, 'precond');

set(obj, 'content', null);
assert.equal(obj.length, 0, 'length was updated');

set(obj, 'length', 0);
assert.equal(obj.length, 0, 'length is still updated');
}

'@test setting length to greater than zero'(assert) {
let obj = ArrayProxy.create({ content: ['foo', 'bar'] });

assert.equal(obj.length, 2, 'precond');

set(obj, 'length', 1);

assert.equal(obj.length, 1, 'length was updated');
assert.deepEqual(obj.content, ['foo'], 'content length was truncated');
}

['@test array proxy + aliasedProperty complex test'](assert) {
let aCalled, bCalled, cCalled, dCalled, eCalled;

Expand Down