@salesforce/core 8.16.0 → 8.17.0
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/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/util/mutex.d.ts +42 -1
- package/lib/util/mutex.js +42 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -38,3 +38,4 @@ export { ScratchOrgLifecycleEvent, scratchOrgLifecycleEventName, scratchOrgLifec
|
|
|
38
38
|
export { ScratchOrgCache } from './org/scratchOrgCache';
|
|
39
39
|
export { default as ScratchOrgSettingsGenerator } from './org/scratchOrgSettingsGenerator';
|
|
40
40
|
export * from './util/sfdc';
|
|
41
|
+
export * from './util/mutex';
|
package/lib/index.js
CHANGED
|
@@ -118,4 +118,5 @@ var scratchOrgSettingsGenerator_1 = require("./org/scratchOrgSettingsGenerator")
|
|
|
118
118
|
Object.defineProperty(exports, "ScratchOrgSettingsGenerator", { enumerable: true, get: function () { return __importDefault(scratchOrgSettingsGenerator_1).default; } });
|
|
119
119
|
// Utility sub-modules
|
|
120
120
|
__exportStar(require("./util/sfdc"), exports);
|
|
121
|
+
__exportStar(require("./util/mutex"), exports);
|
|
121
122
|
//# sourceMappingURL=index.js.map
|
package/lib/util/mutex.d.ts
CHANGED
|
@@ -1,8 +1,49 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* A mutual exclusion (mutex) class that ensures only one asynchronous operation
|
|
3
|
+
* can execute at a time, providing thread-safe execution of critical sections.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* const mutex = new Mutex();
|
|
8
|
+
*
|
|
9
|
+
* // Only one of these will execute at a time
|
|
10
|
+
* mutex.lock(async () => {
|
|
11
|
+
* // Critical section code here
|
|
12
|
+
* return someAsyncOperation();
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
3
15
|
*/
|
|
4
16
|
export declare class Mutex {
|
|
17
|
+
/**
|
|
18
|
+
* Internal promise chain that maintains the mutex state.
|
|
19
|
+
* Each new lock acquisition is chained to this promise.
|
|
20
|
+
*
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
5
23
|
private mutex;
|
|
24
|
+
/**
|
|
25
|
+
* Acquires the mutex lock and executes the provided function.
|
|
26
|
+
* The function will not execute until all previously queued operations complete.
|
|
27
|
+
*
|
|
28
|
+
* @template T - The return type of the function
|
|
29
|
+
* @param fn - The function to execute while holding the mutex lock. Can be synchronous or asynchronous.
|
|
30
|
+
* @returns A promise that resolves with the result of the function execution
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const result = await mutex.lock(async () => {
|
|
35
|
+
* // This code is guaranteed to run exclusively
|
|
36
|
+
* return await someAsyncOperation();
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
6
40
|
lock<T>(fn: () => Promise<T> | T): Promise<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Acquires the mutex by waiting for the current promise chain to resolve
|
|
43
|
+
* and returns a release function to unlock the mutex.
|
|
44
|
+
*
|
|
45
|
+
* @private
|
|
46
|
+
* @returns A promise that resolves to a function that releases the mutex lock
|
|
47
|
+
*/
|
|
7
48
|
private acquire;
|
|
8
49
|
}
|
package/lib/util/mutex.js
CHANGED
|
@@ -8,10 +8,44 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.Mutex = void 0;
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* A mutual exclusion (mutex) class that ensures only one asynchronous operation
|
|
12
|
+
* can execute at a time, providing thread-safe execution of critical sections.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const mutex = new Mutex();
|
|
17
|
+
*
|
|
18
|
+
* // Only one of these will execute at a time
|
|
19
|
+
* mutex.lock(async () => {
|
|
20
|
+
* // Critical section code here
|
|
21
|
+
* return someAsyncOperation();
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
12
24
|
*/
|
|
13
25
|
class Mutex {
|
|
26
|
+
/**
|
|
27
|
+
* Internal promise chain that maintains the mutex state.
|
|
28
|
+
* Each new lock acquisition is chained to this promise.
|
|
29
|
+
*
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
14
32
|
mutex = Promise.resolve();
|
|
33
|
+
/**
|
|
34
|
+
* Acquires the mutex lock and executes the provided function.
|
|
35
|
+
* The function will not execute until all previously queued operations complete.
|
|
36
|
+
*
|
|
37
|
+
* @template T - The return type of the function
|
|
38
|
+
* @param fn - The function to execute while holding the mutex lock. Can be synchronous or asynchronous.
|
|
39
|
+
* @returns A promise that resolves with the result of the function execution
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const result = await mutex.lock(async () => {
|
|
44
|
+
* // This code is guaranteed to run exclusively
|
|
45
|
+
* return await someAsyncOperation();
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
15
49
|
async lock(fn) {
|
|
16
50
|
const unlock = await this.acquire();
|
|
17
51
|
try {
|
|
@@ -21,6 +55,13 @@ class Mutex {
|
|
|
21
55
|
unlock();
|
|
22
56
|
}
|
|
23
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Acquires the mutex by waiting for the current promise chain to resolve
|
|
60
|
+
* and returns a release function to unlock the mutex.
|
|
61
|
+
*
|
|
62
|
+
* @private
|
|
63
|
+
* @returns A promise that resolves to a function that releases the mutex lock
|
|
64
|
+
*/
|
|
24
65
|
async acquire() {
|
|
25
66
|
let release;
|
|
26
67
|
const promise = new Promise((resolve) => {
|