Skip to content

Commit

Permalink
Provide placeholder functions for lifecycle methods (can't provide pl…
Browse files Browse the repository at this point in the history
…aceholder for `shouldUpdate` as some renderers need to check if it exists)
  • Loading branch information
Robert-Frampton authored and Robert-Frampton committed Nov 2, 2017
1 parent ba375c9 commit 8ae43fb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
45 changes: 38 additions & 7 deletions packages/metal-component/src/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ import { EventEmitter, EventHandler } from 'metal-events';
* class CustomComponent extends Component {
* created() {
* }
*
* willUpdate() {
* }
*
* rendered() {
* }
*
Expand All @@ -40,6 +37,19 @@ import { EventEmitter, EventHandler } from 'metal-events';
* attached() {
* }
*
* willReceiveState() {
* }
*
* // JSX components only
* willReceiveProps() {
* }
*
* shouldUpdate() {
* }
*
* willUpdate() {
* }
*
* willDetach() {
* }
*
Expand Down Expand Up @@ -417,9 +427,7 @@ class Component extends EventEmitter {
* @protected
*/
handleStateWillChange_(event) {
if (this.willReceiveState) {
this.willReceiveState(event.changes);
}
this.willReceiveState(event.changes);
}

/**
Expand Down Expand Up @@ -448,6 +456,15 @@ class Component extends EventEmitter {
this.emit('rendered', firstRender);
}

/**
* Informs the component that the renderer is about to update. Calls the
* component's `willUpdate` lifecycle method.
* @param {Object} changes
*/
informWillUpdate(...args) {
this.willUpdate(...args);
}

/**
* Checks if the given function is a component constructor.
* @param {!function()} fn Any function
Expand Down Expand Up @@ -715,6 +732,20 @@ class Component extends EventEmitter {
* Lifecycle. Fires before component is detached from the DOM.
*/
willDetach() {}

/**
* Lifecycle. Called when the component is about to receive state changes.
* Provides a hook point for modifying state that can be used in the next
* rerender.
* @param {Object} changes Changes made to this.state
*/
willReceiveState() {}

/**
* Lifecycle. Called when the component's renderer is about to update.
* @param {Object} changes
*/
willUpdate() {}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/metal-incremental-dom/src/IncrementalDomRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ class IncrementalDomRenderer extends ComponentRenderer.constructor {
* @param {Object} changes
*/
willUpdate_(component, changes) {
if (!component.willUpdate || !component.wasRendered || !changes) {
if (!component.wasRendered || !changes) {
return;
}
component.willUpdate(...this.buildShouldUpdateArgs(changes));
component.informWillUpdate(...this.buildShouldUpdateArgs(changes));
}
}

Expand Down
10 changes: 9 additions & 1 deletion packages/metal-jsx/src/JSXComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JSXComponent extends Component {
* @protected
*/
handleStateWillChange_(event) {
if (event.type !== 'state' && this.willReceiveProps) {
if (event.type !== 'state') {
this.willReceiveProps(event.changes);
}
}
Expand All @@ -64,6 +64,14 @@ class JSXComponent extends Component {

return retObj;
}

/**
* Lifecycle. Called when the component is about to receive new props.
* Provides a hook point for modifying state that can be used in the next
* rerender.
* @param {Object} changes Changes made to this.props
*/
willReceiveProps() {}
}

JSXComponent.DATA_MANAGER = JSXDataManager;
Expand Down

0 comments on commit 8ae43fb

Please sign in to comment.