|
@@ -4,7 +4,7 @@
|
|
|
* @author David Sehnal <david.sehnal@gmail.com>
|
|
|
*/
|
|
|
|
|
|
-import { Task, Run, Progress, Scheduler, now } from 'mol-task'
|
|
|
+import { Task, Run, Progress, Scheduler, now, MultistepTask } from 'mol-task'
|
|
|
|
|
|
export async function test1() {
|
|
|
const t = Task.create('test', async () => 1);
|
|
@@ -13,11 +13,17 @@ export async function test1() {
|
|
|
}
|
|
|
|
|
|
function messageTree(root: Progress.Node, prefix = ''): string {
|
|
|
- if (!root.children.length) return `${prefix}${root.progress.taskName}: ${root.progress.message}`;
|
|
|
+ const p = root.progress;
|
|
|
+ if (!root.children.length) {
|
|
|
+ if (p.isIndeterminate) return `${prefix}${p.taskName}: ${p.message}`;
|
|
|
+ return `${prefix}${p.taskName}: [${p.current}/${p.max}] ${p.message}`;
|
|
|
+ }
|
|
|
|
|
|
const newPrefix = prefix + ' |_ ';
|
|
|
const subTree = root.children.map(c => messageTree(c, newPrefix));
|
|
|
- return `${prefix}${root.progress.taskName}: ${root.progress.message}\n${subTree.join('\n')}`;
|
|
|
+ if (p.isIndeterminate) return `${prefix}${p.taskName}: ${p.message}\n${subTree.join('\n')}`;
|
|
|
+ return `${prefix}${p.taskName}: [${p.current}/${p.max}] ${p.message}\n${subTree.join('\n')}`;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
function createTask<T>(delayMs: number, r: T): Task<T> {
|
|
@@ -38,7 +44,7 @@ export function abortAfter(delay: number) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-function testTree() {
|
|
|
+export function testTree() {
|
|
|
return Task.create('test o', async ctx => {
|
|
|
await Scheduler.delay(250);
|
|
|
if (ctx.shouldUpdate) await ctx.update({ message: 'hi! 1' });
|
|
@@ -60,6 +66,17 @@ function testTree() {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+const ms = MultistepTask('ms-task', ['step 1', 'step 2', 'step 3'], async (p: { i: number }, step) => {
|
|
|
+ await Scheduler.delay(250);
|
|
|
+ await step(0);
|
|
|
+ await Scheduler.delay(250);
|
|
|
+ await step(1);
|
|
|
+ await Scheduler.delay(250);
|
|
|
+ await step(2);
|
|
|
+ await Scheduler.delay(250);
|
|
|
+ return p.i + 3;
|
|
|
+})
|
|
|
+
|
|
|
export function abortingObserver(p: Progress) {
|
|
|
console.log(messageTree(p.root));
|
|
|
if (now() - p.root.progress.startedTime > 1000) {
|
|
@@ -67,11 +84,16 @@ export function abortingObserver(p: Progress) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+export function logP(p: Progress) { console.log(messageTree(p.root)); }
|
|
|
+
|
|
|
async function test() {
|
|
|
try {
|
|
|
//const r = await Run(testTree(), p => console.log(messageTree(p.root)), 250);
|
|
|
- const r = await Run(testTree(), abortingObserver, 250);
|
|
|
- console.log(r);
|
|
|
+ //const r = await Run(testTree(), abortingObserver, 250);
|
|
|
+ //console.log(r);
|
|
|
+
|
|
|
+ const m = await Run(ms({ i: 10 }), logP);
|
|
|
+ console.log(m);
|
|
|
} catch (e) {
|
|
|
console.error(e);
|
|
|
}
|