dazscript-framework 1.0.30 → 1.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
const started: Array<{
|
|
4
|
+
info: string
|
|
5
|
+
totalSteps: number
|
|
6
|
+
isCancellable: boolean
|
|
7
|
+
showTimeElapsed: boolean
|
|
8
|
+
}> = []
|
|
9
|
+
const steps: number[] = []
|
|
10
|
+
|
|
11
|
+
const installProgressGlobals = (cancelled = false): void => {
|
|
12
|
+
vi.stubGlobal('startProgress', (info: string, totalSteps: number, isCancellable: boolean, showTimeElapsed: boolean) => {
|
|
13
|
+
started.push({ info, totalSteps, isCancellable, showTimeElapsed })
|
|
14
|
+
})
|
|
15
|
+
vi.stubGlobal('stepProgress', (count = 1) => {
|
|
16
|
+
steps.push(count)
|
|
17
|
+
})
|
|
18
|
+
vi.stubGlobal('finishProgress', vi.fn())
|
|
19
|
+
vi.stubGlobal('progressIsCancelled', () => cancelled)
|
|
20
|
+
vi.stubGlobal('processEvents', vi.fn())
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
started.length = 0
|
|
25
|
+
steps.length = 0
|
|
26
|
+
vi.resetModules()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
vi.unstubAllGlobals()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('withProgress', () => {
|
|
34
|
+
it('starts progress, exposes a manual step handle, and finishes after callback returns', async () => {
|
|
35
|
+
installProgressGlobals()
|
|
36
|
+
const { withProgress } = await import('./progress-helper')
|
|
37
|
+
|
|
38
|
+
const result = withProgress('Manual Work', 4, (progress) => {
|
|
39
|
+
progress.step()
|
|
40
|
+
progress.step(2)
|
|
41
|
+
return 'done'
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
expect(result).toBe('done')
|
|
45
|
+
expect(started).toEqual([
|
|
46
|
+
{
|
|
47
|
+
info: 'Manual Work',
|
|
48
|
+
totalSteps: 4,
|
|
49
|
+
isCancellable: true,
|
|
50
|
+
showTimeElapsed: true,
|
|
51
|
+
},
|
|
52
|
+
])
|
|
53
|
+
expect(steps).toEqual([1, 2])
|
|
54
|
+
expect(finishProgress).toHaveBeenCalledTimes(1)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('finishes progress when the callback throws', async () => {
|
|
58
|
+
installProgressGlobals()
|
|
59
|
+
const { withProgress } = await import('./progress-helper')
|
|
60
|
+
|
|
61
|
+
expect(() => withProgress('Manual Work', 2, () => {
|
|
62
|
+
throw new Error('failed phase')
|
|
63
|
+
})).toThrow('failed phase')
|
|
64
|
+
|
|
65
|
+
expect(finishProgress).toHaveBeenCalledTimes(1)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('reports cancellation consistently with cancellable progress', async () => {
|
|
69
|
+
installProgressGlobals(true)
|
|
70
|
+
const { withProgress } = await import('./progress-helper')
|
|
71
|
+
|
|
72
|
+
let callbackCancelled = false
|
|
73
|
+
withProgress('Manual Work', 2, (progress) => {
|
|
74
|
+
callbackCancelled = progress.isCancelled()
|
|
75
|
+
progress.step()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
expect(callbackCancelled).toBe(true)
|
|
79
|
+
expect(processEvents).toHaveBeenCalledTimes(1)
|
|
80
|
+
expect(steps).toEqual([])
|
|
81
|
+
expect(finishProgress).toHaveBeenCalledTimes(1)
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
describe('progress', () => {
|
|
86
|
+
it('finishes progress when an item callback stops the loop', async () => {
|
|
87
|
+
installProgressGlobals()
|
|
88
|
+
const { progress } = await import('./progress-helper')
|
|
89
|
+
|
|
90
|
+
progress('Array Work', [1, 2, 3], (item: number) => item !== 2)
|
|
91
|
+
|
|
92
|
+
expect(steps).toEqual([1])
|
|
93
|
+
expect(finishProgress).toHaveBeenCalledTimes(1)
|
|
94
|
+
})
|
|
95
|
+
})
|
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
import { status } from '@dsf/common/log';
|
|
2
2
|
|
|
3
|
+
export type ProgressOptions = {
|
|
4
|
+
isCancellable?: boolean
|
|
5
|
+
showTimeElapsed?: boolean
|
|
6
|
+
totalSteps?: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type ProgressHandle = {
|
|
10
|
+
step: (count?: number) => boolean
|
|
11
|
+
isCancelled: () => boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const defaultProgressOptions = (totalSteps: number, options?: ProgressOptions): Required<ProgressOptions> => ({
|
|
15
|
+
isCancellable: true,
|
|
16
|
+
showTimeElapsed: true,
|
|
17
|
+
totalSteps,
|
|
18
|
+
...options
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const reportInfo = (info: string | string[]): string => {
|
|
22
|
+
const infoMessage = Array.isArray(info) ? '' : info;
|
|
23
|
+
if (Array.isArray(info)) info.forEach(line => status(line));
|
|
24
|
+
return infoMessage;
|
|
25
|
+
}
|
|
26
|
+
|
|
3
27
|
/**
|
|
4
28
|
* Displays a progress dialog to the user if one is not already being displayed and starts a progress tracking operation.
|
|
5
29
|
* @param info The string to display in the progress dialog as the current description of the operation.
|
|
@@ -10,43 +34,79 @@ import { status } from '@dsf/common/log';
|
|
|
10
34
|
* @param totalSteps The number of progress steps for the operation to be complete.
|
|
11
35
|
*/
|
|
12
36
|
export const progress = <T>(info: string | string[], items: T[], callback: (item: T) => boolean | void,
|
|
13
|
-
options?:
|
|
37
|
+
options?: ProgressOptions) => {
|
|
14
38
|
|
|
15
|
-
|
|
16
|
-
isCancellable: true,
|
|
17
|
-
showTimeElapsed: true,
|
|
18
|
-
totalSteps: items.length,
|
|
19
|
-
...options
|
|
20
|
-
};
|
|
39
|
+
const resolvedOptions = defaultProgressOptions(items.length, options);
|
|
21
40
|
|
|
22
|
-
const infoMessage =
|
|
23
|
-
startProgress(infoMessage,
|
|
41
|
+
const infoMessage = reportInfo(info);
|
|
42
|
+
startProgress(infoMessage, resolvedOptions.totalSteps, resolvedOptions.isCancellable, resolvedOptions.showTimeElapsed);
|
|
24
43
|
|
|
25
|
-
|
|
44
|
+
try {
|
|
45
|
+
// const total = Math.max(1, options.totalSteps ?? items.length);
|
|
46
|
+
// let lastQuarter = -1; // -1 so first boundary triggers
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < items.length; i++) {
|
|
49
|
+
// cancellation
|
|
50
|
+
if (resolvedOptions.isCancellable && progressIsCancelled()) {
|
|
51
|
+
processEvents(); // flush once on cancel
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// work
|
|
56
|
+
if (callback && callback(items[i]) === false) break;
|
|
26
57
|
|
|
27
|
-
|
|
28
|
-
|
|
58
|
+
// progress
|
|
59
|
+
stepProgress(1);
|
|
29
60
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
processEvents();
|
|
34
|
-
|
|
61
|
+
// 25/50/75/100 checkpoints
|
|
62
|
+
// const quarter = Math.floor(((i + 1) * 2) / total); // 0..4
|
|
63
|
+
// if (quarter > lastQuarter) {
|
|
64
|
+
// processEvents();
|
|
65
|
+
// lastQuarter = quarter;
|
|
66
|
+
// }
|
|
35
67
|
}
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
finishProgress();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
36
73
|
|
|
37
|
-
|
|
38
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Starts a progress operation for multi-phase work that does not map to a single item array.
|
|
76
|
+
* Uses script-level cleanup for normal returns and catchable script exceptions; host crashes or native aborts can still bypass script cleanup.
|
|
77
|
+
* @param info The string to display in the progress dialog as the current description of the operation.
|
|
78
|
+
* @param totalSteps The number of progress steps for the operation to be complete.
|
|
79
|
+
* @param callback The function to run with a manual progress handle.
|
|
80
|
+
* @param options Progress dialog options consistent with the array-driven progress helper.
|
|
81
|
+
*/
|
|
82
|
+
export const withProgress = <T>(info: string | string[], totalSteps: number, callback: (progress: ProgressHandle) => T,
|
|
83
|
+
options?: Omit<ProgressOptions, 'totalSteps'>): T => {
|
|
39
84
|
|
|
40
|
-
|
|
41
|
-
|
|
85
|
+
const resolvedOptions = defaultProgressOptions(totalSteps, options);
|
|
86
|
+
const infoMessage = reportInfo(info);
|
|
87
|
+
startProgress(infoMessage, resolvedOptions.totalSteps, resolvedOptions.isCancellable, resolvedOptions.showTimeElapsed);
|
|
42
88
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
89
|
+
let flushedCancellation = false;
|
|
90
|
+
const handle: ProgressHandle = {
|
|
91
|
+
isCancelled: () => {
|
|
92
|
+
const cancelled = resolvedOptions.isCancellable && Boolean(progressIsCancelled());
|
|
93
|
+
if (cancelled && !flushedCancellation) {
|
|
94
|
+
processEvents();
|
|
95
|
+
flushedCancellation = true;
|
|
96
|
+
}
|
|
97
|
+
return cancelled;
|
|
98
|
+
},
|
|
99
|
+
step: (count: number = 1) => {
|
|
100
|
+
if (handle.isCancelled()) return false;
|
|
101
|
+
stepProgress(count);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
49
104
|
}
|
|
50
105
|
|
|
51
|
-
|
|
52
|
-
|
|
106
|
+
try {
|
|
107
|
+
return callback(handle);
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
finishProgress();
|
|
111
|
+
}
|
|
112
|
+
}
|