{"id":25659492,"url":"https://github.com/apollo79/evtemitter","last_synced_at":"2025-04-19T17:08:36.587Z","repository":{"id":60198097,"uuid":"463577567","full_name":"apollo79/evtemitter","owner":"apollo79","description":"Eventemitter for deno.","archived":false,"fork":false,"pushed_at":"2024-04-24T19:01:49.000Z","size":62,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T00:13:49.990Z","etag":null,"topics":["deno","event-emitter","events-manager","eventtarget","typescript"],"latest_commit_sha":null,"homepage":"","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/apollo79.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":"2022-02-25T15:12:34.000Z","updated_at":"2024-04-24T19:01:52.000Z","dependencies_parsed_at":"2022-09-26T16:00:11.537Z","dependency_job_id":null,"html_url":"https://github.com/apollo79/evtemitter","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollo79%2Fevtemitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollo79%2Fevtemitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollo79%2Fevtemitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollo79%2Fevtemitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apollo79","download_url":"https://codeload.github.com/apollo79/evtemitter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249745977,"owners_count":21319581,"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":["deno","event-emitter","events-manager","eventtarget","typescript"],"created_at":"2025-02-24T01:17:35.665Z","updated_at":"2025-04-19T17:08:36.570Z","avatar_url":"https://github.com/apollo79.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# evtemitter\n\n[![Deno](https://github.com/apollo79/evtemitter/actions/workflows/deno.yml/badge.svg)](https://github.com/apollo79/evtemitter/actions/workflows/deno.yml)\n[![Deploy](https://github.com/apollo79/evtemitter/actions/workflows/deploy.yml/badge.svg)](https://github.com/apollo79/evtemitter/actions/workflows/deploy.yml)\\\n![Code Coverage](https://img.shields.io/static/v1?label=coverage\u0026message=82.736%\u0026color=yellowgreen)\\\n[![Total alerts](https://img.shields.io/lgtm/alerts/g/apollo79/evtemitter.svg?logo=lgtm\u0026logoWidth=18)](https://lgtm.com/projects/g/apollo79/evtemitter/alerts/)\n[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/apollo79/evtemitter.svg?logo=lgtm\u0026logoWidth=18)](https://lgtm.com/projects/g/apollo79/evtemitter/context:javascript)\n[![Deno](https://github.com/apollo79/evtemitter/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/apollo79/evtemitter/actions/workflows/codeql-analysis.yml)\n\nA typed Eventemitter for [Deno](https://deno.land) and the browser.\n\nThis EventEmitter is based on\n[CustomEvents](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent)\nfully compatible with the standard\n[EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget),\nwhich means, you can use `addEventListener`, `removeEventListener` and\n`dispatchEvent` exactly as you are used to, and allows strong typing of events\nas well as reserved events that can only be invoked from an extending class.\n\nThere are different ways to use this EventEmitter:\n\n- You can use it without typed events, like an untyped EventEmitter:\n  ```typescript\n  // JSR\n  import { EventEmitter } from \"@apollo79/evtemitter\";\n  // or alternatively https://deno.land/x/\n  // import { EventEmitter } from \"https://deno.land/x/evtemitter@2.0.0/mod.ts\";\n\n  const target = new EventEmitter();\n\n  target.on(\"foo\", (detail) =\u003e {\n    console.log(detail); // undefined\n  });\n\n  target.emit(\"foo\");\n\n  target.on(\"bar\", (detail) =\u003e {\n    console.log(detail); // hello world\n  });\n\n  target.emit(\"bar\", \"hello world\");\n  ```\n\n- You can use it as typed EventEmitter that provides strongly typed events and\n  methods with autocompletion in you code editor.\n  ```typescript\n  import { EventEmitter } from \"https://deno.land/x/evtemitter@2.0.0/mod.ts\";\n\n  type Events = {\n    foo: undefined;\n    bar: string;\n  };\n\n  const emitter = new EventEmitter\u003cEvents\u003e();\n\n  target.on(\"foo\", (detail) =\u003e {\n    console.log(\"Foo has been emitted\");\n  });\n\n  // works\n  target.emit(\"foo\");\n\n  // would throw an exception\n  // target.emit(\"foo\", \"hello world\");\n\n  target.once(\"bar\", (detail) =\u003e {\n    console.log(\"Bar has been emitted\");\n  });\n\n  // works\n  target.emit(\"bar\", \"hello world\");\n\n  // would throw an exception\n  // target.emit(\"bar\", 123);\n  ```\n\n- And you can use it with reserved events, which is for example useful if you\n  want to allow everyone to emit `message` events but in your class you want to\n  emit a `connection` event. Of course, this type of emitter is also strongly\n  typed and provides autocompletion:\n  ```typescript\n  import { EventEmitter } from \"https://deno.land/x/evtemitter@2.0.0/mod.ts\";\n\n  // Events that can be emitted via `emit`, `dispatch` and `publish` and that can be listened to\n  type UserEvents = {\n      message: string;\n  };\n\n  // Events that can only be emitted via the protected `emitReserved` method. It is also possible to listen to these events\n  type ReservedEvents = {\n      connection: { name: string };\n  };\n\n  class Implementing extends EventEmitter\u003c\n      UserEvents\n      ReservedEvents\n  \u003e {\n      constructor() {\n          super();\n\n          // your logic here\n      }\n\n      onConnect(name: string) {\n          // logic here...\n          this.emitReserved(\"connection\", { name });\n      }\n  }\n\n  const target = new Implementing();\n\n  target.addEventListener(\"connection\", (event) =\u003e {\n      const name = event.detail.name; // this is typed as a string in this example\n\n      console.log(`${name} has connected!`);\n  });\n\n  // of course, this makes no sense in reality, it's just for showing\n  target.onConnect(\"Apollo\");\n\n  target.pull(\"message\").then((message) =\u003e {\n      console.log(message);\n  });\n\n  target.emit(\"message\", \"hello world\");\n\n  // this would throw an exception\n  // target.emit(\"connection\", \"Apollo\");\n  ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo79%2Fevtemitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapollo79%2Fevtemitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo79%2Fevtemitter/lists"}