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

Batch UI updates #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@cycle/history": "^1.1.0",
"@cycle/isolate": "1.2.x",
"@cycle/storage": "^2.0.3",
"raf": "^3.2.0",
"rx": "4.0.7",
"todomvc-app-css": "2.0.3",
"todomvc-common": "1.0.1"
Expand Down
3 changes: 2 additions & 1 deletion src/components/Todos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import view from './view';
import deserialize from './storage-source';
import serialize from './storage-sink';
import TodoItem from '../TodoItem';
import {batchUpdate} from '../../utils'

// AMEND STATE WITH CHILDREN
// This function creates the projection function
Expand Down Expand Up @@ -87,7 +88,7 @@ function Todos({DOM, History, storage}) {
// Write the virtual dom stream to the DOM and write the
// storage stream to localStorage.
return {
DOM: view(amendedState$),
DOM: batchUpdate(view(amendedState$)),
History: actions.url$,
storage: storage$,
};
Expand Down
15 changes: 14 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {Observable} from 'rx'
import raf from 'raf'

class PropertyHook {
constructor(fn) {
this.fn = fn;
Expand All @@ -15,4 +18,14 @@ function propHook(fn) {
const ENTER_KEY = 13;
const ESC_KEY = 27;

export {propHook, ENTER_KEY, ESC_KEY};
function batchUpdate (dom$) {
return dom$
.flatMapLatest(dom => Observable
.fromEventPattern(
callback => raf(() => callback(dom)),
(_, handler) => raf.cancel(handler)
).first()
)
}

export {propHook, batchUpdate, ENTER_KEY, ESC_KEY};