@sidewinder/async
Advanced tools
+10
| /** Interface given to instances that issue locks */ | ||
| export interface Lockable { | ||
| lock(): Promise<Lock>; | ||
| } | ||
| export declare class Lock { | ||
| #private; | ||
| constructor(disposeCallback: Function); | ||
| /** Disposes of this lock */ | ||
| dispose(): void; | ||
| } |
+57
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sidewinder/async | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
| if (kind === "m") throw new TypeError("Private method is not writable"); | ||
| if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); | ||
| if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); | ||
| return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; | ||
| }; | ||
| var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
| if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
| if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
| return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
| }; | ||
| var _Lock_disposeCallback; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Lock = void 0; | ||
| // -------------------------------------------------------- | ||
| // Lock | ||
| // -------------------------------------------------------- | ||
| class Lock { | ||
| constructor(disposeCallback) { | ||
| _Lock_disposeCallback.set(this, void 0); | ||
| __classPrivateFieldSet(this, _Lock_disposeCallback, disposeCallback, "f"); | ||
| } | ||
| /** Disposes of this lock */ | ||
| dispose() { | ||
| __classPrivateFieldGet(this, _Lock_disposeCallback, "f").call(this); | ||
| } | ||
| } | ||
| exports.Lock = Lock; | ||
| _Lock_disposeCallback = new WeakMap(); |
| import { Lock, Lockable } from './lock'; | ||
| /** In-memory mutex used to prevent concurrent access on asynchronous resources */ | ||
| export declare class Mutex implements Lockable { | ||
| #private; | ||
| constructor(); | ||
| /** Acquires a lock. */ | ||
| lock(): Promise<Lock>; | ||
| } |
+75
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sidewinder/async | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
| if (kind === "m") throw new TypeError("Private method is not writable"); | ||
| if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); | ||
| if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); | ||
| return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; | ||
| }; | ||
| var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
| if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
| if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
| return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
| }; | ||
| var _Mutex_instances, _Mutex_queue, _Mutex_running, _Mutex_condition, _Mutex_dispatch; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Mutex = void 0; | ||
| const lock_1 = require("./lock"); | ||
| const deferred_1 = require("./deferred"); | ||
| /** In-memory mutex used to prevent concurrent access on asynchronous resources */ | ||
| class Mutex { | ||
| constructor() { | ||
| _Mutex_instances.add(this); | ||
| _Mutex_queue.set(this, void 0); | ||
| _Mutex_running.set(this, void 0); | ||
| __classPrivateFieldSet(this, _Mutex_running, false, "f"); | ||
| __classPrivateFieldSet(this, _Mutex_queue, [], "f"); | ||
| } | ||
| /** Acquires a lock. */ | ||
| async lock() { | ||
| const deferred = new deferred_1.Deferred(); | ||
| __classPrivateFieldGet(this, _Mutex_queue, "f").push(deferred); | ||
| __classPrivateFieldGet(this, _Mutex_instances, "m", _Mutex_dispatch).call(this); | ||
| return deferred.promise(); | ||
| } | ||
| } | ||
| exports.Mutex = Mutex; | ||
| _Mutex_queue = new WeakMap(), _Mutex_running = new WeakMap(), _Mutex_instances = new WeakSet(), _Mutex_condition = function _Mutex_condition() { | ||
| return __classPrivateFieldGet(this, _Mutex_running, "f") === false && __classPrivateFieldGet(this, _Mutex_queue, "f").length > 0; | ||
| }, _Mutex_dispatch = function _Mutex_dispatch() { | ||
| if (!__classPrivateFieldGet(this, _Mutex_instances, "m", _Mutex_condition).call(this)) | ||
| return; | ||
| __classPrivateFieldSet(this, _Mutex_running, true, "f"); | ||
| const next = __classPrivateFieldGet(this, _Mutex_queue, "f").shift(); | ||
| const lock = new lock_1.Lock(() => { | ||
| __classPrivateFieldSet(this, _Mutex_running, false, "f"); | ||
| __classPrivateFieldGet(this, _Mutex_instances, "m", _Mutex_dispatch).call(this); | ||
| }); | ||
| next.resolve(lock); | ||
| }; |
+2
-0
@@ -5,4 +5,6 @@ export * from './barrier'; | ||
| export * from './delay'; | ||
| export * from './lock'; | ||
| export * from './mutex'; | ||
| export * from './retry'; | ||
| export * from './semaphore'; | ||
| export * from './timeout'; |
+2
-0
@@ -48,4 +48,6 @@ "use strict"; | ||
| __exportStar(require("./delay"), exports); | ||
| __exportStar(require("./lock"), exports); | ||
| __exportStar(require("./mutex"), exports); | ||
| __exportStar(require("./retry"), exports); | ||
| __exportStar(require("./semaphore"), exports); | ||
| __exportStar(require("./timeout"), exports); |
+1
-1
| { | ||
| "name": "@sidewinder/async", | ||
| "description": "Sidewinder Async", | ||
| "version": "0.12.15", | ||
| "version": "0.13.0", | ||
| "author": "sinclairzx81", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
36022
27.61%23
21.05%686
25.64%