Skip to content

Commit cf83fd2

Browse files
committed
Refactor queue, this, and others
1 parent da824fd commit cf83fd2

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $nprogress-color: #29d;
4141
</details>
4242

4343
<details>
44-
<summary>CDN via jsdelivr</summary>
44+
<summary>UMD build (via CDN)</summary>
4545

4646
```html
4747
<script src="nprogress.js"></script>
@@ -50,7 +50,7 @@ $nprogress-color: #29d;
5050

5151
Also available via [jsdelivr] CDN:
5252

53-
- https://cdn.jsdelivr.net/npm/nprogress@next/dist/nprogress.js
53+
- https://cdn.jsdelivr.net/npm/nprogress@next/dist/nprogress.umd.js
5454
- https://cdn.jsdelivr.net/npm/nprogress@next/css/nprogress.css
5555

5656
</details>

src/nprogress.js

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { removeElement, addClass, removeClass } from "./dom";
2+
import { queue } from "./queue";
23
import {
34
toBarPerc,
45
barPositionCSS,
@@ -58,7 +59,7 @@ function configure(options) {
5859
Settings[key] = value;
5960
}
6061

61-
return this;
62+
return NProgress;
6263
}
6364

6465
/**
@@ -75,7 +76,7 @@ function set(n) {
7576
n = clamp(n, Settings.minimum, 1);
7677
NProgress.status = n === 1 ? null : n;
7778

78-
var progress = NProgress.render(!started),
79+
var progress = render(!started),
7980
bar = progress.querySelector(Settings.barSelector),
8081
speed = Settings.speed,
8182
ease = Settings.easing;
@@ -116,7 +117,7 @@ function set(n) {
116117
}
117118
});
118119

119-
return this;
120+
return NProgress;
120121
}
121122

122123
function isStarted() {
@@ -144,7 +145,7 @@ NProgress.start = function () {
144145

145146
if (Settings.trickle) work();
146147

147-
return this;
148+
return NProgress;
148149
};
149150

150151
/**
@@ -162,7 +163,7 @@ NProgress.start = function () {
162163
*/
163164

164165
NProgress.done = function (force) {
165-
if (!force && !NProgress.status) return this;
166+
if (!force && !NProgress.status) return NProgress;
166167

167168
inc(0.3 + 0.5 * Math.random());
168169
set(1);
@@ -221,9 +222,11 @@ function getParent() {
221222
/**
222223
* (Internal) renders the progress bar markup based on the `template`
223224
* setting.
225+
*
226+
* @param {boolean=} fromStart If true, then it will reset to 0% before starting
224227
*/
225228

226-
NProgress.render = function (fromStart) {
229+
function render(fromStart) {
227230
if (isRendered()) return document.getElementById("nprogress");
228231

229232
addClass(document.documentElement, "nprogress-busy");
@@ -261,7 +264,7 @@ NProgress.render = function (fromStart) {
261264

262265
parent.appendChild(progress);
263266
return progress;
264-
};
267+
}
265268

266269
/**
267270
* Removes the element. Opposite of render().
@@ -286,32 +289,13 @@ function isRendered() {
286289
return !!document.getElementById("nprogress");
287290
}
288291

289-
/**
290-
* (Internal) Queues a function to be executed.
291-
*/
292-
293-
var queue = (function () {
294-
var pending = [];
295-
296-
function next() {
297-
var fn = pending.shift();
298-
if (fn) {
299-
fn(next);
300-
}
301-
}
302-
303-
return function (fn) {
304-
pending.push(fn);
305-
if (pending.length == 1) next();
306-
};
307-
})();
308-
309292
// Default export for commonjs / import NProgress
310293
NProgress.configure = configure;
311294
NProgress.inc = inc;
312295
NProgress.isRendered = isRendered;
313296
NProgress.isStarted = isStarted;
314297
NProgress.remove = remove;
298+
NProgress.render = render;
315299
NProgress.set = set;
316300
NProgress.settings = Settings;
317301
NProgress.trickle = inc;
@@ -324,6 +308,7 @@ export {
324308
isRendered,
325309
isStarted,
326310
remove,
311+
render,
327312
set,
328313
Settings as settings,
329314
};

src/queue.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let pending = [];
2+
3+
/**
4+
* (Internal) Queues up an action. Doesn't execute actions unless others have
5+
* finished.
6+
*
7+
* @param {(() => any) => void} fn
8+
*/
9+
10+
export function queue(fn) {
11+
pending.push(fn);
12+
if (pending.length == 1) next();
13+
}
14+
15+
function next() {
16+
var fn = pending.shift();
17+
if (fn) {
18+
fn(next);
19+
}
20+
}

0 commit comments

Comments
 (0)