@matrixai/async-init
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,15 +0,24 @@ | ||
interface CreateDestroy { | ||
get destroyed(): boolean; | ||
destroy(...args: Array<any>): Promise<any>; | ||
import { Mutex } from 'async-mutex'; | ||
/** | ||
* Symbols prevents name clashes with decorated classes | ||
*/ | ||
declare const _destroyed: unique symbol; | ||
declare const destroyed: unique symbol; | ||
declare const initLock: unique symbol; | ||
interface CreateDestroy<DestroyReturn = unknown> { | ||
get [destroyed](): boolean; | ||
readonly [initLock]: Mutex; | ||
destroy(...args: Array<any>): Promise<DestroyReturn | void>; | ||
} | ||
declare function CreateDestroy(): <T extends new (...args: any[]) => { | ||
destroy?(...args: Array<any>): Promise<any>; | ||
declare function CreateDestroy<DestroyReturn = unknown>(): <T extends new (...args: any[]) => { | ||
destroy?(...args: Array<any>): Promise<DestroyReturn | void>; | ||
}>(constructor: T) => { | ||
new (...args: any[]): { | ||
_destroyed: boolean; | ||
readonly destroyed: boolean; | ||
destroy(...args: Array<any>): Promise<any>; | ||
destroy(...args: Array<any>): Promise<DestroyReturn | void>; | ||
[_destroyed]: boolean; | ||
readonly [initLock]: Mutex; | ||
readonly [destroyed]: boolean; | ||
}; | ||
} & T; | ||
declare function ready(errorDestroyed?: Error): (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
export { CreateDestroy, ready }; | ||
declare function ready(errorDestroyed?: Error, wait?: boolean): (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
export { CreateDestroy, ready, destroyed, initLock }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ready = exports.CreateDestroy = void 0; | ||
exports.initLock = exports.destroyed = exports.ready = exports.CreateDestroy = void 0; | ||
const async_mutex_1 = require("async-mutex"); | ||
const utils_1 = require("./utils"); | ||
const errors_1 = require("./errors"); | ||
/** | ||
* Symbols prevents name clashes with decorated classes | ||
*/ | ||
const _destroyed = Symbol('_destroyed'); | ||
const destroyed = Symbol('destroyed'); | ||
exports.destroyed = destroyed; | ||
const initLock = Symbol('initLock'); | ||
exports.initLock = initLock; | ||
function CreateDestroy() { | ||
return (constructor) => { | ||
var _a, _b; | ||
return class extends constructor { | ||
constructor() { | ||
super(...arguments); | ||
this._destroyed = false; | ||
this[_a] = false; | ||
this[_b] = new async_mutex_1.Mutex(); | ||
} | ||
get destroyed() { | ||
return this._destroyed; | ||
get [(_a = _destroyed, _b = initLock, destroyed)]() { | ||
return this[_destroyed]; | ||
} | ||
async destroy(...args) { | ||
const release = await this[initLock].acquire(); | ||
try { | ||
if (this._destroyed) { | ||
if (this[_destroyed]) { | ||
return; | ||
} | ||
this._destroyed = true; | ||
let result; | ||
if (typeof super['destroy'] === 'function') { | ||
return await super.destroy(...args); | ||
result = await super.destroy(...args); | ||
} | ||
this[_destroyed] = true; | ||
return result; | ||
} | ||
catch (e) { | ||
this._destroyed = false; | ||
throw e; | ||
finally { | ||
release(); | ||
} | ||
@@ -35,3 +48,3 @@ } | ||
exports.CreateDestroy = CreateDestroy; | ||
function ready(errorDestroyed = new errors_1.ErrorAsyncInitDestroyed()) { | ||
function ready(errorDestroyed = new errors_1.ErrorAsyncInitDestroyed(), wait = false) { | ||
return (target, key, descriptor) => { | ||
@@ -54,3 +67,11 @@ let kind; | ||
descriptor[kind] = async function (...args) { | ||
if (this._destroyed) { | ||
if (wait) { | ||
await this[initLock].waitForUnlock(); | ||
} | ||
else { | ||
if (this[initLock].isLocked()) { | ||
throw errorDestroyed; | ||
} | ||
} | ||
if (this[_destroyed]) { | ||
throw errorDestroyed; | ||
@@ -63,5 +84,10 @@ } | ||
descriptor[kind] = function* (...args) { | ||
if (this._destroyed) { | ||
// If locked, it is during destroy | ||
// Consider it already destroyed | ||
if (this[initLock].isLocked()) { | ||
throw errorDestroyed; | ||
} | ||
if (this[_destroyed]) { | ||
throw errorDestroyed; | ||
} | ||
yield* f.apply(this, args); | ||
@@ -72,3 +98,11 @@ }; | ||
descriptor[kind] = async function* (...args) { | ||
if (this._destroyed) { | ||
if (wait) { | ||
await this[initLock].waitForUnlock(); | ||
} | ||
else { | ||
if (this[initLock].isLocked()) { | ||
throw errorDestroyed; | ||
} | ||
} | ||
if (this[_destroyed]) { | ||
throw errorDestroyed; | ||
@@ -81,5 +115,10 @@ } | ||
descriptor[kind] = function (...args) { | ||
if (this._destroyed) { | ||
// If locked, it is during destroy | ||
// Consider it already destroyed | ||
if (this[initLock].isLocked()) { | ||
throw errorDestroyed; | ||
} | ||
if (this[_destroyed]) { | ||
throw errorDestroyed; | ||
} | ||
return f.apply(this, args); | ||
@@ -86,0 +125,0 @@ }; |
@@ -1,24 +0,35 @@ | ||
interface CreateDestroyStartStop { | ||
get running(): boolean; | ||
get destroyed(): boolean; | ||
start(...args: Array<any>): Promise<any>; | ||
stop(...args: Array<any>): Promise<any>; | ||
destroy(...args: Array<any>): Promise<any>; | ||
import { Mutex } from 'async-mutex'; | ||
/** | ||
* Symbols prevents name clashes with decorated classes | ||
*/ | ||
declare const _running: unique symbol; | ||
declare const running: unique symbol; | ||
declare const _destroyed: unique symbol; | ||
declare const destroyed: unique symbol; | ||
declare const initLock: unique symbol; | ||
interface CreateDestroyStartStop<StartReturn = unknown, StopReturn = unknown, DestroyReturn = unknown> { | ||
get [running](): boolean; | ||
get [destroyed](): boolean; | ||
readonly [initLock]: Mutex; | ||
start(...args: Array<any>): Promise<StartReturn | void>; | ||
stop(...args: Array<any>): Promise<StopReturn | void>; | ||
destroy(...args: Array<any>): Promise<DestroyReturn | void>; | ||
} | ||
declare function CreateDestroyStartStop(errorRunning?: Error, errorDestroyed?: Error): <T extends new (...args: any[]) => { | ||
start?(...args: Array<any>): Promise<any>; | ||
stop?(...args: Array<any>): Promise<any>; | ||
destroy?(...args: Array<any>): Promise<any>; | ||
declare function CreateDestroyStartStop<StartReturn = unknown, StopReturn = unknown, DestroyReturn = unknown>(errorRunning?: Error, errorDestroyed?: Error): <T extends new (...args: any[]) => { | ||
start?(...args: Array<any>): Promise<StartReturn | void>; | ||
stop?(...args: Array<any>): Promise<StopReturn | void>; | ||
destroy?(...args: Array<any>): Promise<DestroyReturn | void>; | ||
}>(constructor: T) => { | ||
new (...args: any[]): { | ||
_running: boolean; | ||
_destroyed: boolean; | ||
readonly running: boolean; | ||
readonly destroyed: boolean; | ||
destroy(...args: Array<any>): Promise<any>; | ||
start(...args: Array<any>): Promise<any>; | ||
stop(...args: Array<any>): Promise<any>; | ||
destroy(...args: Array<any>): Promise<DestroyReturn | void>; | ||
start(...args: Array<any>): Promise<StartReturn | void>; | ||
stop(...args: Array<any>): Promise<StopReturn | void>; | ||
[_running]: boolean; | ||
[_destroyed]: boolean; | ||
readonly [initLock]: Mutex; | ||
readonly [running]: boolean; | ||
readonly [destroyed]: boolean; | ||
}; | ||
} & T; | ||
declare function ready(errorNotRunning?: Error): (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
export { CreateDestroyStartStop, ready }; | ||
declare function ready(errorNotRunning?: Error, wait?: boolean): (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
export { CreateDestroyStartStop, ready, running, destroyed, initLock }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ready = exports.CreateDestroyStartStop = void 0; | ||
exports.initLock = exports.destroyed = exports.running = exports.ready = exports.CreateDestroyStartStop = void 0; | ||
const async_mutex_1 = require("async-mutex"); | ||
const utils_1 = require("./utils"); | ||
const errors_1 = require("./errors"); | ||
/** | ||
* Symbols prevents name clashes with decorated classes | ||
*/ | ||
const _running = Symbol('_running'); | ||
const running = Symbol('running'); | ||
exports.running = running; | ||
const _destroyed = Symbol('_destroyed'); | ||
const destroyed = Symbol('destroyed'); | ||
exports.destroyed = destroyed; | ||
const initLock = Symbol('initLock'); | ||
exports.initLock = initLock; | ||
function CreateDestroyStartStop(errorRunning = new errors_1.ErrorAsyncInitRunning(), errorDestroyed = new errors_1.ErrorAsyncInitDestroyed()) { | ||
return (constructor) => { | ||
var _a, _b, _c; | ||
return class extends constructor { | ||
constructor() { | ||
super(...arguments); | ||
this._running = false; | ||
this._destroyed = false; | ||
this[_a] = false; | ||
this[_b] = false; | ||
this[_c] = new async_mutex_1.Mutex(); | ||
} | ||
get running() { | ||
return this._running; | ||
get [(_a = _running, _b = _destroyed, _c = initLock, running)]() { | ||
return this[_running]; | ||
} | ||
get destroyed() { | ||
return this._destroyed; | ||
get [destroyed]() { | ||
return this[_destroyed]; | ||
} | ||
async destroy(...args) { | ||
const release = await this[initLock].acquire(); | ||
try { | ||
if (this._destroyed) { | ||
if (this[_destroyed]) { | ||
return; | ||
} | ||
if (this._running) { | ||
if (this[_running]) { | ||
throw errorRunning; | ||
} | ||
this._destroyed = true; | ||
let result; | ||
if (typeof super['destroy'] === 'function') { | ||
return await super.destroy(...args); | ||
result = await super.destroy(...args); | ||
} | ||
this[_destroyed] = true; | ||
return result; | ||
} | ||
catch (e) { | ||
this._destroyed = false; | ||
throw e; | ||
finally { | ||
release(); | ||
} | ||
} | ||
async start(...args) { | ||
const release = await this[initLock].acquire(); | ||
try { | ||
if (this._running) { | ||
if (this[_running]) { | ||
return; | ||
} | ||
if (this._destroyed) { | ||
if (this[_destroyed]) { | ||
throw errorDestroyed; | ||
} | ||
this._running = true; | ||
let result; | ||
if (typeof super['start'] === 'function') { | ||
return await super.start(...args); | ||
result = await super.start(...args); | ||
} | ||
this[_running] = true; | ||
return result; | ||
} | ||
catch (e) { | ||
this._running = false; | ||
throw e; | ||
finally { | ||
release(); | ||
} | ||
} | ||
async stop(...args) { | ||
const release = await this[initLock].acquire(); | ||
try { | ||
if (!this._running) { | ||
if (!this[_running]) { | ||
return; | ||
} | ||
if (this._destroyed) { | ||
if (this[_destroyed]) { | ||
// It is not possible to be running and destroyed | ||
@@ -66,10 +85,11 @@ // however this line is here for completion | ||
} | ||
this._running = false; | ||
let result; | ||
if (typeof super['stop'] === 'function') { | ||
return await super.stop(...args); | ||
result = await super.stop(...args); | ||
} | ||
this[_running] = false; | ||
return result; | ||
} | ||
catch (e) { | ||
this._running = true; | ||
throw e; | ||
finally { | ||
release(); | ||
} | ||
@@ -81,3 +101,3 @@ } | ||
exports.CreateDestroyStartStop = CreateDestroyStartStop; | ||
function ready(errorNotRunning = new errors_1.ErrorAsyncInitNotRunning()) { | ||
function ready(errorNotRunning = new errors_1.ErrorAsyncInitNotRunning(), wait = false) { | ||
return (target, key, descriptor) => { | ||
@@ -100,3 +120,11 @@ let kind; | ||
descriptor[kind] = async function (...args) { | ||
if (!this._running) { | ||
if (wait) { | ||
await this[initLock].waitForUnlock(); | ||
} | ||
else { | ||
if (this[initLock].isLocked()) { | ||
throw errorNotRunning; | ||
} | ||
} | ||
if (!this[_running]) { | ||
throw errorNotRunning; | ||
@@ -109,5 +137,8 @@ } | ||
descriptor[kind] = function* (...args) { | ||
if (!this._running) { | ||
if (this[initLock].isLocked()) { | ||
throw errorNotRunning; | ||
} | ||
if (!this[_running]) { | ||
throw errorNotRunning; | ||
} | ||
yield* f.apply(this, args); | ||
@@ -118,3 +149,11 @@ }; | ||
descriptor[kind] = async function* (...args) { | ||
if (!this._running) { | ||
if (wait) { | ||
await this[initLock].waitForUnlock(); | ||
} | ||
else { | ||
if (this[initLock].isLocked()) { | ||
throw errorNotRunning; | ||
} | ||
} | ||
if (!this[_running]) { | ||
throw errorNotRunning; | ||
@@ -127,5 +166,8 @@ } | ||
descriptor[kind] = function (...args) { | ||
if (!this._running) { | ||
if (this[initLock].isLocked()) { | ||
throw errorNotRunning; | ||
} | ||
if (!this[_running]) { | ||
throw errorNotRunning; | ||
} | ||
return f.apply(this, args); | ||
@@ -132,0 +174,0 @@ }; |
@@ -1,18 +0,27 @@ | ||
interface StartStop { | ||
get running(): boolean; | ||
start(...args: Array<any>): Promise<any>; | ||
stop(...args: Array<any>): Promise<any>; | ||
import { Mutex } from 'async-mutex'; | ||
/** | ||
* Symbols prevents name clashes with decorated classes | ||
*/ | ||
declare const _running: unique symbol; | ||
declare const running: unique symbol; | ||
declare const initLock: unique symbol; | ||
interface StartStop<StartReturn = unknown, StopReturn = unknown> { | ||
get [running](): boolean; | ||
readonly [initLock]: Mutex; | ||
start(...args: Array<any>): Promise<StartReturn | void>; | ||
stop(...args: Array<any>): Promise<StopReturn | void>; | ||
} | ||
declare function StartStop(): <T extends new (...args: any[]) => { | ||
start?(...args: Array<any>): Promise<any>; | ||
stop?(...args: Array<any>): Promise<any>; | ||
declare function StartStop<StartReturn = unknown, StopReturn = unknown>(): <T extends new (...args: any[]) => { | ||
start?(...args: Array<any>): Promise<StartReturn | void>; | ||
stop?(...args: Array<any>): Promise<StopReturn | void>; | ||
}>(constructor: T) => { | ||
new (...args: any[]): { | ||
_running: boolean; | ||
readonly running: boolean; | ||
start(...args: Array<any>): Promise<any>; | ||
stop(...args: Array<any>): Promise<any>; | ||
start(...args: Array<any>): Promise<StartReturn | void>; | ||
stop(...args: Array<any>): Promise<StopReturn | void>; | ||
[_running]: boolean; | ||
readonly [initLock]: Mutex; | ||
readonly [running]: boolean; | ||
}; | ||
} & T; | ||
declare function ready(errorNotRunning?: Error): (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
export { StartStop, ready }; | ||
declare function ready(errorNotRunning?: Error, wait?: boolean): (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
export { StartStop, ready, running, initLock }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ready = exports.StartStop = void 0; | ||
exports.initLock = exports.running = exports.ready = exports.StartStop = void 0; | ||
const async_mutex_1 = require("async-mutex"); | ||
const utils_1 = require("./utils"); | ||
const errors_1 = require("./errors"); | ||
/** | ||
* Symbols prevents name clashes with decorated classes | ||
*/ | ||
const _running = Symbol('_running'); | ||
const running = Symbol('running'); | ||
exports.running = running; | ||
const initLock = Symbol('initLock'); | ||
exports.initLock = initLock; | ||
function StartStop() { | ||
return (constructor) => { | ||
var _a, _b; | ||
return class extends constructor { | ||
constructor() { | ||
super(...arguments); | ||
this._running = false; | ||
this[_a] = false; | ||
this[_b] = new async_mutex_1.Mutex(); | ||
} | ||
get running() { | ||
return this._running; | ||
get [(_a = _running, _b = initLock, running)]() { | ||
return this[_running]; | ||
} | ||
async start(...args) { | ||
const release = await this[initLock].acquire(); | ||
try { | ||
if (this._running) { | ||
if (this[_running]) { | ||
return; | ||
} | ||
this._running = true; | ||
let result; | ||
if (typeof super['start'] === 'function') { | ||
return await super.start(...args); | ||
result = await super.start(...args); | ||
} | ||
this[_running] = true; | ||
return result; | ||
} | ||
catch (e) { | ||
this._running = false; | ||
throw e; | ||
finally { | ||
release(); | ||
} | ||
} | ||
async stop(...args) { | ||
const release = await this[initLock].acquire(); | ||
try { | ||
if (!this._running) { | ||
if (!this[_running]) { | ||
return; | ||
} | ||
this._running = false; | ||
let result; | ||
if (typeof super['stop'] === 'function') { | ||
return await super.stop(...args); | ||
result = await super.stop(...args); | ||
} | ||
this[_running] = false; | ||
return result; | ||
} | ||
catch (e) { | ||
this._running = true; | ||
throw e; | ||
finally { | ||
release(); | ||
} | ||
@@ -50,3 +65,3 @@ } | ||
exports.StartStop = StartStop; | ||
function ready(errorNotRunning = new errors_1.ErrorAsyncInitNotRunning()) { | ||
function ready(errorNotRunning = new errors_1.ErrorAsyncInitNotRunning(), wait = false) { | ||
return (target, key, descriptor) => { | ||
@@ -69,3 +84,11 @@ let kind; | ||
descriptor[kind] = async function (...args) { | ||
if (!this._running) { | ||
if (wait) { | ||
await this[initLock].waitForUnlock(); | ||
} | ||
else { | ||
if (this[initLock].isLocked()) { | ||
throw errorNotRunning; | ||
} | ||
} | ||
if (!this[_running]) { | ||
throw errorNotRunning; | ||
@@ -78,5 +101,8 @@ } | ||
descriptor[kind] = function* (...args) { | ||
if (!this._running) { | ||
if (this[initLock].isLocked()) { | ||
throw errorNotRunning; | ||
} | ||
if (!this[_running]) { | ||
throw errorNotRunning; | ||
} | ||
yield* f.apply(this, args); | ||
@@ -87,3 +113,11 @@ }; | ||
descriptor[kind] = async function* (...args) { | ||
if (!this._running) { | ||
if (wait) { | ||
await this[initLock].waitForUnlock(); | ||
} | ||
else { | ||
if (this[initLock].isLocked()) { | ||
throw errorNotRunning; | ||
} | ||
} | ||
if (!this[_running]) { | ||
throw errorNotRunning; | ||
@@ -96,5 +130,8 @@ } | ||
descriptor[kind] = function (...args) { | ||
if (!this._running) { | ||
if (this[initLock].isLocked()) { | ||
throw errorNotRunning; | ||
} | ||
if (!this[_running]) { | ||
throw errorNotRunning; | ||
} | ||
return f.apply(this, args); | ||
@@ -101,0 +138,0 @@ }; |
{ | ||
"name": "@matrixai/async-init", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"author": "Roger Qiu", | ||
@@ -22,2 +22,3 @@ "description": "Asynchronous Initialisation and Deinitialisation Decorators", | ||
"dependencies": { | ||
"async-mutex": "^0.3.2", | ||
"ts-custom-error": "^3.2.0" | ||
@@ -24,0 +25,0 @@ }, |
@@ -9,2 +9,4 @@ # js-async-init | ||
TypeScript does not allow decorator properties that are protected or private. | ||
Example Usage: | ||
@@ -11,0 +13,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50581
597
142
2
+ Addedasync-mutex@^0.3.2
+ Addedasync-mutex@0.3.2(transitive)
+ Addedtslib@2.8.1(transitive)