Skip to content

Commit

Permalink
Adds why-did-you-update and example change. (mattermost#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkraft authored and jwilander committed Dec 24, 2017
1 parent 53a778f commit 9a0026d
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
6 changes: 6 additions & 0 deletions components/form_error.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import PropTypes from 'prop-types';
import React from 'react';

import shallowEqualExceptKeys from 'utils/equality';

export default class FormError extends React.Component {
static get propTypes() {
// accepts either a single error or an array of errors
Expand All @@ -22,6 +24,10 @@ export default class FormError extends React.Component {
};
}

shouldComponentUpdate(nextProps) {
return !shallowEqualExceptKeys(nextProps, this.props, ['errors']);
}

render() {
if (!this.props.error && this.props.errors.length === 0) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@
"style-loader": "0.19.0",
"url-loader": "0.6.2",
"webpack": "3.8.1",
"webpack-node-externals": "1.6.0"
"webpack-node-externals": "1.6.0",
"why-did-you-update": "https://github.com/mkraft/why-did-you-update.git#master"
},
"jest": {
"snapshotSerializers": [
Expand Down
10 changes: 10 additions & 0 deletions root.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function preRenderSetup(callwhendone) {
$.when(d1).done(() => {
initializePlugins();
I18n.doAddLocaleData();
devModeInit();
callwhendone();
});
}
Expand Down Expand Up @@ -164,6 +165,15 @@ function appendOnLoadEvent(fn) {
}
}

function devModeInit() {
if (window.mm_config && window.mm_config.EnableDeveloper === 'true') {
const {whyDidYouUpdate} = require('why-did-you-update'); // eslint-disable-line global-require
whyDidYouUpdate(React);

// Add other dev. mode init here
}
}

global.window.setup_root = () => {
// Append trackLoadTime function to any exisitng onload events
appendOnLoadEvent(trackLoadTime);
Expand Down
60 changes: 60 additions & 0 deletions utils/equality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const hasOwnProperty = Object.prototype.hasOwnProperty;

/**
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
function is(x, y) {
// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
// Added the nonzero y check to make Flow happy, but it is redundant
return x !== 0 || y !== 0 || 1 / x === 1 / y;
}

// Step 6.a: NaN == NaN
return x !== x && y !== y;
}

function deepIs(x, y) {
return JSON.stringify(x) === JSON.stringify(y);
}

/*
* Adaptation of https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
* that allows specific keys to use a more expensive deep equality check.
*/
function shallowEqualExceptKeys(objA, objB, deepKeys = []) {
if (is(objA, objB)) {
return true;
}

if (typeof objA !== 'object' || objA === null ||
typeof objB !== 'object' || objB === null) {
return false;
}

const keysA = Object.keys(objA);
const keysB = Object.keys(objB);

if (keysA.length !== keysB.length) {
return false;
}

// Test for A's keys different from B.
for (let i = 0; i < keysA.length; i++) {
if (!hasOwnProperty.call(objB, keysA[i])) {
return false;
}
if (deepKeys.includes(keysA[i]) && !deepIs(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
if (!deepKeys.includes(keysA[i]) && !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}

return true;
}

module.exports = shallowEqualExceptKeys;
48 changes: 47 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5402,6 +5402,14 @@ lodash.escape@^3.0.0:
dependencies:
lodash._root "^3.0.0"

lodash.every@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.every/-/lodash.every-4.6.0.tgz#eb89984bebc4364279bb3aefbbd1ca19bfa6c6a7"

lodash.filter@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.filter/-/lodash.filter-4.6.0.tgz#668b1d4981603ae1cc5a6fa760143e480b4c4ace"

lodash.flattendeep@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2"
Expand Down Expand Up @@ -5433,14 +5441,22 @@ lodash.isempty@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.isempty/-/lodash.isempty-4.4.0.tgz#6f86cbedd8be4ec987be9aaf33c9684db1b31e7e"

lodash.isequal@^4.0.0:
lodash.isequal@^4.0.0, lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"

lodash.isfunction@^3.0.8:
version "3.0.8"
resolved "https://registry.yarnpkg.com/lodash.isfunction/-/lodash.isfunction-3.0.8.tgz#4db709fc81bc4a8fd7127a458a5346c5cdce2c6b"

lodash.isplainobject@^4.0.0:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"

lodash.isstring@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"

lodash.keys@^3.0.0:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a"
Expand All @@ -5449,6 +5465,10 @@ lodash.keys@^3.0.0:
lodash.isarguments "^3.0.0"
lodash.isarray "^3.0.0"

lodash.keys@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-4.2.0.tgz#a08602ac12e4fb83f91fc1fb7a360a4d9ba35205"

lodash.keysin@^4.0.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/lodash.keysin/-/lodash.keysin-4.2.0.tgz#8cc3fb35c2d94acc443a1863e02fa40799ea6f28"
Expand All @@ -5465,6 +5485,10 @@ lodash.once@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"

lodash.pick@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"

lodash.pickby@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.pickby/-/lodash.pickby-4.6.0.tgz#7dea21d8c18d7703a27c704c15d3b84a67e33aff"
Expand All @@ -5481,6 +5505,10 @@ lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"

lodash.some@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.some/-/lodash.some-4.6.0.tgz#1bb9f314ef6b8baded13b549169b2a945eb68e4d"

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down Expand Up @@ -5510,6 +5538,10 @@ lodash.templatesettings@^3.0.0:
lodash._reinterpolate "^3.0.0"
lodash.escape "^3.0.0"

lodash.union@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.union/-/lodash.union-4.6.0.tgz#48bb5088409f16f1821666641c44dd1aaae3cd88"

lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
Expand Down Expand Up @@ -9069,6 +9101,20 @@ which@1, which@^1.2.12, which@^1.2.9:
dependencies:
isexe "^2.0.0"

"why-did-you-update@https://github.com/mkraft/why-did-you-update.git#master":
version "0.1.0"
resolved "https://github.com/mkraft/why-did-you-update.git#0a74da620ec3c20fb78205acbc89ffdd12e8912b"
dependencies:
lodash.every "^4.6.0"
lodash.filter "^4.6.0"
lodash.isequal "^4.5.0"
lodash.isfunction "^3.0.8"
lodash.isstring "^4.0.1"
lodash.keys "^4.2.0"
lodash.pick "^4.4.0"
lodash.some "^4.6.0"
lodash.union "^4.6.0"

wide-align@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.0.tgz#40edde802a71fea1f070da3e62dcda2e7add96ad"
Expand Down

0 comments on commit 9a0026d

Please sign in to comment.