Skip to content

Commit

Permalink
fix: allow application.stop() to release release resources from init()
Browse files Browse the repository at this point in the history
Some applications create resources during `init()` and `stop()` does not
clean up such resources at the moment.

Signed-off-by: Raymond Feng <enjoyjava@gmail.com>
  • Loading branch information
raymondfeng committed Nov 10, 2021
1 parent 3b56625 commit c4487b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Expand Up @@ -140,6 +140,14 @@ describe('Application life cycle', () => {
expect(app.state).to.equal('created');
});

it('allows application.stop when it is initialized', async () => {
const app = new Application();
await app.init();
expect(app.state).to.equal('initialized');
await app.stop();
expect(app.state).to.equal('stopped');
});

it('await application.stop when it is stopping', async () => {
const app = new Application();
await app.start();
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/application.ts
Expand Up @@ -88,8 +88,8 @@ export class Application extends Context implements LifeCycleObserver {
* - !started -> starting -> started
* - started -> started (no-op)
* 2. stop
* - started -> stopping -> stopped
* - !started -> stopped (no-op)
* - (started | initialized) -> stopping -> stopped
* - ! (started || initialized) -> stopped (no-op)
*
* Two types of states are expected:
* - stable, such as `started` and `stopped`
Expand Down Expand Up @@ -399,7 +399,7 @@ export class Application extends Context implements LifeCycleObserver {
if (this._state === 'stopping') return this.awaitState('stopped');
this.assertNotInProcess('stop');
// No-op if it's created or stopped
if (this._state !== 'started') return;
if (this._state !== 'started' && this._state !== 'initialized') return;
this.setState('stopping');
if (!this._isShuttingDown) {
// Explicit stop is called, let's remove signal listeners to avoid
Expand Down

0 comments on commit c4487b1

Please sign in to comment.