{"name":"memoization-registry","repository":{"type":"git","url":"git+https://github.com/fabiospampinato/memoization-registry.git"},"description":"A generalized multi-key memoization solution that does not leak memory.","version":"1.0.0","type":"module","main":"dist/index.js","exports":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"clean":"tsex clean","compile":"tsex compile","compile:watch":"tsex compile --watch","test":"node --expose-gc test/index.js","prepublishOnly":"npm run clean && npm run compile && npm run test"},"keywords":["memoization","finalization","registry","memory","leak"],"dependencies":{"mild-map":"^1.0.0"},"devDependencies":{"fava":"^0.1.2","tsex":"^2.1.0","typescript":"^4.9.5"},"bugs":{"url":"https://github.com/fabiospampinato/memoization-registry/issues"},"homepage":"https://github.com/fabiospampinato/memoization-registry#readme","_id":"memoization-registry@1.0.0","_nodeVersion":"18.12.0","_npmVersion":"8.19.2","dist":{"integrity":"sha512-RTKJ11YUNtSL2EDkAEAAykywwaH2vfhh+oFk5oNwX3IHXwVkkaSDC++SAAvq2z8MbCaVLTVvIlmV3V/AzJzBSQ==","shasum":"3ea90a176a9681a0ee66c6e4df319fc914fc0faa","tarball":"http://registry-npm.ywwl.com/memoization-registry/download/memoization-registry-1.0.0.tgz","fileCount":15,"unpackedSize":15459,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCqPgeU7+yG3YT6K2qXtp0ILlzC/Zw5BREi3kE9ywkH/QIhAMoENDWZV5wolQQLx3sTRM2yVilPsTx/FhkAOawE6Bnb"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj6vUnACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoxjA/8DimqtY6FBRh0JZs6FzKthMmDWWIV/Aunc6SlxZqmbRYSL9h/\r\nkYK1CdxVftfoKHzPq/45+bI4dUCgY0EpVBh1j59YmYpWJsKgvRnl4b6aojh5\r\n8DLvEwd59UAGL+yyy223cKKrAMkMy17CDAIQ99tcyK7TBxLUypkzdhC2v+tz\r\nNcf/fV+gNySV1Ja7yRw9R+TjKt9EIgtXw72lNBlbol9X65dHpXCq/CuX9ruV\r\nCKNBHdtBaJUukf1WPOG/P3v7/fU+AtfotcUjBzJHupJIbsIQNcj3UHt9jiK8\r\nZfBoQ+Uyw6fpX4aLZJIxGIcJ0mm6sl6Cjx5TzeSQfa1Y3ylO1Is9sRPn/Kuc\r\n9s1UNn80M3iG5xpEEjrUM4+Mzhq/8VSQeJmBk8SykPCPzRCaRe6WRPgaefk5\r\nHzTf/3W2c4x0rdSiItszE67I1zXV8kRLchYeT3olZeIOylsx/kLUh4yk//9K\r\nJdRicRI9t5x5pkHqKvhuDrszOhz6U0IGqAMRV7BAPrz4DoPcq34gCOcfKs5A\r\nYf8Jqec3rk37V5NFk/P20pNBw5elWof0yOK52elqaI8uhGpoNGOdWh3C7b3T\r\nA2D2DOmJ3EsAaE8qE3dmFs3HPwmnLTdjQQYmc5CeZPZkW0q1I7Dho2O1irmL\r\n/D7ZA6sp4RpyrYFYGtULzOXfeYOTEORe8Ek=\r\n=wAMR\r\n-----END PGP SIGNATURE-----\r\n","size":4702,"noattachment":false,"key":"/memoization-registry/-/memoization-registry-1.0.0.tgz"},"_npmUser":{"name":"fabiospampinato","email":"spampinabio@gmail.com"},"directories":{},"maintainers":[{"name":"fabiospampinato","email":"spampinabio@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/memoization-registry_1.0.0_1676342567120_0.41584167249011394"},"_hasShrinkwrap":false,"readme":"# Memoization Registry\n\nA generalized multi-key memoization solution that does not leak memory.\n\n## Features\n\n- It supports memoizing on multiple arbitrary keys.\n- It supports memoizing things other than function calls too.\n- It doesn't hold strong references to memoized values nor memoization keys, unless they are primitives.\n\n## Install\n\n```sh\nnpm install --save memoization-registry\n```\n\n## Usage\n\nMaybe the best way to explain what this allows for is with an example, how would you write a \"Magic\" class in such a way that all the following assertions succeeded?\n\n```ts\nclass Foo extends Magic {}\n\nconst foo1 = new Foo ( 'some-id' );\nconst foo2 = new Foo ( 'other-id' );\nconst foo3 = new Foo ( 'some-id' );\n\nconsole.assert ( foo1 !== foo2 );\nconsole.assert ( foo2 !== foo3 );\nconsole.assert ( foo1 === foo3 );\n\nclass Bar extends Foo {}\n\nconst bar1 = new Bar ( 'some-id' );\nconst bar2 = new Bar ( 'other-id' );\nconst bar3 = new Bar ( 'some-id' );\n\nconsole.assert ( foo1 !== bar1 );\nconsole.assert ( foo2 !== bar2 );\nconsole.assert ( foo3 !== bar3 );\n\nconsole.assert ( bar1 !== bar2 );\nconsole.assert ( bar2 !== bar3 );\nconsole.assert ( bar1 === bar3 );\n```\n\nIt may seem borderline impossible to simply make that work without any problems -- basically we want to automatically memoize some class instances, but the thing to memoize is not a regular function call, and we don't want to hold strong references to the memoized values, nor the memoization keys, as that will cause a memory leak.\n\nHere's a solution, enabled by this powerful library:\n\n```ts\nimport MemoizationRegistry from 'memoization-registry';\n\nconst registry = new MemoizationRegistry<[Function, string], Magic> ();\n\nclass Magic {\n  constructor ( id: string ) {\n    const keys = [new.target, id];\n    const factory = () => this;\n    return registry.register ( keys, factory );\n  }\n}\n\nclass Foo extends Magic {}\n\nconst foo1 = new Foo ( 'some-id' );\nconst foo2 = new Foo ( 'other-id' );\nconst foo3 = new Foo ( 'some-id' );\n\nconsole.assert ( foo1 !== foo2 );\nconsole.assert ( foo2 !== foo3 );\nconsole.assert ( foo1 === foo3 );\n\nclass Bar extends Foo {}\n\nconst bar1 = new Bar ( 'some-id' );\nconst bar2 = new Bar ( 'other-id' );\nconst bar3 = new Bar ( 'some-id' );\n\nconsole.assert ( foo1 !== bar1 );\nconsole.assert ( foo2 !== bar2 );\nconsole.assert ( foo3 !== bar3 );\n\nconsole.assert ( bar1 !== bar2 );\nconsole.assert ( bar2 !== bar3 );\nconsole.assert ( bar1 === bar3 );\n```\n\n## License\n\nMIT © Fabio Spampinato\n","publish_time":1676342567331,"_cnpm_publish_time":1676342567331,"dist-tags":{"latest":"1.0.2"}}