UNPKG

@nestjs/core

Version:

Nest - modern, fast, powerful node.js web framework (@core)

46 lines (45 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const shared_utils_1 = require("@nestjs/common/utils/shared.utils"); const iterare_1 = require("iterare"); const transient_instances_1 = require("../injector/transient-instances"); /** * Returns true or false if the given instance has a `onModuleDestroy` function * * @param instance The instance which should be checked */ function hasOnModuleDestroyHook(instance) { return !shared_utils_1.isNil(instance.onModuleDestroy); } /** * Calls the given instances onModuleDestroy hook */ function callOperator(instances) { return iterare_1.default(instances) .filter(instance => !shared_utils_1.isNil(instance)) .filter(hasOnModuleDestroyHook) .map(async (instance) => instance.onModuleDestroy()) .toArray(); } /** * Calls the `onModuleDestroy` function on the module and its children * (providers / controllers). * * @param module The module which will be initialized */ async function callModuleDestroyHook(module) { const providers = [...module.providers]; // Module (class) instance is the first element of the providers array // Lifecycle hook has to be called once all classes are properly destroyed const [_, { instance: moduleClassInstance }] = providers.shift(); const instances = [...module.controllers, ...providers]; const nonTransientInstances = transient_instances_1.getNonTransientInstances(instances); await Promise.all(callOperator(nonTransientInstances)); const transientInstances = transient_instances_1.getTransientInstances(instances); await Promise.all(callOperator(transientInstances)); // Call the module instance itself if (moduleClassInstance && hasOnModuleDestroyHook(moduleClassInstance)) { await moduleClassInstance.onModuleDestroy(); } } exports.callModuleDestroyHook = callModuleDestroyHook;