@stryke/helpers 0.4.6 → 0.5.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/README.md CHANGED
@@ -54,20 +54,22 @@ other Stryke projects.
54
54
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
55
55
  ## Table of Contents
56
56
 
57
- - [Quick Features](#quick-features)
58
- - [Installing](#installing)
59
- - [Reduced Package Size](#reduced-package-size)
60
- - [Development](#development)
61
- - [Building](#building)
62
- - [Running unit tests](#running-unit-tests)
63
- - [Linting](#linting)
64
- - [Storm Workspaces](#storm-workspaces)
65
- - [Roadmap](#roadmap)
66
- - [Support](#support)
67
- - [License](#license)
68
- - [Changelog](#changelog)
69
- - [Contributing](#contributing)
70
- - [Contributors](#contributors)
57
+ - [Stryke - Helper Functions](#stryke---helper-functions)
58
+ - [Table of Contents](#table-of-contents)
59
+ - [Quick Features](#quick-features)
60
+ - [Installing](#installing)
61
+ - [Reduced Package Size](#reduced-package-size)
62
+ - [Development](#development)
63
+ - [Building](#building)
64
+ - [Running unit tests](#running-unit-tests)
65
+ - [Linting](#linting)
66
+ - [Storm Workspaces](#storm-workspaces)
67
+ - [Roadmap](#roadmap)
68
+ - [Support](#support)
69
+ - [License](#license)
70
+ - [Changelog](#changelog)
71
+ - [Contributing](#contributing)
72
+ - [Contributors](#contributors)
71
73
 
72
74
  <!-- END doctoc -->
73
75
 
@@ -101,6 +103,8 @@ The following modules are available in this package:
101
103
  - **throttle**: Provides a function to throttle another function, limiting its
102
104
  execution rate.
103
105
  - **timeout**: Provides a function to execute another function with a timeout.
106
+ - **semaphore**: Create a semaphore instance.
107
+ - **mutex**: Create a mutex instance.
104
108
  - **to-deep-key**: Provides a function to convert a path to a deep key.
105
109
  - **to-path**: Provides a function to convert a deep key to a path.
106
110
  - **unflatten-object**: Provides a function to unflatten a single level object
package/dist/index.cjs CHANGED
@@ -157,6 +157,17 @@ Object.keys(_matchSorter).forEach(function (key) {
157
157
  }
158
158
  });
159
159
  });
160
+ var _mutex = require("./mutex.cjs");
161
+ Object.keys(_mutex).forEach(function (key) {
162
+ if (key === "default" || key === "__esModule") return;
163
+ if (key in exports && exports[key] === _mutex[key]) return;
164
+ Object.defineProperty(exports, key, {
165
+ enumerable: true,
166
+ get: function () {
167
+ return _mutex[key];
168
+ }
169
+ });
170
+ });
160
171
  var _noop = require("./noop.cjs");
161
172
  Object.keys(_noop).forEach(function (key) {
162
173
  if (key === "default" || key === "__esModule") return;
@@ -190,6 +201,17 @@ Object.keys(_removeEmptyItems).forEach(function (key) {
190
201
  }
191
202
  });
192
203
  });
204
+ var _semaphore = require("./semaphore.cjs");
205
+ Object.keys(_semaphore).forEach(function (key) {
206
+ if (key === "default" || key === "__esModule") return;
207
+ if (key in exports && exports[key] === _semaphore[key]) return;
208
+ Object.defineProperty(exports, key, {
209
+ enumerable: true,
210
+ get: function () {
211
+ return _semaphore[key];
212
+ }
213
+ });
214
+ });
193
215
  var _setField = require("./set-field.cjs");
194
216
  Object.keys(_setField).forEach(function (key) {
195
217
  if (key === "default" || key === "__esModule") return;
package/dist/index.d.ts CHANGED
@@ -12,9 +12,11 @@ export * from "./get-unique";
12
12
  export * from "./identity";
13
13
  export * from "./is-equal";
14
14
  export * from "./match-sorter";
15
+ export * from "./mutex";
15
16
  export * from "./noop";
16
17
  export * from "./remove-accents";
17
18
  export * from "./remove-empty-items";
19
+ export * from "./semaphore";
18
20
  export * from "./set-field";
19
21
  export * from "./throttle";
20
22
  export * from "./timeout";
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export*from"./arg-identity";export*from"./debounce";export*from"./deep-clone";export*from"./deep-copy";export*from"./deep-merge";export*from"./delay";export*from"./filter-empty";export*from"./flatten-object";export*from"./get-field";export*from"./get-ordered-by";export*from"./get-unique";export*from"./identity";export*from"./is-equal";export*from"./match-sorter";export*from"./noop";export*from"./remove-accents";export*from"./remove-empty-items";export*from"./set-field";export*from"./throttle";export*from"./timeout";export*from"./to-deep-key";export*from"./to-path";export*from"./unflatten-object";export*from"./union";export*from"./with-timeout";
1
+ export*from"./arg-identity";export*from"./debounce";export*from"./deep-clone";export*from"./deep-copy";export*from"./deep-merge";export*from"./delay";export*from"./filter-empty";export*from"./flatten-object";export*from"./get-field";export*from"./get-ordered-by";export*from"./get-unique";export*from"./identity";export*from"./is-equal";export*from"./match-sorter";export*from"./mutex";export*from"./noop";export*from"./remove-accents";export*from"./remove-empty-items";export*from"./semaphore";export*from"./set-field";export*from"./throttle";export*from"./timeout";export*from"./to-deep-key";export*from"./to-path";export*from"./unflatten-object";export*from"./union";export*from"./with-timeout";
package/dist/mutex.cjs ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Mutex = void 0;
7
+ var _semaphore = require("./semaphore.cjs");
8
+ class Mutex {
9
+ semaphore = new _semaphore.Semaphore(1);
10
+ get isLocked() {
11
+ return this.semaphore.available === 0;
12
+ }
13
+ async acquire() {
14
+ return this.semaphore.acquire();
15
+ }
16
+ release() {
17
+ this.semaphore.release();
18
+ }
19
+ }
20
+ exports.Mutex = Mutex;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * A Mutex (mutual exclusion lock) for async functions.
3
+ * It allows only one async task to access a critical section at a time.
4
+ *
5
+ * @example
6
+ * const mutex = new Mutex();
7
+ *
8
+ * async function criticalSection() {
9
+ * await mutex.acquire();
10
+ * try {
11
+ * // This code section cannot be executed simultaneously
12
+ * } finally {
13
+ * mutex.release();
14
+ * }
15
+ * }
16
+ *
17
+ * criticalSection();
18
+ * criticalSection(); // This call will wait until the first call releases the mutex.
19
+ */
20
+ export declare class Mutex {
21
+ private semaphore;
22
+ /**
23
+ * Checks if the mutex is currently locked.
24
+ * @returns {boolean} True if the mutex is locked, false otherwise.
25
+ *
26
+ * @example
27
+ * const mutex = new Mutex();
28
+ * console.log(mutex.isLocked); // false
29
+ * await mutex.acquire();
30
+ * console.log(mutex.isLocked); // true
31
+ * mutex.release();
32
+ * console.log(mutex.isLocked); // false
33
+ */
34
+ get isLocked(): boolean;
35
+ /**
36
+ * Acquires the mutex, blocking if necessary until it is available.
37
+ * @returns {Promise<void>} A promise that resolves when the mutex is acquired.
38
+ *
39
+ * @example
40
+ * const mutex = new Mutex();
41
+ * await mutex.acquire();
42
+ * try {
43
+ * // This code section cannot be executed simultaneously
44
+ * } finally {
45
+ * mutex.release();
46
+ * }
47
+ */
48
+ acquire(): Promise<void>;
49
+ /**
50
+ * Releases the mutex, allowing another waiting task to proceed.
51
+ *
52
+ * @example
53
+ * const mutex = new Mutex();
54
+ * await mutex.acquire();
55
+ * try {
56
+ * // This code section cannot be executed simultaneously
57
+ * } finally {
58
+ * mutex.release(); // Allows another waiting task to proceed.
59
+ * }
60
+ */
61
+ release(): void;
62
+ }
package/dist/mutex.mjs ADDED
@@ -0,0 +1 @@
1
+ import{Semaphore as e}from"./semaphore";export class Mutex{semaphore=new e(1);get isLocked(){return this.semaphore.available===0}async acquire(){return this.semaphore.acquire()}release(){this.semaphore.release()}}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Semaphore = void 0;
7
+ class Semaphore {
8
+ capacity;
9
+ available;
10
+ deferredTasks = [];
11
+ constructor(e) {
12
+ this.capacity = e, this.available = e;
13
+ }
14
+ async acquire() {
15
+ if (this.available > 0) {
16
+ this.available--;
17
+ return;
18
+ }
19
+ return new Promise(e => {
20
+ this.deferredTasks.push(e);
21
+ });
22
+ }
23
+ release() {
24
+ const e = this.deferredTasks.shift();
25
+ if (e != null) {
26
+ e();
27
+ return;
28
+ }
29
+ this.available < this.capacity && this.available++;
30
+ }
31
+ }
32
+ exports.Semaphore = Semaphore;
@@ -0,0 +1,82 @@
1
+ /**
2
+ * A counting semaphore for async functions that manages available permits.
3
+ * Semaphores are mainly used to limit the number of concurrent async tasks.
4
+ *
5
+ * Each `acquire` operation takes a permit or waits until one is available.
6
+ * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.
7
+ *
8
+ * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.
9
+ *
10
+ * @example
11
+ * const sema = new Semaphore(2);
12
+ *
13
+ * async function task() {
14
+ * await sema.acquire();
15
+ * try {
16
+ * // This code can only be executed by two tasks at the same time
17
+ * } finally {
18
+ * sema.release();
19
+ * }
20
+ * }
21
+ *
22
+ * task();
23
+ * task();
24
+ * task(); // This task will wait until one of the previous tasks releases the semaphore.
25
+ */
26
+ export declare class Semaphore {
27
+ /**
28
+ * The maximum number of concurrent operations allowed.
29
+ */
30
+ capacity: number;
31
+ /**
32
+ * The number of available permits.
33
+ */
34
+ available: number;
35
+ private deferredTasks;
36
+ /**
37
+ * Creates an instance of Semaphore.
38
+ * @param capacity - The maximum number of concurrent operations allowed.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.
43
+ * ```
44
+ */
45
+ constructor(capacity: number);
46
+ /**
47
+ * Acquires a semaphore, blocking if necessary until one is available.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const sema = new Semaphore(1);
52
+ *
53
+ * async function criticalSection() {
54
+ * await sema.acquire();
55
+ * try {
56
+ * // This code section cannot be executed simultaneously
57
+ * } finally {
58
+ * sema.release();
59
+ * }
60
+ * }
61
+ * ```
62
+ *
63
+ * @returns A promise that resolves when the semaphore is acquired.
64
+ */
65
+ acquire(): Promise<void>;
66
+ /**
67
+ * Releases a semaphore, allowing one more operation to proceed.
68
+ *
69
+ * @example
70
+ * const sema = new Semaphore(1);
71
+ *
72
+ * async function task() {
73
+ * await sema.acquire();
74
+ * try {
75
+ * // This code can only be executed by two tasks at the same time
76
+ * } finally {
77
+ * sema.release(); // Allows another waiting task to proceed.
78
+ * }
79
+ * }
80
+ */
81
+ release(): void;
82
+ }
@@ -0,0 +1 @@
1
+ export class Semaphore{capacity;available;deferredTasks=[];constructor(e){this.capacity=e,this.available=e}async acquire(){if(this.available>0){this.available--;return}return new Promise(e=>{this.deferredTasks.push(e)})}release(){const e=this.deferredTasks.shift();if(e!=null){e();return}this.available<this.capacity&&this.available++}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/helpers",
3
- "version": "0.4.6",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "A package containing miscellaneous helper functions that are used across many different Storm Software projects.",
6
6
  "repository": {
@@ -165,6 +165,20 @@
165
165
  "default": "./dist/set-field.mjs"
166
166
  }
167
167
  },
168
+ "./semaphore": {
169
+ "import": {
170
+ "types": "./dist/semaphore.d.ts",
171
+ "default": "./dist/semaphore.mjs"
172
+ },
173
+ "require": {
174
+ "types": "./dist/semaphore.d.ts",
175
+ "default": "./dist/semaphore.cjs"
176
+ },
177
+ "default": {
178
+ "types": "./dist/semaphore.d.ts",
179
+ "default": "./dist/semaphore.mjs"
180
+ }
181
+ },
168
182
  "./remove-empty-items": {
169
183
  "import": {
170
184
  "types": "./dist/remove-empty-items.d.ts",
@@ -198,6 +212,14 @@
198
212
  "require": { "types": "./dist/noop.d.ts", "default": "./dist/noop.cjs" },
199
213
  "default": { "types": "./dist/noop.d.ts", "default": "./dist/noop.mjs" }
200
214
  },
215
+ "./mutex": {
216
+ "import": { "types": "./dist/mutex.d.ts", "default": "./dist/mutex.mjs" },
217
+ "require": {
218
+ "types": "./dist/mutex.d.ts",
219
+ "default": "./dist/mutex.cjs"
220
+ },
221
+ "default": { "types": "./dist/mutex.d.ts", "default": "./dist/mutex.mjs" }
222
+ },
201
223
  "./match-sorter": {
202
224
  "import": {
203
225
  "types": "./dist/match-sorter.d.ts",