Skip to content

Commit

Permalink
fix(engine): isBeingConstructed flag is not get out of sync (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
caridy authored May 7, 2018
1 parent 8bf1dd5 commit 888bd0d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
12 changes: 0 additions & 12 deletions packages/lwc-engine/src/framework/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,12 @@ export interface ComponentConstructor {
readonly __circular__?: any;
}

export let vmBeingConstructed: VM | null = null;
export function isBeingConstructed(vm: VM): boolean {
if (process.env.NODE_ENV !== 'production') {
assert.vm(vm);
}
return vmBeingConstructed === vm;
}

export function createComponent(vm: VM, Ctor: ComponentConstructor) {
if (process.env.NODE_ENV !== 'production') {
assert.vm(vm);
}
// create the component instance
const vmBeingConstructedInception = vmBeingConstructed;
vmBeingConstructed = vm;

const component = invokeComponentConstructor(vm, Ctor);
vmBeingConstructed = vmBeingConstructedInception;

if (process.env.NODE_ENV !== 'production') {
assert.isTrue(vm.component === component, `Invalid construction for ${vm}, maybe you are missing the call to super() on classes extending Element.`);
Expand Down
4 changes: 2 additions & 2 deletions packages/lwc-engine/src/framework/decorators/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from "../assert";
import { defineProperty, isObject, isNull, isTrue } from "../language";
import { isRendering, vmBeingRendered } from "../invoker";
import { isRendering, vmBeingRendered, isBeingConstructed } from "../invoker";
import { observeMutation, notifyMutation } from "../watcher";
import { isBeingConstructed, Component } from "../component";
import { Component } from "../component";
import { VM } from "../vm";
import { getCustomElementVM } from "../html-element";
import { isUndefined, isFunction } from "../language";
Expand Down
4 changes: 2 additions & 2 deletions packages/lwc-engine/src/framework/html-element.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "./assert";
import { Root, shadowRootQuerySelector, shadowRootQuerySelectorAll, ShadowRoot } from "./root";
import { vmBeingConstructed, isBeingConstructed, Component } from "./component";
import { Component } from "./component";
import { isObject, ArrayFilter, freeze, seal, defineProperty, defineProperties, getOwnPropertyNames, isUndefined, ArraySlice, isNull, forEach } from "./language";
import {
getGlobalHTMLPropertiesInfo,
Expand All @@ -15,7 +15,7 @@ import {
CustomEvent,
} from "./dom";
import { getPropNameFromAttrName } from "./utils";
import { isRendering, vmBeingRendered } from "./invoker";
import { vmBeingConstructed, isBeingConstructed, isRendering, vmBeingRendered } from "./invoker";
import { wasNodePassedIntoVM, VM } from "./vm";
import { pierce, piercingHook } from "./piercing";
import { ViewModelReflection } from "./def";
Expand Down
11 changes: 11 additions & 0 deletions packages/lwc-engine/src/framework/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import { startMeasure, endMeasure } from "./performance-timing";
export let isRendering: boolean = false;
export let vmBeingRendered: VM|null = null;

export let vmBeingConstructed: VM | null = null;
export function isBeingConstructed(vm: VM): boolean {
if (process.env.NODE_ENV !== 'production') {
assert.vm(vm);
}
return vmBeingConstructed === vm;
}

export function invokeComponentCallback(vm: VM, fn: (...args: any[]) => any, args?: any[]): any {
const { context, component } = vm;
const ctx = currentContext;
Expand Down Expand Up @@ -61,6 +69,8 @@ export function invokeComponentConstructor(vm: VM, Ctor: ComponentConstructor):
const { context } = vm;
const ctx = currentContext;
establishContext(context);
const vmBeingConstructedInception = vmBeingConstructed;
vmBeingConstructed = vm;

if (process.env.NODE_ENV !== 'production') {
startMeasure(vm, 'constructor');
Expand All @@ -78,6 +88,7 @@ export function invokeComponentConstructor(vm: VM, Ctor: ComponentConstructor):
}

establishContext(ctx);
vmBeingConstructed = vmBeingConstructedInception;
if (error) {
error.wcStack = getComponentStack(vm);
// rethrowing the original error annotated after restoring the context
Expand Down
4 changes: 2 additions & 2 deletions packages/lwc-engine/src/framework/root.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "./assert";
import { ViewModelReflection, ComponentDef } from "./def";
import { isUndefined, ArrayFilter, defineProperty, isNull, defineProperties, create, getOwnPropertyNames, forEach, hasOwnProperty, ArrayIndexOf, ArraySplice, ArrayPush, isFunction, isFalse } from "./language";
import { isBeingConstructed, getCustomElementComponent } from "./component";
import { getCustomElementComponent } from "./component";
import { OwnerKey, isNodeOwnedByVM, VM } from "./vm";
import { register } from "./services";
import { pierce, piercingHook } from "./piercing";
Expand All @@ -25,7 +25,7 @@ import {
removeEventListener,
} from './dom';
import { getAttrNameFromPropName } from "./utils";
import { invokeRootCallback, isRendering, vmBeingRendered } from "./invoker";
import { invokeRootCallback, isRendering, vmBeingRendered, isBeingConstructed } from "./invoker";

function getLinkedElement(root: ShadowRoot): HTMLElement {
return getCustomElementVM(root).elm;
Expand Down

0 comments on commit 888bd0d

Please sign in to comment.