@vorplex/core 0.0.38 → 0.0.41

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.
@@ -3,6 +3,7 @@ export declare const TaskStatus: {
3
3
  readonly Busy: "busy";
4
4
  readonly Failed: "failed";
5
5
  readonly Done: "done";
6
+ readonly Cancelled: "cancelled";
6
7
  };
7
8
  export type TaskStatus = (typeof TaskStatus)[keyof typeof TaskStatus];
8
9
  export interface Attachment {
@@ -24,6 +25,8 @@ export declare class Task extends Subscribable<Task> {
24
25
  tasks: Task[];
25
26
  logs: Log[];
26
27
  constructor(name: string, task?: Task);
28
+ isCancelled(): any;
29
+ cancel(): void;
27
30
  do<T>(name: string, callback: (task: Task) => Promise<T> | T): Promise<T>;
28
31
  static do<T>(name: string, callback: (task: Task) => Promise<T> | T): Promise<T>;
29
32
  log(message: string, options?: {
@@ -9,6 +9,7 @@ export const TaskStatus = {
9
9
  Busy: 'busy',
10
10
  Failed: 'failed',
11
11
  Done: 'done',
12
+ Cancelled: 'cancelled'
12
13
  };
13
14
  export class Task extends Subscribable {
14
15
  startTimestamp = Date.now();
@@ -24,7 +25,20 @@ export class Task extends Subscribable {
24
25
  this.parent = task;
25
26
  this.subscribe(task => task.parent?.emit(this.parent));
26
27
  }
28
+ isCancelled() {
29
+ return this.status === TaskStatus.Cancelled || this.parent?.isCancelled() === true;
30
+ }
31
+ cancel() {
32
+ if (this.status === TaskStatus.Busy) {
33
+ this.log('Task cancelled', { level: 'error' });
34
+ this.status = TaskStatus.Cancelled;
35
+ this.finishTimestamp = Date.now();
36
+ this.emit(this);
37
+ }
38
+ }
27
39
  async do(name, callback) {
40
+ if (this.isCancelled())
41
+ throw new Error('Task cancelled');
28
42
  const task = new Task(name, this);
29
43
  this.tasks.push(task);
30
44
  this.emit(this);
@@ -34,7 +48,7 @@ export class Task extends Subscribable {
34
48
  return result;
35
49
  }
36
50
  catch (error) {
37
- task.fail();
51
+ task.fail(error);
38
52
  throw error;
39
53
  }
40
54
  }
@@ -52,6 +66,8 @@ export class Task extends Subscribable {
52
66
  }
53
67
  }
54
68
  log(message, options) {
69
+ if (this.isCancelled())
70
+ throw new Error('Task cancelled');
55
71
  this.logs.push({
56
72
  timestamp: Date.now(),
57
73
  message,
@@ -61,32 +77,34 @@ export class Task extends Subscribable {
61
77
  this.emit(this);
62
78
  }
63
79
  fail(error) {
64
- if (this.status !== 'failed') {
80
+ if (this.status === TaskStatus.Busy) {
65
81
  if (error)
66
82
  this.log(error instanceof Error ? error.stack ?? error.message : error, { level: 'error' });
67
- this.status = 'failed';
83
+ this.status = TaskStatus.Failed;
68
84
  this.finishTimestamp = Date.now();
69
85
  this.emit(this);
70
86
  }
71
87
  }
72
88
  complete() {
73
- if (this.status === 'busy') {
74
- this.status = 'done';
89
+ if (this.status === TaskStatus.Busy) {
90
+ this.status = TaskStatus.Done;
75
91
  this.finishTimestamp = Date.now();
76
92
  this.emit(this);
77
93
  }
78
94
  }
79
95
  getStatus() {
80
96
  let failed;
97
+ if (this.isCancelled())
98
+ return TaskStatus.Cancelled;
81
99
  for (const task of this.tasks) {
82
100
  const status = task.getStatus();
83
- if (status === 'failed')
101
+ if (status === TaskStatus.Failed)
84
102
  failed = true;
85
- if (status === 'busy')
86
- return 'busy';
103
+ if (status === TaskStatus.Busy)
104
+ return TaskStatus.Busy;
87
105
  }
88
106
  if (failed)
89
- return 'failed';
107
+ return TaskStatus.Failed;
90
108
  return this.status;
91
109
  }
92
110
  hasWarning() {
@@ -104,7 +122,7 @@ export class Task extends Subscribable {
104
122
  toConsoleLog() {
105
123
  const formatter = {
106
124
  task: (task) => {
107
- const status = { done: `${Chalk.White}★ [Done]`, busy: `${Chalk.Orange}★ [Busy]`, failed: `${Chalk.Red}★ [Failed]` }[task.getStatus()];
125
+ const status = { done: `${Chalk.White}★ [Done]`, busy: `${Chalk.Orange}★ [Busy]`, failed: `${Chalk.Red}★ [Failed]`, cancelled: `${Chalk.Red}★ [Cancelled]` }[task.getStatus()];
108
126
  const date = $Date.format(new Date(task.startTimestamp), '[YYYY-MM-DD hh:mm:ss]');
109
127
  const duration = $Number.toUnitString(task.finishTimestamp - task.startTimestamp, Unit.Time);
110
128
  return `${status} ${date} ${task.name} ${Chalk.Dim}${duration}${Chalk.Reset}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vorplex/core",
3
- "version": "0.0.38",
3
+ "version": "0.0.41",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "files": [