{"_id":"memoization-registry","_rev":"3184320","name":"memoization-registry","description":"A generalized multi-key memoization solution that does not leak memory.","dist-tags":{"latest":"1.0.1"},"maintainers":[{"name":"fabiospampinato","email":"spampinabio@gmail.com"}],"time":{"modified":"2023-12-06T08:50:28.000Z","created":"2023-02-14T02:42:47.331Z","1.0.1":"2023-02-14T15:51:03.013Z","1.0.0":"2023-02-14T02:42:47.331Z"},"users":{},"repository":{"type":"git","url":"git+https://github.com/fabiospampinato/memoization-registry.git"},"versions":{"1.0.1":{"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.1","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.1.0"},"devDependencies":{"fava":"^0.1.2","tsex":"^2.1.0","typescript":"^4.9.5"},"gitHead":"b3bac1437960b4511cbdbb8e24afcf61e45e13d5","bugs":{"url":"https://github.com/fabiospampinato/memoization-registry/issues"},"homepage":"https://github.com/fabiospampinato/memoization-registry#readme","_id":"memoization-registry@1.0.1","_nodeVersion":"18.12.0","_npmVersion":"8.19.2","dist":{"shasum":"e931e56c59f4e985f955d19e9314816f92af3df0","size":4848,"noattachment":false,"key":"/memoization-registry/-/memoization-registry-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoization-registry/download/memoization-registry-1.0.1.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.1_1676389862832_0.27836894271875745"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-02-14T15:51:03.013Z","publish_time":1676389863013,"_cnpm_publish_time":1676389863013},"1.0.0":{"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":{"shasum":"3ea90a176a9681a0ee66c6e4df319fc914fc0faa","size":4702,"noattachment":false,"key":"/memoization-registry/-/memoization-registry-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoization-registry/download/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,"_cnpmcore_publish_time":"2023-02-14T02:42:47.331Z","publish_time":1676342567331,"_cnpm_publish_time":1676342567331}},"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","_attachments":{},"homepage":"https://github.com/fabiospampinato/memoization-registry#readme","bugs":{"url":"https://github.com/fabiospampinato/memoization-registry/issues"}}