Skip to content

Commit

Permalink
Merge pull request #1107 from emberjs/no-deep-clone
Browse files Browse the repository at this point in the history
Only deepClone when without prototype extensions
  • Loading branch information
chancancode committed Dec 18, 2019
2 parents e9ebeda + 794dfb5 commit 4a5de94
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
strategy:
matrix:
scenario:
- default-no-prototype-extensions
- release
- beta
- canary
Expand All @@ -77,6 +78,9 @@ jobs:
uses: rwjblue/setup-volta@v1
- name: Install dependencies (yarn)
run: yarn install
- name: Set NO_EXTEND_PROTOTYPES
if: matrix.scenario == 'default-no-prototype-extensions'
run: echo "::set-env name=NO_EXTEND_PROTOTYPES,::true"
- name: Setup ember-try scenario
run: yarn ember try:one ember-${{ matrix.scenario }} --skip-cleanup --- cat package.json
- name: Build
Expand Down
6 changes: 6 additions & 0 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ module.exports = function() {
npm: {
devDependencies: {}
}
},
{
name: 'ember-default-no-prototype-extensions',
env: {
NO_EXTEND_PROTOTYPES: 'true'
}
}
]
};
Expand Down
2 changes: 1 addition & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function(environment) {
// e.g. EMBER_MODULE_UNIFICATION: true
EMBER_METAL_TRACKED_PROPERTIES: true
},
EXTEND_PROTOTYPES: {
EXTEND_PROTOTYPES: process.env.NO_EXTEND_PROTOTYPES ? false : {
// Prevent Ember Data from overriding Date.parse.
Date: false
}
Expand Down
56 changes: 32 additions & 24 deletions ember_debug/adapters/web-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,36 @@ export default BasicAdapter.extend({
}
});

/**
* Recursively clones all arrays. Needed because Chrome
* refuses to clone Ember Arrays when extend prototypes is disabled.
*
* If the item passed is an array, a clone of the array is returned.
* If the item is an object or an array, or array properties/items are cloned.
*
* @param {Mixed} item The item to clone
* @return {Mixed}
*/
function deepClone(item) {
let clone = item;
if (isArray(item)) {
clone = new Array(item.length);
item.forEach((child, key) => {
clone[key] = deepClone(child);
});
} else if (item && typeOf(item) === 'object') {
clone = {};
keys(item).forEach(key => {
clone[key] = deepClone(item[key]);
});
}
return clone;
let deepClone;

if (Ember.ENV.EXTEND_PROTOTYPES.Array) {
deepClone = function deepClone(item) {
return item;
};
} else {
/**
* Recursively clones all arrays. Needed because Chrome
* refuses to clone Ember Arrays when extend prototypes is disabled.
*
* If the item passed is an array, a clone of the array is returned.
* If the item is an object or an array, or array properties/items are cloned.
*
* @param {Mixed} item The item to clone
* @return {Mixed}
*/
deepClone = function deepClone(item) {
let clone = item;
if (isArray(item)) {
clone = new Array(item.length);
item.forEach((child, key) => {
clone[key] = deepClone(child);
});
} else if (item && typeOf(item) === 'object') {
clone = {};
keys(item).forEach(key => {
clone[key] = deepClone(item[key]);
});
}
return clone;
};
}
15 changes: 11 additions & 4 deletions tests/ember_debug/deprecation-debug-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ module('Ember Debug - Deprecation', function(hooks) {
});

test('deprecations are caught and sent', async function t(assert) {
let messages = [];
let deprecations, count;

port.reopen({
send(name, message) {
messages.push({ name, message });
if (name === 'deprecation:deprecationsAdded') {
deprecations = message.deprecations;
} else if (name === 'deprecation:count') {
count = message.count;
}
}
});

Expand All @@ -52,20 +57,22 @@ module('Ember Debug - Deprecation', function(hooks) {
});

run(port, 'trigger', 'deprecation:watch');

await visit('/');
let deprecations = messages.filterBy('name', 'deprecation:deprecationsAdded').get('lastObject').message.deprecations;

assert.equal(deprecations.length, 2);

let deprecation = deprecations[0];
assert.equal(deprecation.count, 2, 'Correctly combined');
assert.equal(deprecation.message, 'Deprecation 1');
assert.equal(deprecation.sources.length, 2, 'Correctly separated by source');

deprecation = deprecations[1];
assert.equal(deprecation.count, 1);
assert.equal(deprecation.message, 'Deprecation 2');
assert.equal(deprecation.sources.length, 1);
assert.equal(deprecation.url, 'http://www.emberjs.com');

let count = messages.filterBy('name', 'deprecation:count').get('lastObject').message.count;
assert.equal(count, 3, 'count correctly sent');
});

Expand Down

0 comments on commit 4a5de94

Please sign in to comment.