-
Notifications
You must be signed in to change notification settings - Fork 392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(engine): abstraction for create/insert/remove/render to support CE #97
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import assert from "./assert"; | ||
import { isUndefined, isFunction, assign } from "./language"; | ||
import { isUndefined, isFunction, assign, hasOwnProperty } from "./language"; | ||
import { createVM, removeVM, appendVM, renderVM } from "./vm"; | ||
import { registerComponent, getCtorByTagName, prepareForAttributeMutationFromTemplate } from "./def"; | ||
import { registerComponent, getCtorByTagName, prepareForAttributeMutationFromTemplate, ViewModelReflection } from "./def"; | ||
import { ComponentConstructor } from "./component"; | ||
import { getCustomElementVM } from "./html-element"; | ||
|
||
|
@@ -58,19 +58,21 @@ export function createElement(sel: string, options: any = {}): HTMLElement { | |
throw new TypeError(); | ||
} | ||
registerComponent(sel, options.is); | ||
// extracing the registered constructor just in case we need to force the tagName | ||
// extracting the registered constructor just in case we need to force the tagName | ||
const Ctor = getCtorByTagName(sel); | ||
const { forceTagName } = Ctor as ComponentConstructor; | ||
const tagName = isUndefined(forceTagName) ? sel : forceTagName; | ||
// Create element with correct tagName | ||
const element = document.createElement(tagName); | ||
if (hasOwnProperty.call(element, ViewModelReflection)) { | ||
return element; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the element is registered somehow, then the VM will be attached to it, and we don't have to carry on the hacks to detection insertion and removal, as well as the initial creation of the VM because it was already created. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how can this occur if we are creating brand new element right here? |
||
} | ||
// In case the element is not initialized already, we need to carry on the manual creation | ||
createVM(sel, element); | ||
// Handle insertion and removal from the DOM | ||
// Handle insertion and removal from the DOM manually | ||
element[ConnectingSlot] = () => { | ||
const vm = getCustomElementVM(element); | ||
if (vm.idx > 0) { | ||
removeVM(vm); // moving the element from one place to another is observable via life-cycle hooks | ||
} | ||
removeVM(vm); // moving the element from one place to another is observable via life-cycle hooks | ||
appendVM(vm); | ||
// TODO: this is the kind of awkwardness introduced by "is" attribute | ||
// We don't want to do this during construction because it breaks another | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import assert from "./assert"; | |
import { getComponentDef } from "./def"; | ||
import { createComponent, linkComponent, renderComponent, clearReactiveListeners, ComponentConstructor, ErrorCallback } from "./component"; | ||
import { patchChildren } from "./patch"; | ||
import { ArrayPush, isUndefined, isNull, ArrayUnshift, ArraySlice, create } from "./language"; | ||
import { ArrayPush, isUndefined, isNull, ArrayUnshift, ArraySlice, create, hasOwnProperty } from "./language"; | ||
import { addCallbackToNextTick, EmptyObject, EmptyArray, usesNativeSymbols } from "./utils"; | ||
import { ViewModelReflection, getCtorByTagName } from "./def"; | ||
import { invokeServiceHook, Services } from "./services"; | ||
|
@@ -94,23 +94,28 @@ export function removeInsertionIndex(vm: VM) { | |
export function renderVM(vm: VM) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert.vm(vm); | ||
assert.isTrue(vm.isDirty, `${vm} must be dirty before renderVM is invoked.`); | ||
} | ||
rehydrate(vm); | ||
if (vm.isDirty) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of an assertion, it is just a runtime check. |
||
rehydrate(vm); | ||
} | ||
} | ||
|
||
export function appendVM(vm: VM) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert.vm(vm); | ||
assert.isTrue(vm.idx === 0, `${vm} is already inserted.`); | ||
} | ||
if (vm.idx !== 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of an assertion, it is just a runtime check. |
||
return; // already appended | ||
} | ||
addInsertionIndex(vm); | ||
} | ||
|
||
export function removeVM(vm: VM) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert.vm(vm); | ||
assert.isTrue(vm.idx > 0, `${vm} is not inserted.`); | ||
} | ||
if (vm.idx === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of an assertion, it is just a runtime check. |
||
return; // already removed | ||
} | ||
removeInsertionIndex(vm); | ||
// just in case it comes back, with this we guarantee re-rendering it | ||
|
@@ -128,6 +133,9 @@ export function createVM(tagName: string, elm: HTMLElement, cmpSlots?: Slotset) | |
if (process.env.NODE_ENV !== 'production') { | ||
assert.invariant(elm instanceof HTMLElement, `VM creation requires a DOM element instead of ${elm}.`); | ||
} | ||
if (hasOwnProperty.call(elm, ViewModelReflection)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the VM was already attached, somewhere else, there is nothing to do here. |
||
return; // already created | ||
} | ||
const Ctor = getCtorByTagName(tagName) as ComponentConstructor; | ||
const def = getComponentDef(Ctor); | ||
const isRoot = arguments.length === 2; // root elements can't provide slotset | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renderVM will take care of the check now.