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

Only deepClone when without prototype extensions #1107

Merged
merged 2 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -66,28 +66,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to confirm, this is so that we can send the cloned item through the postmessage right? I ask because I want to confirm that we don't actually need to worry about deepClone being used just to be able to mutate the value...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. As far as. I can tell this is related to structure cloning. It’s done as one of the last steps before passing to chrome’s postMessage.

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