From f5f8d01c0baa76d5ff77505b446476f8f56fa71a Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Tue, 5 Jul 2016 12:14:29 +0200 Subject: [PATCH] index: add raf guard --- index.js | 11 +++++++++-- package.json | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5c140f69..bb7974d9 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const href = require('sheet-router/href') const hash = require('sheet-router/hash') const hashMatch = require('hash-match') const barracks = require('barracks') +const nanoraf = require('nanoraf') const assert = require('assert') const xtend = require('xtend') const yo = require('yo-yo') @@ -22,6 +23,7 @@ function choo (opts) { var _defaultRoute = null var _rootNode = null var _routes = null + var _frame = null start.toString = toString start.router = router @@ -85,8 +87,13 @@ function choo (opts) { opts.onStateChange(data, state, prev, name, createSend) } - const newTree = _router(state.location.pathname, state, prev) - _rootNode = yo.update(_rootNode, newTree) + if (!_frame) { + _frame = nanoraf(function (state, prev) { + const newTree = _router(state.location.pathname, state, prev) + _rootNode = yo.update(_rootNode, newTree) + }) + } + _frame(state, prev) } // register all routes on the router diff --git a/package.json b/package.json index 8035b1de..85abb3b6 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "document-ready": "~1.0.2", "global": "^4.3.0", "hash-match": "^1.0.2", + "nanoraf": "^2.0.0", "sheet-router": "^3.1.0", "xhr": "^2.2.0", "xtend": "^4.0.1",