Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle re-entrance
  • Loading branch information
buunguyen committed Dec 30, 2019
1 parent 0530bf6 commit 7ef2485
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
3 changes: 1 addition & 2 deletions .eslintrc
Expand Up @@ -20,7 +20,6 @@
}
],
"object-curly-spacing": ["error", "never"],
"array-bracket-spacing": ["error", "never"],
"capitalized-comments": ["error"]
"array-bracket-spacing": ["error", "never"]
}
}
40 changes: 26 additions & 14 deletions src/adapters/pjax.js
Expand Up @@ -5,12 +5,16 @@ class PjaxAdapter extends Adapter {

$(document)
.on('pjax:start', () => {
$(document).trigger(EVENT.REQ_START);
this._dispatchPjaxEventInDom('pjax:start');
if (!this._isDispatching) {
$(document).trigger(EVENT.REQ_START);
this._dispatchPjaxEventInDom('pjax:start');
}
})
.on('pjax:end', () => {
$(document).trigger(EVENT.REQ_END);
this._dispatchPjaxEventInDom('pjax:end');
if (!this._isDispatching) {
$(document).trigger(EVENT.REQ_END);
this._dispatchPjaxEventInDom('pjax:end');
}
})
.on('pjax:timeout', (e) => e.preventDefault());
}
Expand Down Expand Up @@ -93,10 +97,10 @@ class PjaxAdapter extends Adapter {
* Dispatches a pjax event directly in the DOM.
*
* GitHub's own pjax implementation dispatches its events directly in the DOM, while
* the jQuery pjax library we use dispatches its events only within its jQuery instance.
* Because some GitHub add-ons listen to certain pjax events in the DOM it may be
* necessary to forward an event from jQuery to the DOM to make sure those add-ons
* don't break.
* the jQuery pjax library we use dispatches its events (during selectFile()) only
* within its jQuery instance. Because some GitHub add-ons listen to certain pjax events
* in the DOM it may be necessary to forward an event from jQuery to the DOM to make sure
* those add-ons don't break.
*
* Note that we don't forward the details/extra parameters or whether they're cancellable,
* because the pjax implementations differ in this case!
Expand All @@ -107,12 +111,20 @@ class PjaxAdapter extends Adapter {
* @api protected
*/
_dispatchPjaxEventInDom(type) {
const pjaxContainer = $(this._pjaxContainerSel)[0];

if (pjaxContainer) {
pjaxContainer.dispatchEvent(new Event(type, {
bubbles: true
}));
// Avoid re-entrance as dispatching on native DOM will trigger the jQuery Pjax event we're listening.
// NOTE: anywhere in the source code that listens to pjax:start pjax:end should hanlde reentrance.
this._isDispatching = true;
try {
const pjaxContainer = $(this._pjaxContainerSel)[0];
if (pjaxContainer) {
pjaxContainer.dispatchEvent(new Event(type, {
bubbles: true
}));
}
} catch (e) {
// Nothing we can do
} finally {
this._isDispatching = false;
}
}

Expand Down

0 comments on commit 7ef2485

Please sign in to comment.