Skip to content

Commit

Permalink
Refactor Code.stop
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Jul 30, 2023
1 parent cd4372e commit 1cc45ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
15 changes: 3 additions & 12 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions lib/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 1cc45ba

Please sign in to comment.