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

added debouncing to render(): #6

Merged
merged 3 commits into from
Jul 25, 2020
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
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