{"id":20486849,"url":"https://github.com/rricard/proposal-refcollection","last_synced_at":"2026-02-05T14:04:09.528Z","repository":{"id":42901388,"uuid":"242980677","full_name":"rricard/proposal-refcollection","owner":"rricard","description":"ECMAScript proposal for the RefCollection.","archived":false,"fork":false,"pushed_at":"2022-03-26T10:41:38.000Z","size":26,"stargazers_count":3,"open_issues_count":11,"forks_count":1,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-11T10:09:03.325Z","etag":null,"topics":["javascript","references","symbols","tc39"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rricard.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-25T11:12:18.000Z","updated_at":"2023-09-08T18:03:50.000Z","dependencies_parsed_at":"2022-09-07T14:20:20.648Z","dependency_job_id":null,"html_url":"https://github.com/rricard/proposal-refcollection","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rricard%2Fproposal-refcollection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rricard%2Fproposal-refcollection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rricard%2Fproposal-refcollection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rricard%2Fproposal-refcollection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rricard","download_url":"https://codeload.github.com/rricard/proposal-refcollection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243622699,"owners_count":20321019,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","references","symbols","tc39"],"created_at":"2024-11-15T16:38:54.166Z","updated_at":"2026-02-05T14:04:04.493Z","avatar_url":"https://github.com/rricard.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RefCollection\r\n\r\nECMAScript proposal for the RefCollection.\r\n\r\n**Author:**\r\n\r\n- Robin Ricard (Bloomberg)\r\n\r\n**Champions:**\r\n\r\n- Robin Ricard (Bloomberg)\r\n- Richard Button (Bloomberg)\r\n\r\n**Advisors:**\r\n\r\n- Dan Ehrenberg (Igalia)\r\n\r\n**Stage:** 0\r\n\r\n# Overview\r\n\r\nThe RefCollection introduces a way to keep value references to objects through symbols.\r\n\r\nOne of its main goals is to be able to keep track of objects in [Records and Tuples][rt] without introducing anything mutable in them.\r\n\r\nThe RefCollection is able to automatically track symbols in such a way that when they become unreachable (and since symbols can't be forged) the referenced object can become unreachable as well.\r\n\r\n# Examples\r\n\r\n```js\r\nconst refc = new RefCollection();\r\n\r\nconst rootRef = refc.ref(document.getElementById(\"root\"));\r\nassert(refc.deref(rootRef) === document.querySelector(\"#root\"));\r\n\r\nconst otherRootRef = refc.ref(document.querySelector(\"#root\"));\r\nassert(rootRef === otherRootRef);\r\n```\r\n\r\n## _with [Records and Tuples][rt]_\r\n\r\n```js\r\nconst refc = new RefCollection();\r\n\r\nconst vdom = #{\r\n    type: \"div\",\r\n    props: #{ id: \"root\" },\r\n    children: #[\r\n        #{\r\n            type: \"button\",\r\n            props: #{\r\n                onClick: refc.ref(function () {\r\n                    alert(\"Clicked!\");\r\n                }),\r\n            },\r\n            children: #[\r\n                \"Click Me!\",\r\n            ],\r\n        },\r\n    ],\r\n};\r\n\r\nrefc.deref(vdom.children[0].props.onClick).call();\r\n// Alert: Clicked!\r\n```\r\n\r\n# API\r\n\r\n## `new RefCollection()`\r\n\r\nCreates a `RefCollection`. Does not takes any initializer arguments.\r\n\r\n## `RefCollection.prototype.ref(obj, sym = Symbol())`\r\n\r\nReturns a stable `symbol` for a corresponding object `obj`.\r\n\r\nYou can optionally give a symbol `sym` that will be used if a new symbol needs to get registered. If the `obj` is already in the RefCollection, `sym` will get discarded. If `sym` is already pointing to an object in the refCollection, expect a `TypeError`.\r\n\r\n## `RefCollection.prototype.deref(sym)`\r\n\r\nReturns the object corresponding to the symbol `sym`. If a `symbol` not returned by the same `RefCollection` gets passed here you will receive `undefined`.\r\n\r\n# Behaviors\r\n\r\n## Object-Ref stability\r\n\r\nThe collection does not let you change or remove from it, only add. So for a same object, expect the same ref symbol.\r\n\r\n## Unreachability\r\n\r\nThe `RefCollection` will mark all objects tracked as unreachable as soon as the collection becomes unreachable.\r\n\r\nIf a symbol becomes independently unreachable, since they can't be reforged, the collection will mark the corresponding object as unreachable from the collection.\r\n\r\n\u003e _Note_: This second point is impossible to polyfill.\r\n\r\n# Polyfill\r\n\r\nYou will find a [polyfill in this same repository][poly].\r\n\r\n# FAQ\r\n\r\n## Why Symbols?\r\n\r\nSymbols are a good way to represent that reference since we can't reforge them to recreate a ref from nothing (which a number or a string would have been possible to forge). \r\n\r\n[rt]: https://github.com/tc39/proposal-record-tuple\r\n[poly]: ./polyfill/refcoll.js\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frricard%2Fproposal-refcollection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frricard%2Fproposal-refcollection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frricard%2Fproposal-refcollection/lists"}