UNPKG

async-injection

Version:

A robust lightweight dependency injection library for TypeScript.

42 lines 1.82 kB
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */ import { RELEASE_METADATA_KEY } from './constants.js'; /** * Returns true if the specified object looks like a JavaScript Error object. */ export 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). */ export 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. */ export function InvokeReleaseMethod(obj) { var _a, _b; const releaseMethod = Reflect.getMetadata(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; } //# sourceMappingURL=utils.js.map