feat(server): add unload method support for serverMiddleware (#7358)

* feat: add unload method support for serverMiddleware

* chore: update test

Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
Sébastien Chopin 2020-05-13 17:16:09 +02:00 committed by GitHub
parent 2e5184688f
commit 9b29124d31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -292,6 +292,9 @@ export default class Server {
return return
} }
// unload middleware
this.unloadMiddleware(serverStackItem)
// Resolve middleware // Resolve middleware
const { route, handle } = this.resolveMiddleware(middleware, serverStackItem.route) const { route, handle } = this.resolveMiddleware(middleware, serverStackItem.route)
@ -305,6 +308,12 @@ export default class Server {
return serverStackItem return serverStackItem
} }
unloadMiddleware ({ handle }) {
if (handle._middleware && typeof handle._middleware.unload === 'function') {
handle._middleware.unload()
}
}
serverMiddlewarePaths () { serverMiddlewarePaths () {
return this.app.stack.map(({ handle }) => handle._middleware && handle._middleware.entry).filter(Boolean) return this.app.stack.map(({ handle }) => handle._middleware && handle._middleware.entry).filter(Boolean)
} }
@ -369,6 +378,7 @@ export default class Server {
await this.renderer.close() await this.renderer.close()
} }
this.app.stack.forEach(this.unloadMiddleware)
this.app.removeAllListeners() this.app.removeAllListeners()
this.app = null this.app = null

View File

@ -504,7 +504,7 @@ describe('server: server', () => {
test('should close server', async () => { test('should close server', async () => {
const removeAllListeners = jest.fn() const removeAllListeners = jest.fn()
connect.mockReturnValueOnce({ use: jest.fn(), removeAllListeners }) connect.mockReturnValueOnce({ use: jest.fn(), stack: [], removeAllListeners })
const nuxt = createNuxt() const nuxt = createNuxt()
const server = new Server(nuxt) const server = new Server(nuxt)
const listener = { close: jest.fn() } const listener = { close: jest.fn() }
@ -525,7 +525,7 @@ describe('server: server', () => {
test('should prevent closing server multiple times', async () => { test('should prevent closing server multiple times', async () => {
const removeAllListeners = jest.fn() const removeAllListeners = jest.fn()
connect.mockReturnValueOnce({ use: jest.fn(), removeAllListeners }) connect.mockReturnValueOnce({ use: jest.fn(), stack: [], removeAllListeners })
const nuxt = createNuxt() const nuxt = createNuxt()
const server = new Server(nuxt) const server = new Server(nuxt)
server.renderer = {} server.renderer = {}