task.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { PluginComponent } from './base';
  8. import { OrderedMap } from 'immutable';
  9. import { TaskManager } from 'mol-plugin/util/task-manager';
  10. import { filter } from 'rxjs/operators';
  11. import { Progress } from 'mol-task';
  12. export class BackgroundTaskProgress extends PluginComponent<{ }, { tracked: OrderedMap<number, TaskManager.ProgressEvent> }> {
  13. componentDidMount() {
  14. this.subscribe(this.plugin.events.task.progress.pipe(filter(e => e.level !== 'none')), e => {
  15. this.setState({ tracked: this.state.tracked.set(e.id, e) })
  16. });
  17. this.subscribe(this.plugin.events.task.finished, ({ id }) => {
  18. this.setState({ tracked: this.state.tracked.delete(id) })
  19. })
  20. }
  21. state = { tracked: OrderedMap<number, TaskManager.ProgressEvent>() };
  22. render() {
  23. return <div>
  24. {this.state.tracked.valueSeq().map(e => <ProgressEntry key={e!.id} event={e!} />)}
  25. </div>;
  26. }
  27. }
  28. class ProgressEntry extends PluginComponent<{ event: TaskManager.ProgressEvent }> {
  29. render() {
  30. const root = this.props.event.progress.root;
  31. const subtaskCount = countSubtasks(this.props.event.progress.root) - 1;
  32. const pr = root.progress.isIndeterminate
  33. ? void 0
  34. : <>[{root.progress.current}/{root.progress.max}]</>;
  35. const subtasks = subtaskCount > 0
  36. ? <>[{subtaskCount} subtask(s)]</>
  37. : void 0
  38. return <div>
  39. {root.progress.message} {pr} {subtasks}
  40. </div>;
  41. }
  42. }
  43. function countSubtasks(progress: Progress.Node) {
  44. if (progress.children.length === 0) return 1;
  45. let sum = 0;
  46. for (const c of progress.children) sum += countSubtasks(c);
  47. return sum;
  48. }