node-thread-decorator 1.0.3 → 1.0.4
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/README.md +5 -7
- package/dist/thread.decorator.d.ts +23 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
### Install
|
|
9
9
|
|
|
10
10
|
```
|
|
11
|
-
|
|
11
|
+
npm i -S node-thread-decorator
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
### Usage
|
|
@@ -18,18 +18,17 @@ $ npm i -S node-thread-decorator
|
|
|
18
18
|
```ts
|
|
19
19
|
import { RunInNewThread } from "node-thread-decorator";
|
|
20
20
|
```
|
|
21
|
-
|
|
21
|
+
- Usage
|
|
22
22
|
```ts
|
|
23
23
|
import { Controller, Get } from '@nestjs/common';
|
|
24
24
|
|
|
25
25
|
import { RunInNewThread } from 'node-thread-decorator';
|
|
26
|
-
import { IHealthAdapter } from './adapter';
|
|
27
26
|
|
|
28
27
|
@Controller()
|
|
29
28
|
export class HealthController {
|
|
30
29
|
/**
|
|
31
30
|
* Blocks the main thread for a specified duration in milliseconds.
|
|
32
|
-
* This function runs in a new process due to the decorator `@
|
|
31
|
+
* This function runs in a new process due to the decorator `@RunInNewThread`.
|
|
33
32
|
*
|
|
34
33
|
* @param {number} milliseconds - The time to block the thread in milliseconds.
|
|
35
34
|
*/
|
|
@@ -39,7 +38,6 @@ export class HealthController {
|
|
|
39
38
|
while (Date.now() - start < milliseconds) {
|
|
40
39
|
// Looping until time has passed
|
|
41
40
|
}
|
|
42
|
-
console.log("Finished new process", process.pid);
|
|
43
41
|
}
|
|
44
42
|
|
|
45
43
|
/**
|
|
@@ -50,12 +48,12 @@ export class HealthController {
|
|
|
50
48
|
*/
|
|
51
49
|
@Get(['/health', '/'])
|
|
52
50
|
async getHealth(): Promise<string> {
|
|
53
|
-
console.log("Started process", process.pid);
|
|
54
51
|
this.blockMainThread(10000); // Simulating blocking the thread for 10 seconds
|
|
55
52
|
console.log("Health check completed");
|
|
56
53
|
return "APP UP!"
|
|
57
54
|
}
|
|
58
55
|
}
|
|
56
|
+
```
|
|
59
57
|
|
|
60
58
|
---
|
|
61
59
|
|
|
@@ -66,4 +64,4 @@ The following is a list of all the people that have contributed Node Thread Deco
|
|
|
66
64
|
## License
|
|
67
65
|
|
|
68
66
|
It is available under the MIT license.
|
|
69
|
-
[License](https://opensource.org/licenses/mit-license.php)
|
|
67
|
+
[License](https://opensource.org/licenses/mit-license.php)
|
|
@@ -1 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Decorator that executes a method in a new thread.
|
|
3
|
+
*
|
|
4
|
+
* This decorator utilizes `worker_threads` to execute the target method in a separate thread,
|
|
5
|
+
* improving performance for CPU-intensive tasks without blocking the main event loop.
|
|
6
|
+
* Optionally, a timeout can be specified to terminate the thread if it exceeds the given time.
|
|
7
|
+
*
|
|
8
|
+
* @param timeout - (Optional) The maximum execution time in milliseconds before the thread is terminated.
|
|
9
|
+
* If not provided, the thread runs without a timeout.
|
|
10
|
+
* @returns A method decorator that modifies the method to execute in a new thread.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* class TaskProcessor {
|
|
15
|
+
* @RunInNewThread(5000) // Runs in a new thread with a 5-second timeout
|
|
16
|
+
* heavyComputation() {
|
|
17
|
+
* // CPU-intensive task
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function RunInNewThread(timeout?: number):
|
|
23
|
+
(target: object, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|