Skip to content

Commit

Permalink
refactor(vdom): rename newPropsReceived() to propsChanged()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Component lifecycle method `newPropsReceived()` renamed
to `propsChanged()`.
  • Loading branch information
localvoid committed Jun 10, 2018
1 parent 7253efb commit 6434f5a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
32 changes: 16 additions & 16 deletions packages/ivi/src/vdom/__tests__/lifecycle.spec.ts
Expand Up @@ -17,7 +17,7 @@ interface ComponentHooks<P> {
oldProps: P,
newProps: P,
) => boolean;
newPropsReceived?: (
propsChanged?: (
this: Component<P>,
oldProps: P,
newProps: P,
Expand Down Expand Up @@ -52,10 +52,10 @@ const LifecycleTester = statefulComponent(class extends Component<LifecycleTeste
return true;
}

newPropsReceived(oldProps: LifecycleTesterProps, newProps: LifecycleTesterProps): void {
lifecycleTouch(newProps.id, "newPropsReceived");
if (newProps.hooks.newPropsReceived) {
newProps.hooks.newPropsReceived.call(this, oldProps, newProps);
propsChanged(oldProps: LifecycleTesterProps, newProps: LifecycleTesterProps): void {
lifecycleTouch(newProps.id, "propsChanged");
if (newProps.hooks.propsChanged) {
newProps.hooks.propsChanged.call(this, oldProps, newProps);
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ test(`<C><div></C>`, () => {
expect(c("1", "render")).toBe(1);
expect(c("1", "attached")).toBe(2);

expect(c("1", "newPropsReceived")).toBe(-1);
expect(c("1", "propsChanged")).toBe(-1);
expect(c("1", "shouldUpdate")).toBe(-1);
expect(c("1", "detached")).toBe(-1);
expect(c("1", "updated")).toBe(-1);
Expand All @@ -171,7 +171,7 @@ test(`<C><div></C> => <div>`, () => {
expect(c("1", "attached")).toBe(2);
expect(c("1", "detached")).toBe(3);

expect(c("1", "newPropsReceived")).toBe(-1);
expect(c("1", "propsChanged")).toBe(-1);
expect(c("1", "shouldUpdate")).toBe(-1);
expect(c("1", "updated")).toBe(-1);
expect(c("1", "invalidated")).toBe(-1);
Expand All @@ -190,7 +190,7 @@ test(`<div> => <C><div></C>`, () => {
expect(c("1", "render")).toBe(1);
expect(c("1", "attached")).toBe(2);

expect(c("1", "newPropsReceived")).toBe(-1);
expect(c("1", "propsChanged")).toBe(-1);
expect(c("1", "shouldUpdate")).toBe(-1);
expect(c("1", "detached")).toBe(-1);
expect(c("1", "updated")).toBe(-1);
Expand All @@ -210,7 +210,7 @@ test(`<div></div> => <div><C><div></C></div>`, () => {
expect(c("1", "render")).toBe(1);
expect(c("1", "attached")).toBe(2);

expect(c("1", "newPropsReceived")).toBe(-1);
expect(c("1", "propsChanged")).toBe(-1);
expect(c("1", "shouldUpdate")).toBe(-1);
expect(c("1", "detached")).toBe(-1);
expect(c("1", "updated")).toBe(-1);
Expand All @@ -231,7 +231,7 @@ test(`<div><C><div></C></div> => <div></div>`, () => {
expect(c("1", "attached")).toBe(2);
expect(c("1", "detached")).toBe(3);

expect(c("1", "newPropsReceived")).toBe(-1);
expect(c("1", "propsChanged")).toBe(-1);
expect(c("1", "shouldUpdate")).toBe(-1);
expect(c("1", "updated")).toBe(-1);
expect(c("1", "invalidated")).toBe(-1);
Expand All @@ -252,15 +252,15 @@ test(`<C><C><div></C></C>`, () => {
expect(c("1", "attached")).toBe(4);
expect(c("2", "attached")).toBe(5);

expect(c("1", "newPropsReceived")).toBe(-1);
expect(c("1", "propsChanged")).toBe(-1);
expect(c("1", "shouldUpdate")).toBe(-1);
expect(c("1", "detached")).toBe(-1);
expect(c("1", "updated")).toBe(-1);
expect(c("1", "invalidated")).toBe(-1);
expect(c("1", "shouldAugment")).toBe(-1);

expect(c("2", "shouldUpdate")).toBe(-1);
expect(c("2", "newPropsReceived")).toBe(-1);
expect(c("2", "propsChanged")).toBe(-1);
expect(c("2", "detached")).toBe(-1);
expect(c("2", "updated")).toBe(-1);
expect(c("2", "invalidated")).toBe(-1);
Expand All @@ -284,13 +284,13 @@ test(`<C><C><div></C></C> => <div>`, () => {
expect(c("2", "detached")).toBe(6);
expect(c("1", "detached")).toBe(7);

expect(c("1", "newPropsReceived")).toBe(-1);
expect(c("1", "propsChanged")).toBe(-1);
expect(c("1", "shouldUpdate")).toBe(-1);
expect(c("1", "updated")).toBe(-1);
expect(c("1", "invalidated")).toBe(-1);
expect(c("1", "shouldAugment")).toBe(-1);

expect(c("2", "newPropsReceived")).toBe(-1);
expect(c("2", "propsChanged")).toBe(-1);
expect(c("2", "shouldUpdate")).toBe(-1);
expect(c("2", "updated")).toBe(-1);
expect(c("2", "invalidated")).toBe(-1);
Expand All @@ -307,7 +307,7 @@ test(`<C><div></C> => <C><div></C>`, () => {

expect(c("1", "constructor")).toBe(0);
expect(c("1", "attached")).toBe(2);
expect(c("1", "newPropsReceived")).toBe(3);
expect(c("1", "propsChanged")).toBe(3);
expect(c("1", "shouldUpdate")).toBe(4);
expect(c("1", "render")).toBe(5); // 1
expect(c("1", "updated")).toBe(6);
Expand All @@ -333,7 +333,7 @@ test(`<S><C><div></C></S> => <S><C><div></C></S>`, () => {

expect(c("1", "updated")).toBe(-1);

expect(c("1", "newPropsReceived")).toBe(-1);
expect(c("1", "propsChanged")).toBe(-1);
expect(c("1", "shouldUpdate")).toBe(-1);

expect(c("1", "detached")).toBe(-1);
Expand Down
2 changes: 1 addition & 1 deletion packages/ivi/src/vdom/__tests__/stateful_component.spec.ts
Expand Up @@ -23,7 +23,7 @@ test(`props should be passed to constructor`, () => {
test(`props should be passed to newPropsReceived hook`, () => {
startRender((r) => {
const c = statefulComponent(class extends DivComponent<number> {
newPropsReceived(a: number, b: number) {
propsChanged(a: number, b: number) {
expect(a).toBe(1337);
expect(b).toBe(1338);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/ivi/src/vdom/__tests__/utils/lifecycle.ts
Expand Up @@ -2,7 +2,7 @@ export interface LifecycleCounters {
global: number;
constructor: number;
shouldUpdate: number;
newPropsReceived: number;
propsChanged: number;
attached: number;
detached: number;
updated: number;
Expand All @@ -15,7 +15,7 @@ const LIFECYCLE_COUNTERS: LifecycleCounters = {
global: 0,
constructor: 0,
shouldUpdate: 0,
newPropsReceived: 0,
propsChanged: 0,
attached: 0,
detached: 0,
updated: 0,
Expand Down Expand Up @@ -55,7 +55,7 @@ export function checkLifecycle(
LIFECYCLE_COUNTERS.global = 0;
LIFECYCLE_COUNTERS.constructor = 0;
LIFECYCLE_COUNTERS.shouldUpdate = 0;
LIFECYCLE_COUNTERS.newPropsReceived = 0;
LIFECYCLE_COUNTERS.propsChanged = 0;
LIFECYCLE_COUNTERS.attached = 0;
LIFECYCLE_COUNTERS.detached = 0;
LIFECYCLE_COUNTERS.updated = 0;
Expand Down
4 changes: 2 additions & 2 deletions packages/ivi/src/vdom/component.ts
Expand Up @@ -58,12 +58,12 @@ export abstract class Component<P = undefined> {
}

/**
* Lifecycle method `newPropsReceived()` is invoked after new props are assigned.
* Lifecycle method `propsChanged()` when props are changed.
*
* @param prev - Previous properties
* @param next - Next properties
*/
newPropsReceived(prev: P, next: P): void {
propsChanged(prev: P, next: P): void {
/* tslint:disable:no-empty */
/* tslint:enable:no-empty */
}
Expand Down
5 changes: 1 addition & 4 deletions packages/ivi/src/vdom/implementation.ts
Expand Up @@ -515,11 +515,8 @@ export function syncVNode(
}
}
} else { // VNodeFlags.StatefulComponent
// Update component props
if (aProps !== bProps) {
// There is no reason to call `newPropsReceived` when props aren't changed, even when they are
// reassigned later to reduce memory usage.
(instance as Component<any>).newPropsReceived(aProps, bProps);
(instance as Component<any>).propsChanged(aProps, bProps);
}
// Reassign props even when they aren't changed to reduce overall memory usage.
//
Expand Down

0 comments on commit 6434f5a

Please sign in to comment.