Skip to content

Commit

Permalink
Merge m.mount + m.redraw, update router
Browse files Browse the repository at this point in the history
Simplifies the router and redraw mechanism, and makes it much easier to
keep predictable.

Bundle size down to 9433 bytes min+gzip, docs updated accordingly.
  • Loading branch information
dead-claudia committed Jul 6, 2019
1 parent 9b1b429 commit 755ddb1
Show file tree
Hide file tree
Showing 20 changed files with 512 additions and 656 deletions.
52 changes: 52 additions & 0 deletions api/mount-redraw.js
@@ -0,0 +1,52 @@
"use strict"

var Vnode = require("../render/vnode")
var coreRenderer = require("../render/render")

module.exports = function($window, schedule, console) {
var renderService = coreRenderer($window)
var subscriptions = []
var rendering = false
var pending = false

function sync() {
if (rendering) throw new Error("Nested m.redraw.sync() call")
rendering = true
for (var i = 0; i < subscriptions.length; i += 2) {
try { renderService.render(subscriptions[i], Vnode(subscriptions[i + 1])) }
catch (e) { console.error(e) }
}
rendering = false
}
function redraw() {
if (!pending) {
pending = true
schedule(function() {
pending = false
sync()
})
}
}

redraw.sync = sync
renderService.setRedraw(redraw)

function mount(root, component) {
if (component != null && component.view == null && typeof component !== "function") {
throw new TypeError("m.mount(element, component) expects a component, not a vnode")
}

var index = subscriptions.indexOf(root)
if (index >= 0) {
subscriptions.splice(index, 2)
renderService.render(root, [])
}

if (component != null) {
subscriptions.push(root, component)
renderService.render(root, Vnode(component))
}
}

return {mount: mount, redraw: redraw}
}
15 changes: 0 additions & 15 deletions api/mount.js

This file was deleted.

58 changes: 0 additions & 58 deletions api/redraw.js

This file was deleted.

8 changes: 4 additions & 4 deletions api/router.js
Expand Up @@ -10,7 +10,7 @@ var assign = require("../pathname/assign")

var sentinel = {}

module.exports = function($window, redrawService, mount) {
module.exports = function($window, mountRedraw) {
var callAsync = typeof setImmediate === "function" ? setImmediate : setTimeout
var supportsPushState = typeof $window.history.pushState === "function"
var routePrefix = "#!"
Expand Down Expand Up @@ -91,10 +91,10 @@ module.exports = function($window, redrawService, mount) {
component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div"
attrs = data.params, currentPath = path, lastUpdate = null
currentResolver = routeResolver.render ? routeResolver : null
if (state === 2) redrawService.redraw()
if (state === 2) mountRedraw.redraw()
else {
state = 2
redrawService.redraw.sync()
mountRedraw.redraw.sync()
}
}
if (payload.view || typeof payload === "function") update({}, payload)
Expand Down Expand Up @@ -135,7 +135,7 @@ module.exports = function($window, redrawService, mount) {
$window.addEventListener("hashchange", resolveRoute, false)
}

return mount(root, {
return mountRedraw.mount(root, {
onbeforeupdate: function() {
state = state ? 2 : 1
return !(!state || sentinel === currentResolver)
Expand Down
6 changes: 2 additions & 4 deletions api/tests/index.html
Expand Up @@ -24,11 +24,9 @@
<script src="../../querystring/build.js"></script>
<script src="../../querystring/parse.js"></script>
<script src="../../request/request.js"></script>
<script src="../../api/redraw.js"></script>
<script src="../../api/mount.js"></script>
<script src="../../api/mount-redraw.js"></script>
<script src="../../api/router.js"></script>
<script src="./test-redraw.js"></script>
<script src="./test-mount.js"></script>
<script src="./test-mountRedraw.js"></script>
<script src="./test-router.js"></script>
<script src="./test-routerGetSet.js"></script>

Expand Down

0 comments on commit 755ddb1

Please sign in to comment.