Fix async switches #110

Merged
BehindTheMath merged 3 commits from fix/async-switches into master 2018-01-22 10:55:30 -05:00
4 changed files with 19 additions and 12 deletions
Showing only changes of commit ff6f9b36ca - Show all commits

View File

@@ -229,13 +229,15 @@ Pjax.prototype = {
// Update browser history // Update browser history
this.lastUid = this.maxUid = newUid() this.lastUid = this.maxUid = newUid()
var state = this.state
window.history.pushState({ window.history.pushState({
url: this.state.href, url: state.href,
title: this.state.options.title, title: state.options.title,
uid: this.maxUid uid: this.maxUid
}, },
this.state.options.title, state.options.title,
this.state.href) state.href)
} }
this.forEachSelectors(function(el) { this.forEachSelectors(function(el) {

View File

@@ -3,6 +3,8 @@ var forEachEls = require("./foreach-els")
var defaultSwitches = require("./switches") var defaultSwitches = require("./switches")
module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, options) { module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, options) {
var switchesQueue = [];
selectors.forEach(function(selector) { selectors.forEach(function(selector) {
var newEls = fromEl.querySelectorAll(selector) var newEls = fromEl.querySelectorAll(selector)
var oldEls = toEl.querySelectorAll(selector) var oldEls = toEl.querySelectorAll(selector)
@@ -25,14 +27,17 @@ module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, op
this.log("newEl", newEl, "oldEl", oldEl) this.log("newEl", newEl, "oldEl", oldEl)
} }
this.state.numPendingSwitches++ var callback = (switches[selector]) ?
switches[selector].bind(this, oldEl, newEl, options, switchesOptions[selector]) :
defaultSwitches.outerHTML.bind(this, oldEl, newEl, options)
if (switches[selector]) { switchesQueue.push(callback)
switches[selector].bind(this)(oldEl, newEl, options, switchesOptions[selector])
}
else {
defaultSwitches.outerHTML.bind(this)(oldEl, newEl, options)
}
}, this) }, this)
}, this) }, this)
this.state.numPendingSwitches = switchesQueue.length
switchesQueue.forEach(function(queuedSwitch) {
queuedSwitch()
})
} }

View File

@@ -38,7 +38,7 @@
"lint": "jscs . && jshint . --exclude-path .gitignore", "lint": "jscs . && jshint . --exclude-path .gitignore",
"standalone": "browserify index.js --standalone Pjax > pjax.js", "standalone": "browserify index.js --standalone Pjax > pjax.js",
"build-debug": "browserify index.js --debug --standalone Pjax > pjax.js", "build-debug": "browserify index.js --debug --standalone Pjax > pjax.js",
"tests": "tape -r ./tests/index.js ./tests/**/*.js", "tests": "tape -r ./tests/setup.js ./tests/**/*.js",
"test": "npm run lint && npm run tests | tap-spec", "test": "npm run lint && npm run tests | tap-spec",
"coverage-tests": "npm run tests | tap-nyc", "coverage-tests": "npm run tests | tap-nyc",
"coverage": "nyc -x \"tests/**\" npm run coverage-tests", "coverage": "nyc -x \"tests/**\" npm run coverage-tests",