{"id":16641830,"url":"https://github.com/samthor/disposable-element","last_synced_at":"2025-08-12T13:06:17.924Z","repository":{"id":57145725,"uuid":"388381565","full_name":"samthor/disposable-element","owner":"samthor","description":null,"archived":false,"fork":false,"pushed_at":"2021-07-28T04:07:23.000Z","size":40,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-09T04:41:47.599Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samthor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-22T08:14:13.000Z","updated_at":"2021-07-28T04:07:25.000Z","dependencies_parsed_at":"2022-09-05T08:51:16.848Z","dependency_job_id":null,"html_url":"https://github.com/samthor/disposable-element","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/samthor/disposable-element","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fdisposable-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fdisposable-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fdisposable-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fdisposable-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samthor","download_url":"https://codeload.github.com/samthor/disposable-element/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fdisposable-element/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269693593,"owners_count":24460248,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-10-12T07:48:01.756Z","updated_at":"2025-08-12T13:06:17.892Z","avatar_url":"https://github.com/samthor.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"If you happen to be here, [see the post](https://whistlr.info/2021/disposable-web-components/).\nThis is a hopefully interesting idea, but not fully polished yet.\n\n# Usage\n\nInstall via NPM, if you dare.\nESM only for now.\nThis has lit 2.0.0+ as a peer dependency.\n\n```js\nimport { DisposableInner, disposableElement } from 'disposable-element';\n\nclass DemoInner extends DisposableInner {\n  static properties = {\n    something: { type: String },\n  };\n\n  /**\n   * @param {(arg: () =\u003e void) =\u003e void} cleanup\n   */\n  constructor(cleanup) {\n    super();\n    const unsub = subscribeToSomething(this.something, this.#update);\n    cleanup(unsub);\n  }\n\n  /**\n   * @param {lit.PropertyValues} properties\n   */\n  shouldDispose(properties) {\n    return properties.has('something');\n  }\n\n  /**\n   * @param {any} snapshot\n   */\n  #update = (snapshot) =\u003e {\n    // do something with database result\n    this._data = snapshot;\n  };\n\n  render() {\n    return html`\u003c!-- render something --\u003e`;\n  }\n}\n\n/**\n * @typedef {InstanceType\u003ctypeof DemoElement\u003e}\n */\nexport const DemoElement = disposableElement(DemoInner, { something: '' });\ncustomElements.define('demo-something', DemoElement);\n```\n\nThe awkward `@typedef` above is because classes in TS are both _values_ and _types_ (see [Twitter](https://twitter.com/nhardy96/status/1419596619519979528)), and the helper method `disposableElement` only returns the _value_.\n\nIt's possible to create the `DemoElement` by directly subclassing, too:\n\n```js\nexport class DemoElement extends DisposableElement {\n  constructor() {\n    super(DemoInner, { chat: '' });\n  }\n}\n```\n\n\u0026hellip;the idea is that you shouldn't really be interacting with the implementation `DemoElement`, though.\nThe second arg, in both cases, are the default values for your `properties` (and it's also used to type the class).\n\n# TODOs\n\n* Render inside `DisposableInner` will bind its event listeners to the outer element:\n\n```js\nclass MyDisposable extends DisposableInner {\n\n  render() {\n    return html`\u003cbutton @click=${this.click}\u003e\u003c/button\u003e`;\n  }\n\n  click() {\n    // \"this\" will be incorrect, and point to outer element\n  }\n\n}\n```\n\n* State isn't cleared between inner instances - should it be? State could represent database result, maybe extend `properties` to have an extra flag...\n\n* No TS version\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamthor%2Fdisposable-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamthor%2Fdisposable-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamthor%2Fdisposable-element/lists"}