{"id":22840680,"url":"https://github.com/compulim/respondable-event","last_synced_at":"2026-01-02T04:53:42.313Z","repository":{"id":267389897,"uuid":"901096160","full_name":"compulim/respondable-event","owner":"compulim","description":"Enables event listeners to send response back to the event dispatcher.","archived":false,"fork":false,"pushed_at":"2025-12-23T09:45:45.000Z","size":253,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-24T21:56:35.437Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/respondable-event","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/compulim.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-12-10T03:17:02.000Z","updated_at":"2025-12-23T09:44:40.000Z","dependencies_parsed_at":"2025-04-14T06:31:23.560Z","dependency_job_id":"cd6c67e4-e0fa-46d8-bd0e-12c035b486ed","html_url":"https://github.com/compulim/respondable-event","commit_stats":null,"previous_names":["compulim/respondable-event"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/compulim/respondable-event","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Frespondable-event","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Frespondable-event/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Frespondable-event/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Frespondable-event/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/compulim","download_url":"https://codeload.github.com/compulim/respondable-event/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compulim%2Frespondable-event/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28168210,"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":"2026-01-02T02:00:06.235Z","response_time":54,"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-12-13T01:13:04.787Z","updated_at":"2026-01-02T04:53:42.286Z","avatar_url":"https://github.com/compulim.png","language":"TypeScript","readme":"# `respondable-event`\n\nEnables event listeners to send response back to the event dispatcher.\n\n## Background\n\nInspired by the [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent) which is available to [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) only, the `RespondableEvent` allows event listeners to send a response back to the dispatching [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget).\n\nThis is useful in scenarios where custom elements need to communicate with the host asynchronously or infrequently. For persisted messaging, use `RespondableEvent` to set up [Channel Messaging](https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API/Using_channel_messaging) instead.\n\n## How to use\n\nThe code snippet below sends an `\"authenticate\"` event to the hosting page.\n\n```ts\nconst event = new RespondableEvent(\n  'authenticate',\n  token =\u003e {\n    if (token) {\n      // Responded.\n    } else {\n      // No response.\n    }\n  },\n  { bubbles: true } // Available to the whole page.\n);\n\nelement.dispatchEvent(event);\n\n// If `respondWith()` is not called, `checkResponse()`\n// function will callback with `undefined`.\nevent.checkResponse();\n```\n\nIn the hosting page, the following code snippet responds with a token.\n\n```ts\nwindow.addEventListener('authenticate', event =\u003e {\n  if (event.target === myTrustedElement \u0026\u0026 event.request === myTrustedRequest) {\n    event.respondWith('Hello, World!');\n    event.stopPropagation();\n  }\n});\n```\n\nThe callback function passed to the constructor will be called *at most once*. Same as `FetchEvent`, the `respondWith()` function will throw if it is being called for more than once.\n\n### New `checkResponse` function\n\nTo reduce code complexity, the `checkResponse()` function guarantees the `callback` function will be called exactly once. The API design is similar to the [`HTMLFormElement.checkValidity()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/checkValidity) function:\n\n- If `respondWith()` was called, `checkResponse()` will return `true`.\n- If `respondWith()` was never called, `checkResponse()` will call the `callback` function with `undefined`, and return `false`.\n   - Subsequent calls to `checkResponse()` will return `true`.\n\nIt is recommended to put `checkResponse()` immediately after `dispatchEvent()`.\n\n## Designs\n\n### Callback function in constructor\n\nThe callback function follows the pattern found in [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver) and other observers. It is designed to limit the number of audience who can look at the response.\n\n## Behaviors\n\n### Differences between `RespondableEvent` and `FetchEvent`\n\n- `RespondableEvent` extends from [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event), where [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent) extends from [`ExtendableEvent`](https://developer.mozilla.org/en-US/docs/Web/API/ExtendableEvent)\n- `request` property is optional in `RespondableEvent` where it is required in [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent/request)\n- [`checkResponse()`](#new-checkresponse-function) function is new in `RespondableEvent`\n\n## Contributions\n\nLike us? [Star](https://github.com/compulim/respondable-event/stargazers) us.\n\nWant to make it better? [File](https://github.com/compulim/respondable-event/issues) us an issue.\n\nDon't like something you see? [Submit](https://github.com/compulim/respondable-event/pulls) a pull request.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompulim%2Frespondable-event","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcompulim%2Frespondable-event","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompulim%2Frespondable-event/lists"}