UNPKG

async-injection

Version:

A robust lightweight dependency injection library for TypeScript.

45 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isErrorObj = isErrorObj; exports.isPromise = isPromise; exports.InvokeReleaseMethod = InvokeReleaseMethod; const constants_1 = require("./constants"); /** * 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'); } /** * 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'); } /** * 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 o = obj; const releaseMethod = Reflect.getMetadata(constants_1.RELEASE_METADATA_KEY, o.constructor); if (releaseMethod && o.constructor.prototype[releaseMethod] && typeof o.constructor.prototype[releaseMethod] === 'function') { const releaseFn = (_b = (_a = o[releaseMethod]).bind) === null || _b === void 0 ? void 0 : _b.call(_a, o); if (releaseFn) { releaseFn(); return true; } } return false; } //# sourceMappingURL=utils.js.map