@@ -1897,6 +1897,219 @@ a code.
18971897Specifying a code to [` process .exit (code)` ][` process .exit ()` ] will override any
18981898previous setting of ` process .exitCode ` .
18991899
1900+ ## ` process .finalization .register (ref, callback)`
1901+
1902+ <!-- YAML
1903+ added: REPLACEME
1904+ -->
1905+
1906+ > Stability: 1.1 - Active Development
1907+
1908+ * ` ref` {Object | Function} The reference to the resource that is being tracked.
1909+ * ` callback` {Function} The callback function to be called when the resource
1910+ is finalized.
1911+ * ` ref` {Object | Function} The reference to the resource that is being tracked.
1912+ * ` event ` {string} The event that triggered the finalization. Defaults to 'exit'.
1913+
1914+ This function registers a callback to be called when the process emits the ` exit`
1915+ event if the ` ref` object was not garbage collected. If the object ` ref` was garbage collected
1916+ before the ` exit` event is emitted, the callback will be removed from the finalization registry,
1917+ and it will not be called on process exit.
1918+
1919+ Inside the callback you can release the resources allocated by the ` ref` object.
1920+ Be aware that all limitations applied to the ` beforeExit` event are also applied to the ` callback` function,
1921+ this means that there is a possibility that the callback will not be called under special circumstances.
1922+
1923+ The idea of this function is to help you free up resources when the starts process exiting,
1924+ but also let the object be garbage collected if it is no longer being used.
1925+
1926+ Eg: you can register an object that contains a buffer, you want to make sure that buffer is released
1927+ when the process exit, but if the object is garbage collected before the process exit, we no longer
1928+ need to release the buffer, so in this case we just remove the callback from the finalization registry.
1929+
1930+ ` ` ` cjs
1931+ const { finalization } = require (' node:process' );
1932+
1933+ // Please make sure that the function passed to finalization.register()
1934+ // does not create a closure around unnecessary objects.
1935+ function onFinalize (obj , event ) {
1936+ // You can do whatever you want with the object
1937+ obj .dispose ();
1938+ }
1939+
1940+ function setup () {
1941+ // This object can be safely garbage collected,
1942+ // and the resulting shutdown function will not be called.
1943+ // There are no leaks.
1944+ const myDisposableObject = {
1945+ dispose () {
1946+ // Free your resources synchronously
1947+ },
1948+ };
1949+
1950+ finalization .register (myDisposableObject, onFinalize);
1951+ }
1952+
1953+ setup ();
1954+ ` ` `
1955+
1956+ ` ` ` mjs
1957+ import { finalization } from ' node:process' ;
1958+
1959+ // Please make sure that the function passed to finalization.register()
1960+ // does not create a closure around unnecessary objects.
1961+ function onFinalize (obj , event ) {
1962+ // You can do whatever you want with the object
1963+ obj .dispose ();
1964+ }
1965+
1966+ function setup () {
1967+ // This object can be safely garbage collected,
1968+ // and the resulting shutdown function will not be called.
1969+ // There are no leaks.
1970+ const myDisposableObject = {
1971+ dispose () {
1972+ // Free your resources synchronously
1973+ },
1974+ };
1975+
1976+ finalization .register (myDisposableObject, onFinalize);
1977+ }
1978+
1979+ setup ();
1980+ ` ` `
1981+
1982+ The code above relies on the following assumptions:
1983+
1984+ * arrow functions are avoided
1985+ * regular functions are recommended to be within the global context (root)
1986+
1987+ Regular functions _could_ reference the context where the ` obj` lives, making the ` obj` not garbage collectible.
1988+
1989+ Arrow functions will hold the previous context. Consider, for example:
1990+
1991+ ` ` ` js
1992+ class Test {
1993+ constructor () {
1994+ finalization .register (this , (ref ) => ref .dispose ());
1995+
1996+ // even something like this is highly discouraged
1997+ // finalization.register(this, () => this.dispose());
1998+ }
1999+ dispose () {}
2000+ }
2001+ ` ` `
2002+
2003+ It is very unlikely (not impossible) that this object will be garbage collected,
2004+ but if it is not, ` dispose` will be called when ` process .exit ` is called.
2005+
2006+ Be careful and avoid relying on this feature for the disposal of critical resources,
2007+ as it is not guaranteed that the callback will be called under all circumstances.
2008+
2009+ ## ` process .finalization .registerBeforeExit (ref, callback)`
2010+
2011+ <!-- YAML
2012+ added: REPLACEME
2013+ -->
2014+
2015+ > Stability: 1.1 - Active Development
2016+
2017+ * ` ref` {Object | Function} The reference
2018+ to the resource that is being tracked.
2019+ * ` callback` {Function} The callback function to be called when the resource
2020+ is finalized.
2021+ * ` ref` {Object | Function} The reference to the resource that is being tracked.
2022+ * ` event ` {string} The event that triggered the finalization. Defaults to 'beforeExit'.
2023+
2024+ This function behaves exactly like the ` register` , except that the callback will be called
2025+ when the process emits the ` beforeExit` event if ` ref` object was not garbage collected.
2026+
2027+ Be aware that all limitations applied to the ` beforeExit` event are also applied to the ` callback` function,
2028+ this means that there is a possibility that the callback will not be called under special circumstances.
2029+
2030+ ## ` process .finalization .unregister (ref)`
2031+
2032+ <!-- YAML
2033+ added: REPLACEME
2034+ -->
2035+
2036+ > Stability: 1.1 - Active Development
2037+
2038+ * ` ref` {Object | Function} The reference
2039+ to the resource that was registered previously.
2040+
2041+ This function remove the register of the object from the finalization
2042+ registry, so the callback will not be called anymore.
2043+
2044+ ` ` ` cjs
2045+ const { finalization } = require (' node:process' );
2046+
2047+ // Please make sure that the function passed to finalization.register()
2048+ // does not create a closure around unnecessary objects.
2049+ function onFinalize (obj , event ) {
2050+ // You can do whatever you want with the object
2051+ obj .dispose ();
2052+ }
2053+
2054+ function setup () {
2055+ // This object can be safely garbage collected,
2056+ // and the resulting shutdown function will not be called.
2057+ // There are no leaks.
2058+ const myDisposableObject = {
2059+ dispose () {
2060+ // Free your resources synchronously
2061+ },
2062+ };
2063+
2064+ finalization .register (myDisposableObject, onFinalize);
2065+
2066+ // Do something
2067+
2068+ myDisposableObject .dispose ();
2069+ finalization .unregister (myDisposableObject);
2070+ }
2071+
2072+ setup ();
2073+ ` ` `
2074+
2075+ ` ` ` mjs
2076+ import { finalization } from ' node:process' ;
2077+
2078+ // Please make sure that the function passed to finalization.register()
2079+ // does not create a closure around unnecessary objects.
2080+ function onFinalize (obj , event ) {
2081+ // You can do whatever you want with the object
2082+ obj .dispose ();
2083+ }
2084+
2085+ function setup () {
2086+ // This object can be safely garbage collected,
2087+ // and the resulting shutdown function will not be called.
2088+ // There are no leaks.
2089+ const myDisposableObject = {
2090+ dispose () {
2091+ // Free your resources synchronously
2092+ },
2093+ };
2094+
2095+ // Please make sure that the function passed to finalization.register()
2096+ // does not create a closure around unnecessary objects.
2097+ function onFinalize (obj , event ) {
2098+ // You can do whatever you want with the object
2099+ obj .dispose ();
2100+ }
2101+
2102+ finalization .register (myDisposableObject, onFinalize);
2103+
2104+ // Do something
2105+
2106+ myDisposableObject .dispose ();
2107+ finalization .unregister (myDisposableObject);
2108+ }
2109+
2110+ setup ();
2111+ ` ` `
2112+
19002113## ` process .getActiveResourcesInfo ()`
19012114
19022115<!-- YAML
0 commit comments