mirror of
https://github.com/arthur-pbty/portfolio2023.git
synced 2026-06-27 14:17:47 +02:00
modified
This commit is contained in:
-21
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Matteo Collina
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
# on-exit-leak-free
|
||||
|
||||
This module helps dispose of an object gracefully when the Node.js process exits.
|
||||
It executes a function with a given parameter
|
||||
on [`'exit'`](https://nodejs.org/api/process.html#event-exit) without leaking memory,
|
||||
cleaning things up appropriately if the object is garbage collected.
|
||||
|
||||
Requires `WeakRef` and `FinalizationRegistry`, i.e. use Node v14+.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm i on-exit-leak-free
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```js
|
||||
'use strict'
|
||||
|
||||
const { register, unregister } = require('on-exit-leak-free')
|
||||
const assert = require('assert')
|
||||
|
||||
function setup () {
|
||||
// This object can be safely garbage collected,
|
||||
// and the resulting shutdown function will not be called.
|
||||
// There are no leaks.
|
||||
const obj = { foo: 'bar' }
|
||||
register(obj, shutdown)
|
||||
// use registerBeforeExit(obj, shutdown) to execute the function only
|
||||
// on beforeExit
|
||||
// call unregister(obj) to remove
|
||||
}
|
||||
|
||||
let shutdownCalled = false
|
||||
|
||||
// Please make sure that the function passed to register()
|
||||
// does not create a closure around unnecessary objects.
|
||||
function shutdown (obj, eventName) {
|
||||
console.log(eventName) // beforeExit
|
||||
shutdownCalled = true
|
||||
assert.strictEqual(obj.foo, 'bar')
|
||||
}
|
||||
|
||||
setup()
|
||||
|
||||
process.on('exit', function () {
|
||||
assert.strictEqual(shutdownCalled, true)
|
||||
})
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
-108
@@ -1,108 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const refs = {
|
||||
exit: [],
|
||||
beforeExit: []
|
||||
}
|
||||
const functions = {
|
||||
exit: onExit,
|
||||
beforeExit: onBeforeExit
|
||||
}
|
||||
|
||||
let registry
|
||||
|
||||
function ensureRegistry () {
|
||||
if (registry === undefined) {
|
||||
registry = new FinalizationRegistry(clear)
|
||||
}
|
||||
}
|
||||
|
||||
function install (event) {
|
||||
if (refs[event].length > 0) {
|
||||
return
|
||||
}
|
||||
|
||||
process.on(event, functions[event])
|
||||
}
|
||||
|
||||
function uninstall (event) {
|
||||
if (refs[event].length > 0) {
|
||||
return
|
||||
}
|
||||
process.removeListener(event, functions[event])
|
||||
if (refs.exit.length === 0 && refs.beforeExit.length === 0) {
|
||||
registry = undefined
|
||||
}
|
||||
}
|
||||
|
||||
function onExit () {
|
||||
callRefs('exit')
|
||||
}
|
||||
|
||||
function onBeforeExit () {
|
||||
callRefs('beforeExit')
|
||||
}
|
||||
|
||||
function callRefs (event) {
|
||||
for (const ref of refs[event]) {
|
||||
const obj = ref.deref()
|
||||
const fn = ref.fn
|
||||
|
||||
// This should always happen, however GC is
|
||||
// undeterministic so it might not happen.
|
||||
/* istanbul ignore else */
|
||||
if (obj !== undefined) {
|
||||
fn(obj, event)
|
||||
}
|
||||
}
|
||||
refs[event] = []
|
||||
}
|
||||
|
||||
function clear (ref) {
|
||||
for (const event of ['exit', 'beforeExit']) {
|
||||
const index = refs[event].indexOf(ref)
|
||||
refs[event].splice(index, index + 1)
|
||||
uninstall(event)
|
||||
}
|
||||
}
|
||||
|
||||
function _register (event, obj, fn) {
|
||||
if (obj === undefined) {
|
||||
throw new Error('the object can\'t be undefined')
|
||||
}
|
||||
install(event)
|
||||
const ref = new WeakRef(obj)
|
||||
ref.fn = fn
|
||||
|
||||
ensureRegistry()
|
||||
registry.register(obj, ref)
|
||||
refs[event].push(ref)
|
||||
}
|
||||
|
||||
function register (obj, fn) {
|
||||
_register('exit', obj, fn)
|
||||
}
|
||||
|
||||
function registerBeforeExit (obj, fn) {
|
||||
_register('beforeExit', obj, fn)
|
||||
}
|
||||
|
||||
function unregister (obj) {
|
||||
if (registry === undefined) {
|
||||
return
|
||||
}
|
||||
registry.unregister(obj)
|
||||
for (const event of ['exit', 'beforeExit']) {
|
||||
refs[event] = refs[event].filter((ref) => {
|
||||
const _obj = ref.deref()
|
||||
return _obj && _obj !== obj
|
||||
})
|
||||
uninstall(event)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
register,
|
||||
registerBeforeExit,
|
||||
unregister
|
||||
}
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
{
|
||||
"name": "on-exit-leak-free",
|
||||
"version": "2.1.2",
|
||||
"description": "Execute a function on exit without leaking memory, allowing all objects to be garbage collected",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "standard | snazzy && tap test/*.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mcollina/on-exit-or-gc.git"
|
||||
},
|
||||
"keywords": [
|
||||
"weak",
|
||||
"reference",
|
||||
"finalization",
|
||||
"registry",
|
||||
"process",
|
||||
"exit",
|
||||
"garbage",
|
||||
"collector"
|
||||
],
|
||||
"author": "Matteo Collina <hello@matteocollina.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mcollina/on-exit-or-gc/issues"
|
||||
},
|
||||
"homepage": "https://github.com/mcollina/on-exit-or-gc#readme",
|
||||
"devDependencies": {
|
||||
"snazzy": "^9.0.0",
|
||||
"standard": "^17.0.0",
|
||||
"tap": "^16.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user