@matrixai/async-init
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -37,8 +37,18 @@ "use strict"; | ||
return (target, key, descriptor) => { | ||
const f = descriptor.value; | ||
let kind; | ||
if (descriptor.value != null) { | ||
kind = 'value'; | ||
} | ||
else if (descriptor.get != null) { | ||
kind = 'get'; | ||
} | ||
else if (descriptor.set != null) { | ||
kind = 'set'; | ||
} | ||
const f = descriptor[kind]; | ||
if (typeof f !== 'function') { | ||
throw new TypeError(`${key} is not a function`); | ||
} | ||
if (descriptor.value instanceof utils_1.AsyncFunction) { | ||
descriptor.value = async function (...args) { | ||
if (f instanceof utils_1.AsyncFunction) { | ||
descriptor[kind] = async function (...args) { | ||
if (this._destroyed) { | ||
@@ -50,4 +60,4 @@ throw errorDestroyed; | ||
} | ||
else if (descriptor.value instanceof utils_1.GeneratorFunction) { | ||
descriptor.value = function* (...args) { | ||
else if (f instanceof utils_1.GeneratorFunction) { | ||
descriptor[kind] = function* (...args) { | ||
if (this._destroyed) { | ||
@@ -59,4 +69,4 @@ throw errorDestroyed; | ||
} | ||
else if (descriptor.value instanceof utils_1.AsyncGeneratorFunction) { | ||
descriptor.value = async function* (...args) { | ||
else if (f instanceof utils_1.AsyncGeneratorFunction) { | ||
descriptor[kind] = async function* (...args) { | ||
if (this._destroyed) { | ||
@@ -69,3 +79,3 @@ throw errorDestroyed; | ||
else { | ||
descriptor.value = function (...args) { | ||
descriptor[kind] = function (...args) { | ||
if (this._destroyed) { | ||
@@ -78,3 +88,3 @@ throw errorDestroyed; | ||
// Preserve the name | ||
Object.defineProperty(descriptor.value, 'name', { value: key }); | ||
Object.defineProperty(descriptor[kind], 'name', { value: key }); | ||
return descriptor; | ||
@@ -81,0 +91,0 @@ }; |
@@ -82,8 +82,18 @@ "use strict"; | ||
return (target, key, descriptor) => { | ||
const f = descriptor.value; | ||
let kind; | ||
if (descriptor.value != null) { | ||
kind = 'value'; | ||
} | ||
else if (descriptor.get != null) { | ||
kind = 'get'; | ||
} | ||
else if (descriptor.set != null) { | ||
kind = 'set'; | ||
} | ||
const f = descriptor[kind]; | ||
if (typeof f !== 'function') { | ||
throw new TypeError(`${key} is not a function`); | ||
} | ||
if (descriptor.value instanceof utils_1.AsyncFunction) { | ||
descriptor.value = async function (...args) { | ||
if (f instanceof utils_1.AsyncFunction) { | ||
descriptor[kind] = async function (...args) { | ||
if (!this._running) { | ||
@@ -95,4 +105,4 @@ throw errorNotRunning; | ||
} | ||
else if (descriptor.value instanceof utils_1.GeneratorFunction) { | ||
descriptor.value = function* (...args) { | ||
else if (f instanceof utils_1.GeneratorFunction) { | ||
descriptor[kind] = function* (...args) { | ||
if (!this._running) { | ||
@@ -104,4 +114,4 @@ throw errorNotRunning; | ||
} | ||
else if (descriptor.value instanceof utils_1.AsyncGeneratorFunction) { | ||
descriptor.value = async function* (...args) { | ||
else if (f instanceof utils_1.AsyncGeneratorFunction) { | ||
descriptor[kind] = async function* (...args) { | ||
if (!this._running) { | ||
@@ -114,3 +124,3 @@ throw errorNotRunning; | ||
else { | ||
descriptor.value = function (...args) { | ||
descriptor[kind] = function (...args) { | ||
if (!this._running) { | ||
@@ -123,3 +133,3 @@ throw errorNotRunning; | ||
// Preserve the name | ||
Object.defineProperty(descriptor.value, 'name', { value: key }); | ||
Object.defineProperty(descriptor[kind], 'name', { value: key }); | ||
return descriptor; | ||
@@ -126,0 +136,0 @@ }; |
@@ -52,8 +52,18 @@ "use strict"; | ||
return (target, key, descriptor) => { | ||
const f = descriptor.value; | ||
let kind; | ||
if (descriptor.value != null) { | ||
kind = 'value'; | ||
} | ||
else if (descriptor.get != null) { | ||
kind = 'get'; | ||
} | ||
else if (descriptor.set != null) { | ||
kind = 'set'; | ||
} | ||
const f = descriptor[kind]; | ||
if (typeof f !== 'function') { | ||
throw new TypeError(`${key} is not a function`); | ||
} | ||
if (descriptor.value instanceof utils_1.AsyncFunction) { | ||
descriptor.value = async function (...args) { | ||
if (f instanceof utils_1.AsyncFunction) { | ||
descriptor[kind] = async function (...args) { | ||
if (!this._running) { | ||
@@ -65,4 +75,4 @@ throw errorNotRunning; | ||
} | ||
else if (descriptor.value instanceof utils_1.GeneratorFunction) { | ||
descriptor.value = function* (...args) { | ||
else if (f instanceof utils_1.GeneratorFunction) { | ||
descriptor[kind] = function* (...args) { | ||
if (!this._running) { | ||
@@ -74,4 +84,4 @@ throw errorNotRunning; | ||
} | ||
else if (descriptor.value instanceof utils_1.AsyncGeneratorFunction) { | ||
descriptor.value = async function* (...args) { | ||
else if (f instanceof utils_1.AsyncGeneratorFunction) { | ||
descriptor[kind] = async function* (...args) { | ||
if (!this._running) { | ||
@@ -84,3 +94,3 @@ throw errorNotRunning; | ||
else { | ||
descriptor.value = function (...args) { | ||
descriptor[kind] = function (...args) { | ||
if (!this._running) { | ||
@@ -93,3 +103,3 @@ throw errorNotRunning; | ||
// Preserve the name | ||
Object.defineProperty(descriptor.value, 'name', { value: key }); | ||
Object.defineProperty(descriptor[kind], 'name', { value: key }); | ||
return descriptor; | ||
@@ -96,0 +106,0 @@ }; |
{ | ||
"name": "@matrixai/async-init", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Roger Qiu", | ||
@@ -14,3 +14,3 @@ "description": "Asynchronous Initialisation and Deinitialisation Decorators", | ||
"scripts": { | ||
"build": "tsc -p ./tsconfig.build.json", | ||
"build": "rm -r ./dist || true; tsc -p ./tsconfig.build.json", | ||
"ts-node": "ts-node -r tsconfig-paths/register", | ||
@@ -20,3 +20,3 @@ "test": "jest", | ||
"lintfix": "eslint '{src,tests,benches}/**/*.{js,ts}' --fix", | ||
"docs": "typedoc --tsconfig ./tsconfig.build.json --out ./docs src && touch ./docs/.nojekyll" | ||
"docs": "rm -r ./docs || true; typedoc --gitRevision master --tsconfig ./tsconfig.build.json --out ./docs src && touch ./docs/.nojekyll" | ||
}, | ||
@@ -23,0 +23,0 @@ "dependencies": { |
@@ -5,4 +5,6 @@ # js-async-init | ||
Asynchronous initialisation and deinitilisation decorators for JavaScript/TypeScript applications. | ||
Asynchronous initialization and deinitialization decorators for JavaScript/TypeScript applications. | ||
Because decorators are experimental, you must enable: `"experimentalDecorators": true` in your `tsconfig.json` to use this library. | ||
Example Usage: | ||
@@ -13,2 +15,3 @@ | ||
// this hack is necessary to ensure that X's type is decorated | ||
interface X extends CreateDestroyStartStop {}; | ||
@@ -58,2 +61,3 @@ @CreateDestroyStartStop(new Error('Running'), new Error('Destroyed')) | ||
// this hack is necessary to ensure that Y's type is decorated | ||
interface Y extends CreateDestroyStartStop {}; | ||
@@ -92,2 +96,6 @@ @CreateDestroyStartStop(new Error('Running'), new Error('Destroyed')) | ||
Note that `start`, `stop` and `destroy` are idempotent. This means repeated calls are noops. | ||
Refer to https://gist.github.com/CMCDragonkai/1dbf5069d9efc11585c27cc774271584 for further the motivation of this library. | ||
## Installation | ||
@@ -124,3 +132,3 @@ | ||
See the docs at: https://matrixai.github.io/TypeScript-Demo-Lib/ | ||
See the docs at: https://matrixai.github.io/js-async-init/ | ||
@@ -127,0 +135,0 @@ ### Publishing |
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
41265
450
140