Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ whenever `pushState` or `replaceState` is called by default.

- `Pace.ignore`: Explicitly ignore one or more requests, see Tracking below

And these properties:

- `Pace.running`: Whether a run is in progress, from `Pace.start()` until the bar is hidden or `Pace.stop()`. Read-only:
assigning to it does nothing, or throws a TypeError in strict-mode code such as modules.

- `Pace.version`: The version of pace.js, such as `'2.0.0'`.

If a page loads pace.js again after it's already there (a second script tag or bundle, or after Pace 1.x), the later
copy only logs a warning and the first one keeps running. Pace 1.x loaded after this version can't tell, so load only
one copy.

Events
------

Expand Down
100 changes: 72 additions & 28 deletions pace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,46 @@
* Licensed MIT © HubSpot, Inc. and Zachary Miller
*/
(function () {
const VERSION = '1.3.1';

// Hands `value` to a module loader. False when pace was loaded by a <script>.
const exportPace = (value) => {
if (typeof define === 'function' && define.amd) {
define(() => value);
return true;
}
if (typeof exports === 'object') {
module.exports = value;
return true;
}
return false;
};

// Where the first copy of pace on a page keeps its Pace object, for a later
// copy (another script tag or bundle) to find. Page markup can't replace it:
// element names are strings.
const FIRST_COPY = Symbol.for('pace-js');

// Pace 1.x doesn't set FIRST_COPY, but puts an object with a start function on
// window.Pace. Reading from a cross-origin frame named Pace throws.
const olderPace = () => {
try {
return typeof window.Pace?.start === 'function' ? window.Pace : undefined;
} catch {
return undefined;
}
};

const firstCopy = window[FIRST_COPY] ?? olderPace();

// A later copy would wrap requests and history again and add a second bar.
if (firstCopy) {
const firstVersion = typeof firstCopy.version === 'string' ? ` (${firstCopy.version})` : '';
console.warn(`Pace: pace.js ${VERSION} was loaded after another copy${firstVersion}; only the first copy runs.`);
exportPace(firstCopy);
return;
}

// The longest delay a timer can wait: setTimeout treats more, and Infinity, as 0.
const MAX_DELAY = 2147483647;

Expand Down Expand Up @@ -185,18 +225,28 @@
return sum / count;
};

const Pace = window.Pace || {};
const Pace = {};

window.Pace = Pace;

// Whether a run is waiting, running or finishing; Pace.running reads it.
let running = false;

Object.defineProperties(Pace, {
version: { value: VERSION, enumerable: true },
running: { get: () => running, enumerable: true },
});

// The handlers of each event, from Pace.on and Pace.once.
const bindings = Object.create(null);

// Removes the handlers of `event` that match `test`, and marks them so an
// event that is firing right now skips them too.
const removeBindings = (target, event, test) => {
const bindings = target.bindings?.[event];
if (!bindings) {
const removeBindings = (event, test) => {
if (!bindings[event]) {
return;
}
target.bindings[event] = bindings.filter((binding) => {
bindings[event] = bindings[event].filter((binding) => {
if (!test(binding)) {
return true;
}
Expand Down Expand Up @@ -226,33 +276,31 @@
Object.assign(Pace, {
on(event, handler, ctx, once) {
once ??= false;
this.bindings ??= Object.create(null);
this.bindings[event] ??= [];
this.bindings[event].push({ handler, ctx, once });
bindings[event] ??= [];
bindings[event].push({ handler, ctx, once });
},

once(event, handler, ctx) {
this.on(event, handler, ctx, true);
Pace.on(event, handler, ctx, true);
},

off(event, handler) {
removeBindings(this, event, (binding) => handler == null || binding.handler === handler);
removeBindings(event, (binding) => handler == null || binding.handler === handler);
},

trigger(event, ...args) {
const bindings = this.bindings?.[event];
if (!bindings) {
if (!bindings[event]) {
return;
}
for (const binding of bindings.slice()) {
for (const binding of bindings[event].slice()) {
if (binding.removed) {
continue;
}
if (binding.once) {
removeBindings(this, event, (other) => other === binding);
removeBindings(event, (other) => other === binding);
}
try {
binding.handler.apply(binding.ctx ?? this, args);
binding.handler.apply(binding.ctx ?? Pace, args);
} catch (error) {
reportException(error);
}
Expand Down Expand Up @@ -928,10 +976,8 @@
let run = null;

const ajaxMonitor = () => run?.collectors.find((collector) => collector instanceof AjaxMonitor);
let bar = (Pace.bar = new Bar(copyOptions(options)));
let bar = new Bar(copyOptions(options));

Pace.sources = [];
Pace.running = false;

// Waiting or running: a new request joins it instead of restarting the bar.
const isActive = () => run != null && (run.state === 'waiting' || run.state === 'running');
Expand Down Expand Up @@ -999,7 +1045,7 @@
const shownFor = now() - current.startedAt;
current.hideTimer = setTimeout(() => {
run = null;
Pace.running = false;
running = false;
bar.finish();
Pace.trigger('hide');
}, Math.max(current.settings.hideDelay, current.settings.minVisible - shownFor, 0));
Expand Down Expand Up @@ -1098,9 +1144,8 @@
throw error;
}
run = current;
Pace.running = true;
Pace.bar = bar = new Bar(settings);
Pace.sources = current.sources;
running = true;
bar = new Bar(settings);
const show = () => {
if (run !== current) {
return;
Expand Down Expand Up @@ -1144,7 +1189,7 @@

Pace.stop = () => {
endRun();
Pace.running = false;
running = false;
document.body.classList.remove('pace-running');
document.body.classList.add('pace-done');
Pace.trigger('stop');
Expand All @@ -1162,11 +1207,10 @@
}
};

if (typeof define === 'function' && define.amd) {
define(() => Pace);
} else if (typeof exports === 'object') {
module.exports = Pace;
} else if (options.startOnPageLoad) {
// Only now, when it's complete, can a later copy use this one.
Object.defineProperty(window, FIRST_COPY, { value: Pace });

if (!exportPace(Pace) && options.startOnPageLoad) {
Pace.start();
}
})();
Loading
Loading