Skip to content

Commit

Permalink
added debouncing to render(): (#6)
Browse files Browse the repository at this point in the history
* added debouncing to render():
  - setState calls render()
  - render() only reads/writes to DOM at 60fps (even if setState called more often than that)
* updated README
  • Loading branch information
sc0ttj committed Jul 25, 2020
2 parents 4d59a8c + 1625074 commit 61ca16a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,14 @@ Also see [examples/usage-tweenState.js](examples/usage-tweenState.js)

## Changelog

**1.1.10**
- better rendering performance: debounce the `render()` function:
- `setState()` can be called 1000s of times a second
- `setState()` also calls the render() function
- `render()` will only update the DOM at 60fps
- see https://gomakethings.com/debouncing-your-javascript-events/
- updated README

**1.1.9**
- new feature: "middleware"
- define an array of functions as `myComponent.middleware = [ someFunc, otherFunc ]`
Expand Down
2 changes: 1 addition & 1 deletion dist/component.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions examples/usage-in-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,13 @@
console.log('App.log')
console.log(App.log)

// Test debouncing of render():
// - call setState 10,000 times, fast as possible
// - setState calls render() each time
// - render() should only try to access/update DOM at 60fps
for (var i = 0; i < 10000; i++) {
App.plus(1)
}

</script>
</body>
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scottjarvis/component",
"version": "1.1.9",
"version": "1.1.10",
"description": "Simple component library with state management, DOM diffing, tweening, server-side rendering and more.",
"author": "sc0ttj",
"license": "MIT",
Expand Down
12 changes: 11 additions & 1 deletion src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// from https://codepen.io/tevko/pen/LzXjKE?editors=0010
var domDiff = (target, source) => {
//console.log("updating DOM!")
const worker = {
cfg: {
orig: target
Expand Down Expand Up @@ -116,6 +117,9 @@ function Component(state) {

var self = this

// for debouncing render() calls
var timeout

this.isNode =
typeof process !== "undefined" &&
process !== null &&
Expand Down Expand Up @@ -404,7 +408,13 @@ function Component(state) {
} else {
this.container = el
// only interact with the DOM once every animation frame (usually 60fps)
requestAnimationFrame(() => {

// If there's a timer, cancel it
if (timeout) cancelAnimationFrame(timeout)

// Setup the new requestAnimationFrame()
timeout = requestAnimationFrame(() => {
//console.log("debounced")
// get the container element if needed
if (typeof el === "string") el = document.querySelector(`${el}`)
this.container = el
Expand Down

0 comments on commit 61ca16a

Please sign in to comment.