Skip to content
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

add timings #47

Merged
merged 1 commit into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ to `.render` e.g. `.render({ foo, bar })` and assigning internal state to `this`
you want (perhaps `this.state = { fizz: buzz }`).

## API
### `component = Nanocomponent()`
### `component = Nanocomponent([name])`
Create a new Nanocomponent instance. Additional methods can be set on the
prototype.
prototype. Takes an optional name which is used when emitting timings.

### `component.render([arguments…])`
Render the component. Returns a proxy node if already mounted on the DOM. Proxy
Expand Down
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var document = require('global/document')
var nanotiming = require('nanotiming')
var morph = require('nanomorph')
var onload = require('on-load')
var assert = require('assert')
Expand All @@ -9,13 +10,14 @@ function makeID () {
return 'ncid-' + Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
}

function Nanocomponent () {
function Nanocomponent (name) {
this._hasWindow = typeof window !== 'undefined'
this._id = null // represents the id of the root node
this._ncID = null // internal nanocomponent id
this._proxy = null
this._loaded = false // Used to debounce on-load when child-reordering
this._rootNodeName = null
this._name = name || 'nanocomponent'

this._handleLoad = this._handleLoad.bind(this)
this._handleUnload = this._handleUnload.bind(this)
Expand All @@ -31,26 +33,32 @@ function Nanocomponent () {
}

Nanocomponent.prototype.render = function () {
var timing = nanotiming(this._name + '.render')
var self = this
var args = new Array(arguments.length)
var el
for (var i = 0; i < arguments.length; i++) args[i] = arguments[i]
if (!this._hasWindow) {
return this.createElement.apply(this, args)
el = this.createElement.apply(this, args)
timing()
return el
} else if (this.element) {
var shouldUpdate = this.update.apply(this, args)
if (shouldUpdate) {
morph(this.element, this._handleRender(args))
if (this.afterupdate) this.afterupdate(this.element)
}
if (!this._proxy) { this._proxy = this._createProxy() }
timing()
return this._proxy
} else {
this._reset()
var el = this._handleRender(args)
el = this._handleRender(args)
if (this.beforerender) this.beforerender(el)
if (this.load || this.unload) {
onload(el, self._handleLoad, self._handleUnload, self)
}
timing()
return el
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"dependencies": {
"global": "^4.3.1",
"nanomorph": "^5.1.2",
"nanotiming": "^6.1.3",
"on-load": "^3.2.0"
},
"devDependencies": {
Expand Down