async-injection
Version:
A robust lightweight dependency injection library for TypeScript.
48 lines • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvokeReleaseMethod = exports.isPromise = exports.isErrorObj = void 0;
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */
const constants_js_1 = require("./constants.js");
/**
* Returns true if the specified object looks like a JavaScript Error object.
*/
function isErrorObj(err) {
if (!err)
return false;
if (err instanceof Error)
return true;
return err && typeof err.message === 'string' && typeof err.stack === 'string';
}
exports.isErrorObj = isErrorObj;
/**
* Returns true if the specified value is "thenable" (aka a Promise).
*/
function isPromise(value) {
if (!value)
return false;
if (value instanceof Promise)
return true;
return value && typeof value.then === 'function';
}
exports.isPromise = isPromise;
/**
* Simple helper function to find the @Release decorated method of an object (if any), and invoke it.
* This is primarily an internal method as you probably know the exact method, and should invoke it yourself.
* async-injection uses this helper to allow Singletons to clean up any non-garbage-collectable resources they may have allocated.
*/
function InvokeReleaseMethod(obj) {
var _a, _b;
const releaseMethod = Reflect.getMetadata(constants_js_1.RELEASE_METADATA_KEY, obj.constructor);
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
if (releaseMethod && obj.constructor.prototype[releaseMethod] && typeof obj.constructor.prototype[releaseMethod] === 'function') {
const releaseFn = (_b = (_a = obj[releaseMethod]).bind) === null || _b === void 0 ? void 0 : _b.call(_a, obj);
if (releaseFn) {
releaseFn();
return true;
}
}
/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
return false;
}
exports.InvokeReleaseMethod = InvokeReleaseMethod;
//# sourceMappingURL=utils.js.map