ontime-pm
Version:
Ontime Process Manager
260 lines (203 loc) • 4.69 kB
Markdown
<h1>Ontime Process manager</h1>
The library is used to manage and control some processes which you are able to run in the browser or NodeJS.
There is sometimes need to split a complicated process into small pieces of code.
It means that you can create simple tasks and combine them into the one process.
**Process manager** - allows you to controll your processes.
**Process** - allows you to controll tasks.
**Task** - simple iteration.
---
<h2>Install</h2>
```bash
yarn add ontime-pm
// or
npm install ontime-pm
```
<h2>Example</h2>
```typescript
// index.ts
import { ProcessManager, Process, Task } from 'ontime-pm';
// Create instance of process manager
const processManager: ProcessManager = new ProcessManager('user');
// Create Tasks classes
class Task1 extends Task {
async run() {
console.log('Task 1. Do something...');
}
async pause() {}
async resume() {}
async cancel() {}
}
class Task2 extends Task {
async run() {
console.log('Task 2. Do something...');
}
async pause() {}
async resume() {}
async cancel() {}
}
class Task3 extends Task {
async run() {
console.log('Task 3. Do something...');
}
async pause() {}
async resume() {}
async cancel() {}
}
// Create Custom Process class
class CustomProcess extends Process<{}, []> {
public get name(): string { return 'test'; }
public get tasks(): any[] { return [Task1, Task2, Task3]; }
}
// Register custom process inside process manager
pm.register('test', CustomProcess);
// Create a new instance of custom process
const proc: CustomProcess = await pm.create('test');
// subscribe on all events
proc.on('*', (...args: any[]) => console.log(...args));
// run custom process
proc.run();
```
---
<h2>Process manager</h2>
<dl>
<dt>
<h4>constructor(userId: string = '')</h4>
</dt>
<dd>
- userId - Optional. User ID.
</dd>
<dt>
<h4>register<P>(processName: string, procClass: P): void</h4>
</dt>
<dd>
The method registers a class process in the system
- processName - process name
- procClass - process class
</dd>
<dt>
<h4>create<P, V>(processName: string, options: V, restoreId?: string): Promise<P></h4>
</dt>
<dd>
The method creates new process instance.
The method returns new instance.
- processName - process name
- options - process variables
- restoreId. Optional. Using for restore process context.
</dd>
<dt>
<h4>status(processId: string): Promise<TStatus></h4>
<dt>
<dd>
The method returns a status of an instance of a process
- processId - process ID
</dd>
<dt>
<h4>get(processId: string): Promise<Process></h4>
<dt>
<dd>
The method returns an instance of Process by a process ID
- processId - process ID
</dd>
<dt>
<h4>list(): Promise<Process[]></h4>
<dt>
<dd>
The method returns a list of active processes
</dd>
</dl>
---
<h2>Process<V, T extends Task[] | Function[]></h2>
- V generic of variables
<dl>
<dt>
<h4>id: string</h4>
</dt>
<dd>
Process ID
</dd>
<dt>
<h4>name: string</h4>
</dt>
<dd>
Process name. Getter property.
</dd>
<dt>
<h4>tasks: T[]</h4>
</dt>
<dd>
Process tasks. Getter property.
</dd>
<dt>
<h4>constructor(userId: string = '', vars: V | Object = {}, restoreId?: string)</h4>
</dt>
<dd>
- userId - Optional. User ID.
- vars - Optional. Process variables.
- restoreId - Optional. Using for restore process context.
</dd>
<dt>
<h4>run(): Promise<void></h4>
</dt>
<dd>
The method runs the process to execute
</dd>
<dt>
<h4>cancel(): Promise<void></h4>
</dt>
<dd>
The method cancels the process
</dd>
<dt>
<h4>pause(): Promise<void></h4>
</dt>
<dd>
The method pauses the process
</dd>
<dt>
<h4>resume(): Promise<void></h4>
</dt>
<dd>
The method resumes the process
</dd>
<dt>
<h4>status(): Promise<TStatus>
</dt>
<dd>
The method returns a status of the process
</dd>
<dt>
<h4>getVars(): Promise<V>
</dt>
<dd>
The method returns process variables
</dd>
</dl>
---
<h2>Task</h2>
<dl>
<dt>
<h4>destroy(): void</h4>
</dt>
<dd>
Destructor
</dd>
<dt>
<h4>cancel(): Promise<void></h4>
</dt>
<dd>
The method cancels the task
</dd>
<dt>
<h4>pause(): Promise<void></h4>
</dt>
<dd>
The method pauses the task
</dd>
<dt>
<h4>resume(): Promise<void></h4>
</dt>
<dd>
The method resumes the task
</dd>
</dl>
Others API depends on the implementation