Skip to content

Instantly share code, notes, and snippets.

@paularmstrong
Created October 31, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paularmstrong/8046f925665df347bc42f92ba68ab6c5 to your computer and use it in GitHub Desktop.
Save paularmstrong/8046f925665df347bc42f92ba68ab6c5 to your computer and use it in GitHub Desktop.
example.js
export default function(target) {
const originalMethods = {
UNSAFE_componentWillMount: target.prototype.UNSAFE_componentWillMount || noop,
componentDidMount: target.prototype.componentDidMount || noop,
UNSAFE_componentWillUpdate: target.prototype.UNSAFE_componentWillUpdate || noop,
componentDidUpdate: target.prototype.componentDidUpdate || noop,
componentWillUnmount: target.prototype.componentWillUnmount || noop
};
target.prototype.UNSAFE_componentWillMount = function(...args) {
const [name, identifier] = getIdentifiers(this, true);
originalMethods.UNSAFE_componentWillMount.apply(this, args);
recordTimestamp(name, identifier, 'willMount', Date.now());
};
target.prototype.componentDidMount = function(...args) {
const [name, identifier] = getIdentifiers(this);
originalMethods.componentDidMount.apply(this, args);
recordTimestamp(name, identifier, 'didMount', Date.now());
throttledRecordMetrics();
};
target.prototype.UNSAFE_componentWillUpdate = function(...args) {
const [name, identifier] = getIdentifiers(this, true);
originalMethods.UNSAFE_componentWillUpdate.apply(this, args);
recordTimestamp(name, identifier, 'willUpdate', Date.now());
};
target.prototype.componentDidUpdate = function(...args) {
const [name, identifier] = getIdentifiers(this);
originalMethods.componentDidUpdate.apply(this, args);
recordTimestamp(name, identifier, 'didUpdate', Date.now());
throttledRecordMetrics();
};
target.prototype.componentWillUnmount = function(...args) {
originalMethods.componentWillUnmount.apply(this, args);
throttledRecordMetrics();
};
return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment