@guanghechen/task 1.0.0-alpha.1 → 1.0.0-alpha.2

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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.0.0-alpha.2](https://github.com/guanghechen/sora/compare/@guanghechen/task@1.0.0-alpha.1...@guanghechen/task@1.0.0-alpha.2) (2023-08-28)
7
+
8
+
9
+ ### Features
10
+
11
+ * ✨ add @guanghechen/error & refactor other sub packages ([c50e94d](https://github.com/guanghechen/sora/commit/c50e94de4b9e6d7fd635c10e202eb8bdc4f4f8dd))
12
+
13
+
14
+
15
+
16
+
6
17
  # 1.0.0-alpha.1 (2023-08-27)
7
18
 
8
19
 
package/lib/cjs/index.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var error = require('@guanghechen/error');
3
4
  var monitor = require('@guanghechen/monitor');
4
5
 
5
6
  exports.TaskStatus = void 0;
@@ -32,19 +33,20 @@ const terminated = exports.TaskStatus.CANCELLED |
32
33
  exports.TaskStatus.FAILED |
33
34
  exports.TaskStatus.FINISHED;
34
35
 
35
- const noop = () => { };
36
+ function noop(..._args) { }
37
+
36
38
  class TaskState {
37
39
  name;
40
+ _errorCollector;
38
41
  _monitors;
39
- _errorDetails;
40
42
  _status;
41
43
  constructor(name) {
42
44
  this.name = name;
45
+ this._errorCollector = new error.SoraErrorCollector(name);
43
46
  this._monitors = {
44
47
  onAddError: new monitor.Monitor('onAddError'),
45
48
  onStatusChange: new monitor.Monitor('onStatusChange'),
46
49
  };
47
- this._errorDetails = [];
48
50
  this._status = exports.TaskStatus.PENDING;
49
51
  }
50
52
  get status() {
@@ -60,12 +62,12 @@ class TaskState {
60
62
  return (this._status & terminated) > 0;
61
63
  }
62
64
  get hasError() {
63
- return this._errorDetails.length > 0;
65
+ return this._errorCollector.size > 0;
64
66
  }
65
67
  get error() {
66
- if (this._errorDetails.length === 0)
68
+ if (this._errorCollector.size === 0)
67
69
  return undefined;
68
- return { from: this.name, details: this._errorDetails.slice() };
70
+ return { from: this.name, details: this._errorCollector.errors };
69
71
  }
70
72
  set status(status) {
71
73
  const curStatus = this._status;
@@ -84,12 +86,8 @@ class TaskState {
84
86
  if (this.terminated)
85
87
  return noop;
86
88
  const { onAddError, onStatusChange } = monitor;
87
- const unsubscribeOnAddError = onAddError
88
- ? this._monitors.onAddError.subscribe(onAddError)
89
- : noop;
90
- const unsubscribeOnStatusChange = onStatusChange
91
- ? this._monitors.onStatusChange.subscribe(onStatusChange)
92
- : noop;
89
+ const unsubscribeOnAddError = this._monitors.onAddError.subscribe(onAddError);
90
+ const unsubscribeOnStatusChange = this._monitors.onStatusChange.subscribe(onStatusChange);
93
91
  return () => {
94
92
  unsubscribeOnAddError();
95
93
  unsubscribeOnStatusChange();
@@ -129,13 +127,13 @@ class TaskState {
129
127
  cleanup() {
130
128
  if (!this.terminated)
131
129
  throw new Error(`[cleanup] task(${this.name}) is not terminated`);
132
- this._errorDetails.length = 0;
130
+ this._errorCollector.cleanup();
133
131
  this._monitors.onStatusChange.destroy();
134
132
  this._monitors.onAddError.destroy();
135
133
  }
136
- _addError(type, error) {
137
- this._errorDetails.push({ type, error });
138
- this._monitors.onAddError.notify(type, error);
134
+ _addError(type, error$1, level = error.SoraErrorLevel.ERROR) {
135
+ this._errorCollector.add(type, error$1, level);
136
+ this._monitors.onAddError.notify(type, error$1, level);
139
137
  }
140
138
  }
141
139
 
package/lib/esm/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { SoraErrorCollector, SoraErrorLevel } from '@guanghechen/error';
1
2
  import { Monitor } from '@guanghechen/monitor';
2
3
 
3
4
  var TaskStatus;
@@ -30,19 +31,20 @@ const terminated = TaskStatus.CANCELLED |
30
31
  TaskStatus.FAILED |
31
32
  TaskStatus.FINISHED;
32
33
 
33
- const noop = () => { };
34
+ function noop(..._args) { }
35
+
34
36
  class TaskState {
35
37
  name;
38
+ _errorCollector;
36
39
  _monitors;
37
- _errorDetails;
38
40
  _status;
39
41
  constructor(name) {
40
42
  this.name = name;
43
+ this._errorCollector = new SoraErrorCollector(name);
41
44
  this._monitors = {
42
45
  onAddError: new Monitor('onAddError'),
43
46
  onStatusChange: new Monitor('onStatusChange'),
44
47
  };
45
- this._errorDetails = [];
46
48
  this._status = TaskStatus.PENDING;
47
49
  }
48
50
  get status() {
@@ -58,12 +60,12 @@ class TaskState {
58
60
  return (this._status & terminated) > 0;
59
61
  }
60
62
  get hasError() {
61
- return this._errorDetails.length > 0;
63
+ return this._errorCollector.size > 0;
62
64
  }
63
65
  get error() {
64
- if (this._errorDetails.length === 0)
66
+ if (this._errorCollector.size === 0)
65
67
  return undefined;
66
- return { from: this.name, details: this._errorDetails.slice() };
68
+ return { from: this.name, details: this._errorCollector.errors };
67
69
  }
68
70
  set status(status) {
69
71
  const curStatus = this._status;
@@ -82,12 +84,8 @@ class TaskState {
82
84
  if (this.terminated)
83
85
  return noop;
84
86
  const { onAddError, onStatusChange } = monitor;
85
- const unsubscribeOnAddError = onAddError
86
- ? this._monitors.onAddError.subscribe(onAddError)
87
- : noop;
88
- const unsubscribeOnStatusChange = onStatusChange
89
- ? this._monitors.onStatusChange.subscribe(onStatusChange)
90
- : noop;
87
+ const unsubscribeOnAddError = this._monitors.onAddError.subscribe(onAddError);
88
+ const unsubscribeOnStatusChange = this._monitors.onStatusChange.subscribe(onStatusChange);
91
89
  return () => {
92
90
  unsubscribeOnAddError();
93
91
  unsubscribeOnStatusChange();
@@ -127,13 +125,13 @@ class TaskState {
127
125
  cleanup() {
128
126
  if (!this.terminated)
129
127
  throw new Error(`[cleanup] task(${this.name}) is not terminated`);
130
- this._errorDetails.length = 0;
128
+ this._errorCollector.cleanup();
131
129
  this._monitors.onStatusChange.destroy();
132
130
  this._monitors.onAddError.destroy();
133
131
  }
134
- _addError(type, error) {
135
- this._errorDetails.push({ type, error });
136
- this._monitors.onAddError.notify(type, error);
132
+ _addError(type, error, level = SoraErrorLevel.ERROR) {
133
+ this._errorCollector.add(type, error, level);
134
+ this._monitors.onAddError.notify(type, error, level);
137
135
  }
138
136
  }
139
137
 
@@ -1,3 +1,5 @@
1
+ import { ISoraError, SoraErrorLevel, ISoraErrorCollector } from '@guanghechen/error';
2
+
1
3
  declare enum TaskStatus {
2
4
  PENDING = 1,
3
5
  RUNNING = 2,
@@ -18,16 +20,12 @@ declare const active: number;
18
20
  declare const alive: number;
19
21
  declare const terminated: number;
20
22
 
21
- interface ITaskErrorDetail {
22
- type: string;
23
- error: unknown;
24
- }
25
23
  interface ITaskError {
26
24
  from: string;
27
- details: ITaskErrorDetail[];
25
+ details: ISoraError[];
28
26
  }
29
27
  interface ITaskMonitor {
30
- onAddError(type: string, error: unknown): void;
28
+ onAddError(type: string, error: unknown, level: SoraErrorLevel | undefined): void;
31
29
  onStatusChange(status: TaskStatus, prevStatus: TaskStatus): void;
32
30
  }
33
31
  type IUnMonitorTask = () => void;
@@ -84,8 +82,8 @@ interface ITask extends ITaskState {
84
82
 
85
83
  declare class TaskState implements ITaskState {
86
84
  readonly name: string;
85
+ protected readonly _errorCollector: ISoraErrorCollector;
87
86
  private readonly _monitors;
88
- private readonly _errorDetails;
89
87
  private _status;
90
88
  constructor(name: string);
91
89
  get status(): TaskStatus;
@@ -98,7 +96,7 @@ declare class TaskState implements ITaskState {
98
96
  monitor(monitor: Partial<ITaskMonitor>): IUnMonitorTask;
99
97
  check(nextStatus: TaskStatus): boolean;
100
98
  cleanup(): void;
101
- protected _addError(type: string, error: unknown): void;
99
+ protected _addError(type: string, error: unknown, level?: SoraErrorLevel): void;
102
100
  }
103
101
 
104
102
  declare abstract class AtomicTask extends TaskState implements ITask {
@@ -133,4 +131,4 @@ declare abstract class ResumableTask extends TaskState implements ITask {
133
131
  private queueStep;
134
132
  }
135
133
 
136
- export { AtomicTask, type ITask, type ITaskError, type ITaskErrorDetail, type ITaskMonitor, type ITaskState, type IUnMonitorTask, ResumableTask, TaskState, TaskStatus, TaskStrategy, active, alive, terminated };
134
+ export { AtomicTask, type ITask, type ITaskError, type ITaskMonitor, type ITaskState, type IUnMonitorTask, ResumableTask, TaskState, TaskStatus, TaskStrategy, active, alive, terminated };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/task",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.2",
4
4
  "description": "Atomic and resumable tasks",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -8,10 +8,10 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/guanghechen/sora/tree/@guanghechen/task@5.1.0",
11
+ "url": "https://github.com/guanghechen/sora/tree/@guanghechen/task@1.0.0-alpha.1",
12
12
  "directory": "packages/task"
13
13
  },
14
- "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/task@5.1.0/packages/task#readme",
14
+ "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/task@1.0.0-alpha.1/packages/task#readme",
15
15
  "type": "module",
16
16
  "main": "./lib/cjs/index.cjs",
17
17
  "module": "./lib/esm/index.mjs",
@@ -39,13 +39,15 @@
39
39
  "test": "node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir ."
40
40
  },
41
41
  "dependencies": {
42
- "@guanghechen/monitor": "^1.0.0-alpha.1"
42
+ "@guanghechen/error": "^1.0.0-alpha.2",
43
+ "@guanghechen/monitor": "^1.0.0-alpha.2"
43
44
  },
44
45
  "devDependencies": {
46
+ "@guanghechen/shared": "^1.0.0-alpha.2",
45
47
  "cross-env": "^7.0.3",
46
48
  "jest": "^29.6.4",
47
49
  "rimraf": "^5.0.1",
48
50
  "rollup": "^3.28.1"
49
51
  },
50
- "gitHead": "39cf17711022736f580c1dff68a535cea6ffa3ef"
52
+ "gitHead": "c1a0921efc848988c72d7b3c8a94edaa474994e7"
51
53
  }