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 beta] Don't use prototype extensions (arrayContent{Did,Will}Change, removeAt) #13534

Merged
merged 3 commits into from
Jun 16, 2016
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
Expand Up @@ -5,6 +5,7 @@ import { set } from 'ember-metal/property_set';

import EmberObject from 'ember-runtime/system/object';
import { A as emberA } from 'ember-runtime/system/native_array';
import { removeAt } from 'ember-runtime/mixins/mutable_array';

import ActionManager from 'ember-views/system/action_manager';
import jQuery from 'ember-views/system/jquery';
Expand Down Expand Up @@ -761,7 +762,7 @@ moduleFor('Helpers test: element action', class extends RenderingTest {
var actionId = this.$('a[data-ember-action]').attr('data-ember-action');

this.runTask(() => {
things.removeAt(0);
removeAt(things, 0);
});

ok(!ActionManager.registeredActions[actionId], 'After the virtual view was destroyed, the action was unregistered');
Expand Down
13 changes: 7 additions & 6 deletions packages/ember-glimmer/tests/integration/syntax/each-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { set } from 'ember-metal/property_set';
import { applyMixins, strip } from '../../utils/abstract-test-case';
import { moduleFor, RenderingTest } from '../../utils/test-case';
import { A as emberA } from 'ember-runtime/system/native_array';
import { removeAt } from 'ember-runtime/mixins/mutable_array';

import {
BasicConditionalsTest,
Expand Down Expand Up @@ -78,7 +79,7 @@ moduleFor('Syntax test: {{#each as}}', class extends EachTest {
this.runTask(() => {
let list = get(this.context, 'list');
list.pushObject({ text: 'Earth' });
list.removeAt(1);
removeAt(list, 1);
list.insertAt(1, { text: 'Globe' });
});

Expand All @@ -87,23 +88,23 @@ moduleFor('Syntax test: {{#each as}}', class extends EachTest {
this.runTask(() => {
let list = get(this.context, 'list');
list.pushObject({ text: 'Planet' });
list.removeAt(1);
removeAt(list, 1);
list.insertAt(1, { text: ' ' });
list.pushObject({ text: ' ' });
list.pushObject({ text: 'Earth' });
list.removeAt(3);
removeAt(list, 3);
});

this.assertText('Hello WorldPlanet Earth');

this.runTask(() => {
let list = get(this.context, 'list');
list.pushObject({ text: 'Globe' });
list.removeAt(1);
removeAt(list, 1);
list.insertAt(1, { text: ' ' });
list.pushObject({ text: ' ' });
list.pushObject({ text: 'World' });
list.removeAt(2);
removeAt(list, 2);
});

this.assertText('Hello Planet EarthGlobe World');
Expand Down Expand Up @@ -372,7 +373,7 @@ moduleFor('Syntax test: {{#each as}}', class extends EachTest {
this.runTask(() => {
let people = get(this.context, 'people');
set(people.objectAt(1), 'name', 'Stefan Penner');
people.removeAt(0);
removeAt(people, 0);
people.pushObject({ name: 'Tom Dale' });
people.insertAt(1, { name: 'Chad Hietala' });
set(this.context, 'title', 'Principal Engineer');
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-glimmer/tests/integration/syntax/with-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { moduleFor, RenderingTest } from '../../utils/test-case';
import { TogglingSyntaxConditionalsTest } from '../../utils/shared-conditional-tests';
import { strip } from '../../utils/abstract-test-case';
import ObjectProxy from 'ember-runtime/system/object_proxy';
import { removeAt } from 'ember-runtime/mixins/mutable_array';

moduleFor('Syntax test: {{#with}}', class extends TogglingSyntaxConditionalsTest {

Expand Down Expand Up @@ -198,7 +199,7 @@ moduleFor('Syntax test: {{#with as}}', class extends TogglingSyntaxConditionalsT
this.runTask(() => {
let array = get(this.context, 'arrayThing');
array.replace(0, 1, 'Goodbye');
array.removeAt(1);
removeAt(array, 1);
array.insertAt(1, ', ');
array.pushObject('!');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import EmberObject from 'ember-runtime/system/object';
import ObjectProxy from 'ember-runtime/system/object_proxy';
import { A as emberA } from 'ember-runtime/system/native_array';
import ArrayProxy from 'ember-runtime/system/array_proxy';
import { removeAt } from 'ember-runtime/mixins/mutable_array';
import { Component } from './helpers';

class AbstractConditionalsTest extends RenderingTest {
Expand Down Expand Up @@ -308,7 +309,7 @@ export const ArrayTestCases = {

this.assertText('T1F2');

this.runTask(() => get(this.context, 'cond1').removeAt(0));
this.runTask(() => removeAt(get(this.context, 'cond1'), 0));

this.assertText('F1F2');

Expand Down Expand Up @@ -373,7 +374,7 @@ export const ArrayTestCases = {

this.assertText('T1F2');

this.runTask(() => get(this.context, 'cond1.content').removeAt(0));
this.runTask(() => removeAt(get(this.context, 'cond1.content'), 0));

this.assertText('F1F2');

Expand Down
183 changes: 95 additions & 88 deletions packages/ember-runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,99 @@ export function objectAt(content, idx) {
return content[idx];
}

export function arrayContentWillChange(array, startIdx, removeAmt, addAmt) {
let removing, lim;

// if no args are passed assume everything changes
if (startIdx === undefined) {
startIdx = 0;
removeAmt = addAmt = -1;
} else {
if (removeAmt === undefined) {
removeAmt = -1;
}

if (addAmt === undefined) {
addAmt = -1;
}
}

if (array.__each) {
array.__each.arrayWillChange(array, startIdx, removeAmt, addAmt);
}

sendEvent(array, '@array:before', [array, startIdx, removeAmt, addAmt]);

if (startIdx >= 0 && removeAmt >= 0 && get(array, 'hasEnumerableObservers')) {
removing = [];
lim = startIdx + removeAmt;

for (let idx = startIdx; idx < lim; idx++) {
removing.push(objectAt(array, idx));
}
} else {
removing = removeAmt;
}

array.enumerableContentWillChange(removing, addAmt);

return array;
}

export function arrayContentDidChange(array, startIdx, removeAmt, addAmt) {
markObjectAsDirty(metaFor(array));

// if no args are passed assume everything changes
if (startIdx === undefined) {
startIdx = 0;
removeAmt = addAmt = -1;
} else {
if (removeAmt === undefined) {
removeAmt = -1;
}

if (addAmt === undefined) {
addAmt = -1;
}
}

let adding;
if (startIdx >= 0 && addAmt >= 0 && get(array, 'hasEnumerableObservers')) {
adding = [];
let lim = startIdx + addAmt;

for (let idx = startIdx; idx < lim; idx++) {
adding.push(objectAt(array, idx));
}
} else {
adding = addAmt;
}

array.enumerableContentDidChange(removeAmt, adding);

if (array.__each) {
array.__each.arrayDidChange(array, startIdx, removeAmt, addAmt);
}

sendEvent(array, '@array:change', [array, startIdx, removeAmt, addAmt]);

let length = get(array, 'length');
let cachedFirst = cacheFor(array, 'firstObject');
let cachedLast = cacheFor(array, 'lastObject');

if (objectAt(array, 0) !== cachedFirst) {
propertyWillChange(array, 'firstObject');
propertyDidChange(array, 'firstObject');
}

if (objectAt(array, length - 1) !== cachedLast) {
propertyWillChange(array, 'lastObject');
propertyDidChange(array, 'lastObject');
}

return array;
}

const EMBER_ARRAY = symbol('EMBER_ARRAY');

export function isEmberArray(obj) {
Expand Down Expand Up @@ -437,43 +530,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@public
*/
arrayContentWillChange(startIdx, removeAmt, addAmt) {
let removing, lim;

// if no args are passed assume everything changes
if (startIdx === undefined) {
startIdx = 0;
removeAmt = addAmt = -1;
} else {
if (removeAmt === undefined) {
removeAmt = -1;
}

if (addAmt === undefined) {
addAmt = -1;
}
}

if (this.__each) {
this.__each.arrayWillChange(this, startIdx, removeAmt, addAmt);
}

sendEvent(this, '@array:before', [this, startIdx, removeAmt, addAmt]);


if (startIdx >= 0 && removeAmt >= 0 && get(this, 'hasEnumerableObservers')) {
removing = [];
lim = startIdx + removeAmt;

for (let idx = startIdx; idx < lim; idx++) {
removing.push(objectAt(this, idx));
}
} else {
removing = removeAmt;
}

this.enumerableContentWillChange(removing, addAmt);

return this;
return arrayContentWillChange(this, startIdx, removeAmt, addAmt);
},

/**
Expand All @@ -492,57 +549,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@public
*/
arrayContentDidChange(startIdx, removeAmt, addAmt) {
markObjectAsDirty(metaFor(this));

// if no args are passed assume everything changes
if (startIdx === undefined) {
startIdx = 0;
removeAmt = addAmt = -1;
} else {
if (removeAmt === undefined) {
removeAmt = -1;
}

if (addAmt === undefined) {
addAmt = -1;
}
}

let adding;
if (startIdx >= 0 && addAmt >= 0 && get(this, 'hasEnumerableObservers')) {
adding = [];
let lim = startIdx + addAmt;

for (let idx = startIdx; idx < lim; idx++) {
adding.push(objectAt(this, idx));
}
} else {
adding = addAmt;
}

this.enumerableContentDidChange(removeAmt, adding);

if (this.__each) {
this.__each.arrayDidChange(this, startIdx, removeAmt, addAmt);
}

sendEvent(this, '@array:change', [this, startIdx, removeAmt, addAmt]);

let length = get(this, 'length');
let cachedFirst = cacheFor(this, 'firstObject');
let cachedLast = cacheFor(this, 'lastObject');

if (objectAt(this, 0) !== cachedFirst) {
propertyWillChange(this, 'firstObject');
propertyDidChange(this, 'firstObject');
}

if (objectAt(this, length - 1) !== cachedLast) {
propertyWillChange(this, 'lastObject');
propertyDidChange(this, 'lastObject');
}

return this;
return arrayContentDidChange(this, startIdx, removeAmt, addAmt);
},

/**
Expand Down
32 changes: 18 additions & 14 deletions packages/ember-runtime/lib/mixins/mutable_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ import MutableEnumerable from 'ember-runtime/mixins/mutable_enumerable';
import Enumerable from 'ember-runtime/mixins/enumerable';
import isEnabled from 'ember-metal/features';

export function removeAt(array, start, len) {
if ('number' === typeof start) {
if ((start < 0) || (start >= get(array, 'length'))) {
throw new EmberError(OUT_OF_RANGE_EXCEPTION);
}

// fast case
if (len === undefined) {
len = 1;
}

array.replace(start, len, EMPTY);
}

return array;
}

/**
This mixin defines the API for modifying array-like objects. These methods
can be applied only to a collection that keeps its items in an ordered set.
Expand Down Expand Up @@ -141,20 +158,7 @@ export default Mixin.create(EmberArray, MutableEnumerable, {
@public
*/
removeAt(start, len) {
if ('number' === typeof start) {
if ((start < 0) || (start >= get(this, 'length'))) {
throw new EmberError(OUT_OF_RANGE_EXCEPTION);
}

// fast case
if (len === undefined) {
len = 1;
}

this.replace(start, len, EMPTY);
}

return this;
return removeAt(this, start, len);
},

/**
Expand Down
7 changes: 5 additions & 2 deletions packages/ember-runtime/lib/system/array_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import alias from 'ember-metal/alias';
import {
addArrayObserver,
removeArrayObserver,
arrayContentDidChange,
arrayContentWillChange,
objectAt
} from 'ember-runtime/mixins/array';

Expand All @@ -33,6 +35,7 @@ const EMPTY = [];

function K() { return this; }


/**
An ArrayProxy wraps any other object that implements `Ember.Array` and/or
`Ember.MutableArray,` forwarding all requests. This makes it very useful for
Expand Down Expand Up @@ -371,11 +374,11 @@ export default EmberObject.extend(MutableArray, {
},

arrangedContentArrayWillChange(item, idx, removedCnt, addedCnt) {
this.arrayContentWillChange(idx, removedCnt, addedCnt);
arrayContentWillChange(this, idx, removedCnt, addedCnt);
},

arrangedContentArrayDidChange(item, idx, removedCnt, addedCnt) {
this.arrayContentDidChange(idx, removedCnt, addedCnt);
arrayContentDidChange(this, idx, removedCnt, addedCnt);
},

init() {
Expand Down
Loading