Skip to content

Commit 4d423d5

Browse files
committed
Bump version to v1.2.0: Enable smart reuse via currentExecution getter to expose the active task's promise
1 parent e9f254d commit 4d423d5

10 files changed

+146
-14
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ If your use case involves keyed tasks - where you need to ensure the mutually ex
2424
- __Mutual Exclusiveness :lock:__: Ensures the mutually exclusive execution of asynchronous tasks, either to prevent potential race conditions caused by tasks spanning across multiple event-loop iterations, or for performance optimization.
2525
- __Graceful Teardown :hourglass_flowing_sand:__: Await the completion of all currently pending and executing tasks using the `waitForAllExistingTasksToComplete` method. Example use cases include application shutdowns (e.g., `onModuleDestroy` in Nest.js applications) or maintaining a clear state between unit-tests.
2626
- __Suitable for "Check and Abort" scenarios :see_no_evil:__: The `isAvailable` getter indicator enables to skip or abort operations if the lock is currently held by another task.
27+
- __Smart Reuse :recycle:__: In scenarios where launching a duplicate task would be wasteful (e.g., refreshing a shared resource or cache), consumers can **await the ongoing execution instead of scheduling a new one**. This is enabled via the `currentExecution` getter, which exposes the currently executing task's promise, if one is active.
2728
- __Backpressure Metric :bar_chart:__: The `pendingTasksCount` getter provides a real-time metric indicating the current backpressure from tasks waiting for the lock to become available. Users can leverage this data to make informed decisions, such as throttling, load balancing, or managing system load. Additionally, this metric can aid in **internal resource management** within a containerized environment. If multiple locks exist - each tied to a unique key - a backpressure value of 0 may indicate that a lock is no longer needed and can be removed temporarily to optimize resource usage.
2829
- __High Efficiency :gear:__: Leverages the Node.js microtasks queue to serve tasks in FIFO order, eliminating the need for manually managing an explicit queue of pending tasks.
2930
- __Comprehensive documentation :books:__: The class is thoroughly documented, enabling IDEs to provide helpful tooltips that enhance the coding experience.
@@ -65,6 +66,7 @@ If needed, refer to the code documentation for a more comprehensive description
6566
The `ZeroOverheadLock` class provides the following getter methods to reflect the current lock's state:
6667

6768
* __isAvailable__: Indicates whether the lock is currently available to immediately begin executing a new task. This property is particularly useful in "check and abort" scenarios, where an operation should be **skipped or aborted** if the lock is currently held by another task.
69+
* __currentExecution__: Exposes the currently executing task's promise, if one is active. This is useful in scenarios where launching a duplicate task would be wasteful - consumers can simply **await the ongoing execution instead**. This feature enhances the lock’s versatility and supports advanced usage patterns.
6870
* __pendingTasksCount__: Returns the number of tasks that are currently pending execution due to the lock being held. These tasks are waiting for the lock to become available before they can proceed.
6971

7072
## Use Case Example: Aggregating Intrusion Detection Event Logs :shield:<a id="first-use-case-example"></a>

dist/zero-overhead-promise-lock.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type AsyncTask<T> = () => Promise<T>;
4949
*/
5050
export declare class ZeroOverheadLock<T> {
5151
private _pendingTasksCount;
52+
private _currentExecution?;
5253
/**
5354
* Availability indicator:
5455
* A pending `_waitForAvailability` promise signifies that the lock is currently held.
@@ -69,6 +70,19 @@ export declare class ZeroOverheadLock<T> {
6970
* @returns `true` if no task is currently executing; otherwise, `false`.
7071
*/
7172
get isAvailable(): boolean;
73+
/**
74+
* currentExecution
75+
*
76+
* Exposes the currently executing task's promise, if one is active.
77+
*
78+
* ### Smart Reuse
79+
* This property is useful in scenarios where launching a duplicate task is wasteful.
80+
* Instead of scheduling a new task, consumers can await the ongoing execution to avoid
81+
* redundant operations.
82+
*
83+
* @returns The currently executing task’s promise, or `undefined` if the lock is available.
84+
*/
85+
get currentExecution(): Promise<T> | undefined;
7286
/**
7387
* pendingTasksCount
7488
*

dist/zero-overhead-promise-lock.js

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/zero-overhead-promise-lock.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/zero-overhead-promise-lock.test.js

Lines changed: 35 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)