Skip to content

Commit 9d3e43a

Browse files
committed
feat: make spinner API async to allow consumers to wait for spinner process ack
1 parent ede072a commit 9d3e43a

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/spinner.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,41 @@ export default {
1414
/**
1515
* Start the spinner with the given message
1616
*/
17-
start(msg: string, id?: string) {
18-
queue('start', msg, id);
17+
async start(msg: string, id?: string) {
18+
await queue('start', msg, id);
1919
},
2020
/**
2121
* Stop the spinner, replace the spinner graphic with a checkmark, optionally update the message,
2222
* and turn it green.
2323
*/
24-
succeed(msg?: string, id?: string) {
25-
queue('succeed', msg, id);
24+
async succeed(msg?: string, id?: string) {
25+
await queue('succeed', msg, id);
2626
},
2727
/**
2828
* Stop the spinner, replace the spinner graphic with an X, optionally update the message, and
2929
* turn it red.
3030
*/
31-
fail(msg?: string, id?: string) {
32-
queue('fail', msg, id);
31+
async fail(msg?: string, id?: string) {
32+
await queue('fail', msg, id);
3333
},
3434
/**
3535
* Stop the spinner, replace the spinner graphic with the supplied symbol and message with the
3636
* supplied text.
3737
*/
38-
finish(symbol: string, text: string, id?: string) {
39-
queue('finish', symbol, text, id);
38+
async finish(symbol: string, text: string, id?: string) {
39+
await queue('finish', symbol, text, id);
4040
}
4141
};
4242

43-
let operationsQueue: { operation: string, args: any[] }[] = [];
43+
let operationsQueue: { operation: string, args: any[], done(): void }[] = [];
4444
let inFlight = false;
4545
let spinnerIsActive = false;
4646

47-
function queue(operation: string, ...args: any[]): void {
48-
operationsQueue.push({ operation, args });
49-
flushQueue();
47+
async function queue(operation: string, ...args: any[]): Promise<void> {
48+
return new Promise<void>((done) => {
49+
operationsQueue.push({ done, operation, args });
50+
flushQueue();
51+
});
5052
}
5153

5254
function flushQueue() {
@@ -67,6 +69,7 @@ function flushQueue() {
6769
if (nextOperation.operation !== 'start') {
6870
spinnerIsActive = false;
6971
}
72+
nextOperation.done();
7073
flushQueue();
7174
});
7275
}

0 commit comments

Comments
 (0)