hapijs / hapijs/hapi

Resource cleanup

Open
#4,358 10 comments 0 reactions 0 assignees View on GitHub
support
Dominant language
JavaScript
Stars
14.8k
Forks
1.4k
Avg merge
22d 3h
Merged PRs (30d)
1

Description

#### Support plan

* *is this issue currently blocking your project?* (yes/no): no
* *is this issue affecting a production system?* (yes/no): no

#### Context

* *node version*: v16.15.1
* *module version*: 20.2.2
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...):custom plugin
* *any other relevant information*:

#### How can we help?

I am writing a plugin. I want that plugin to expose a method that can create a particular resource on a given request (e.g. a db connection). This is easily achieved (e.g. by decorating the `toolkit`).

I would like that:
1) Multiple calls to request the same resource return the same instance. This is easy.
2) The resource gets cleaned up (once) at the end of the request. **This I am not sure how to achieve**.

There is a `finish` event but this one is not triggered for requests that do not return a payload (GET/HEAD). There is an `onPostResponse` lifecycle hook but this requires to either define the listener for every route, or at the server level.

Is there a way to dynamically add a listener to be run during the `onPostResponse` life cycle ?
Is there a "flag" that can be used to detect that `onPostResponse` should not be called anymore ?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.