Skip to content

Commit

Permalink
updated: removed unused vars and other micro optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Sep 5, 2016
1 parent c05f6d8 commit fa13f0f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
16 changes: 10 additions & 6 deletions lib/browser/tag/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ function moveNestedTags(child, i) {
* @param { Object } patch - all the data needed to handle the move
*/
function move(patch) {
var { i, tags, isVirtual, tag, root, prevBase } = patch
var { i, tags, isVirtual, tag, root, prevBase, item } = patch

// update the DOM
// update the tag...
tag.update(item)

// and move it
if (isVirtual)
moveVirtual(tag, root, tags[i])
else if (tags[i].root.parentNode)
Expand Down Expand Up @@ -239,7 +242,7 @@ export default function _each(dom, parent, expr) {
if (pos !== i && _mustReorder) {
patches.push({
type: MOVE,
i, tags, isVirtual, tag, dom, frag, root, prevBase
item, i, tags, isVirtual, tag, dom, frag, root, prevBase
})

// update the position attribute if it exists
Expand All @@ -263,13 +266,14 @@ export default function _each(dom, parent, expr) {
// update the DOM
each(patches, (patch) => {
switch (patch.type) {
case MOVE:
move(patch)
break
case INSERT:
insert(patch)
break
case MOVE:
move(patch)
break
default:
// just update (patch.type === UPDATE)
patch.tag.update(patch.item)
}
})
Expand Down
23 changes: 12 additions & 11 deletions lib/browser/tag/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ export default function Tag(impl, conf, innerHTML) {
// not yet mounted
this.isMounted = false
root.isLoop = isLoop
this._internal = {

defineProperty(this, '_internal', {
anonymous: anonymous,
origAttrs: instAttrs,
innerHTML: innerHTML,
eventHandlers: {},
// these vars will be needed only for the virtual tags
virts: [],
tail: null,
head: null
}
})

// create a unique id to this tag
// it could be handy to use it also to improve the virtual dom rendering speed
Expand Down Expand Up @@ -194,6 +196,8 @@ export default function Tag(impl, conf, innerHTML) {
defineProperty(this, 'mount', function tagMount(forceUpdate) {
root._tag = this // keep a reference to the tag just created

this.trigger('before-mount')

// add global mixins
var globalMixin = mixin(GLOBAL_MIXIN)

Expand Down Expand Up @@ -230,9 +234,6 @@ export default function Tag(impl, conf, innerHTML) {

this.update(item)

// internal use only, fixes #403
this.trigger('before-mount')

if (isLoop && anonymous) {
// update the root attribute for the looped elements
this.root = root = dom.firstChild
Expand Down Expand Up @@ -263,6 +264,11 @@ export default function Tag(impl, conf, innerHTML) {

this.trigger('before-unmount')

// unbind all the events
each(Object.keys(this._internal.eventHandlers), (eventName) => {
this.root.removeEventListener(eventName, this._internal.eventHandlers[eventName])
})

// remove this tag instance from the global virtualDom variable
if (~tagIndex)
__VIRTUAL_DOM.splice(tagIndex, 1)
Expand Down Expand Up @@ -298,13 +304,8 @@ export default function Tag(impl, conf, innerHTML) {
this.trigger('unmount')
this.off('*')
this.isMounted = false

delete this.root._tag

if (this.root._eventHandlers) {
each(Object.keys(this.root._eventHandlers), (eventName) => {
this.root.removeEventListener(eventName, this.root._eventHandlers[eventName])
})
delete this.root._eventHandlers
}
})
}
15 changes: 9 additions & 6 deletions lib/browser/tag/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
IE_VERSION
} from './../common/global-variables'

const EVENTS_PREFIX_REGEX = /^on/

/**
* Attach an event to a DOM node
* @param { String } name - event name
Expand All @@ -27,6 +29,8 @@ import {
* @param { Tag } tag - tag instance
*/
export function setEventHandler(name, handler, dom, tag) {
var eventName

var handleEvent = function(e) {
var ptag = tag._parent,
item = tag._item
Expand Down Expand Up @@ -60,14 +64,13 @@ export function setEventHandler(name, handler, dom, tag) {
return
}

var eventName = name.replace(/^on/, '')

if (dom._eventHandlers && dom._eventHandlers[eventName])
dom.removeEventListener(eventName, dom._eventHandlers[eventName])
// normalize event name
eventName = name.replace(EVENTS_PREFIX_REGEX, '')

if (!dom._eventHandlers) dom._eventHandlers = {}
if (tag._internal.eventHandlers[eventName])
dom.removeEventListener(eventName, tag._internal.eventHandlers[eventName])

dom._eventHandlers[eventName] = handleEvent
tag._internal.eventHandlers[eventName] = handleEvent
dom.addEventListener(eventName, handleEvent, false)
}

Expand Down

0 comments on commit fa13f0f

Please sign in to comment.