From 1cc45ba2b5a3304f9f338a0f0463e47620b0575b Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Sun, 30 Jul 2023 19:06:40 +0300 Subject: [PATCH] Refactor `Code.stop` --- lib/application.js | 15 +++------------ lib/code.js | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/application.js b/lib/application.js index 8c2b81d0..f0d7561b 100644 --- a/lib/application.js +++ b/lib/application.js @@ -111,22 +111,13 @@ class Application extends node.events.EventEmitter { async shutdown() { this.finalization = true; - await this.stopPlace('domain'); - await this.stopPlace('db'); - await this.stopPlace('lib'); + await this.domain.stop(); + await this.db.stop(); + await this.lib.stop(); if (this.server) await this.server.close(); if (this.logger) await this.logger.close(); } - async stopPlace(name) { - if (!this.sandbox) return; - const place = this[name]; - for (const moduleName of Object.keys(place)) { - const module = place[moduleName]; - if (typeof module.stop === 'function') await this.execute(module.stop); - } - } - createSandbox() { const { config, console, resources, schemas } = this; const { server: { host, port, protocol } = {} } = this; diff --git a/lib/code.js b/lib/code.js index cb847215..9fd4c066 100644 --- a/lib/code.js +++ b/lib/code.js @@ -10,11 +10,20 @@ class Code extends Place { this.tree = {}; } - stop(name, method) { + async stop() { + for (const moduleName of Object.keys(this.tree)) { + const module = this.tree[moduleName]; + if (typeof module.stop === 'function') { + await this.application.execute(module.stop); + } + } + } + + stopModule(name, module) { const timeout = this.application.config.server.timeouts.watch; setTimeout(() => { if (this.tree[name] !== undefined) return; - this.application.execute(method); + this.application.execute(module.stop); }, timeout); } @@ -27,7 +36,7 @@ class Code extends Place { let next = level[name]; if (depth === last) { if (unit === null) { - if (name === 'stop') this.stop(names[0], level.stop); + if (name === 'stop') this.stopModule(names[0], level); delete level[name]; return; }