{"id":21812195,"url":"https://github.com/felladrin/typed-event-dispatcher","last_synced_at":"2025-04-13T23:10:41.373Z","repository":{"id":36473724,"uuid":"226724335","full_name":"felladrin/typed-event-dispatcher","owner":"felladrin","description":"Strongly-typed events that can be publicly listened but internally-only dispatched: https://npm.im/typed-event-dispatcher","archived":false,"fork":false,"pushed_at":"2025-04-13T14:27:50.000Z","size":3454,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T23:10:06.900Z","etag":null,"topics":["npm-package"],"latest_commit_sha":null,"homepage":"https://npm.im/typed-event-dispatcher","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/felladrin.png","metadata":{"files":{"readme":"readme-too.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","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},"funding":{"ko_fi":"felladrin"}},"created_at":"2019-12-08T20:05:37.000Z","updated_at":"2025-04-13T14:27:28.000Z","dependencies_parsed_at":"2024-03-10T22:37:17.301Z","dependency_job_id":"ff582c7c-8d27-40a5-821a-26822eaddcad","html_url":"https://github.com/felladrin/typed-event-dispatcher","commit_stats":{"total_commits":426,"total_committers":8,"mean_commits":53.25,"dds":0.5164319248826291,"last_synced_commit":"572002dde6b76c27db5f202c991c8dfb0b95badf"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felladrin%2Ftyped-event-dispatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felladrin%2Ftyped-event-dispatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felladrin%2Ftyped-event-dispatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felladrin%2Ftyped-event-dispatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/felladrin","download_url":"https://codeload.github.com/felladrin/typed-event-dispatcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248794571,"owners_count":21162615,"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":["npm-package"],"created_at":"2024-11-27T14:14:46.491Z","updated_at":"2025-04-13T23:10:41.353Z","avatar_url":"https://github.com/felladrin.png","language":"TypeScript","funding_links":["https://ko-fi.com/felladrin"],"categories":[],"sub_categories":[],"readme":"# Typed Event Dispatcher\n\n## Usage Overview\n\nDefine private event dispatchers on your class, with or without data-passthroughs, like this:\n\n```ts\nclass ServerExample {\n  // Passing no data, just informing the event happened:\n  private onStartedDispatcher = new TypedEventDispatcher();\n\n  // Passing a number along with the event:\n  private onPlayersCountUpdatedDispatcher = new TypedEventDispatcher\u003cnumber\u003e();\n\n  // Passing a boolean:\n  private onDebugModeToggledDispatcher = new TypedEventDispatcher\u003cboolean\u003e();\n}\n```\n\nIf you need to pass several data with your event, define a custom data type:\n\n```ts\ntype Player = {\n  name: string;\n  level: number;\n  isAlive: boolean;\n};\n\nclass ServerExample {\n  // Passing the complete player info along with the event:\n  private onPlayerConnectedDispatcher = new TypedEventDispatcher\u003cPlayer\u003e();\n}\n```\n\nThen, on the same class, create public getters for your events,\nby returning the `getter` property from a dispatcher.  \nThe getters expose only two methods: `addListener()` and `removeListener()`.  \nAnd you don't need to declare the return type of the getters,\nas TypeScript resolves it automatically.\n\n```ts\nclass ServerExample {\n  public get onStarted() {\n    return this.onStartedDispatcher.getter;\n  }\n\n  public get onPlayersCountUpdated() {\n    return this.onPlayersCountUpdatedDispatcher.getter;\n  }\n\n  public get onDebugModeToggled() {\n    return this.onDebugModeToggledDispatcher.getter;\n  }\n\n  public get onPlayerConnected() {\n    return this.onPlayerConnectedDispatcher.getter;\n  }\n}\n```\n\nFinally, `dispatch()` the events when some action occurs!  \nUsually we do it at the end of the class methods, so other\nclasses react after those actions.\n\n```ts\nclass ServerExample {\n  private start() {\n    // (...)\n    this.onStartedDispatcher.dispatch();\n  }\n\n  private updateStats() {\n    // (...)\n    this.onPlayersCountUpdatedDispatcher.dispatch(32);\n  }\n\n  private toggleDebugMode() {\n    // (...)\n    this.onDebugModeToggledDispatcher.dispatch(true);\n  }\n\n  private registerPlayer(player: Player) {\n    // (...)\n    this.onPlayerConnectedDispatcher.dispatch(player);\n  }\n}\n```\n\nOn other classes, start listening to those events.\nThe callback parameters are also auto-resolved by TypeScript,\nbased on the type of the event. So you don't need to declare them.\n\n```ts\nclass AppExample {\n  //-------------------------------//\n  // A private variable holding an //\n  // instance of the other class.  //\n  //-------------------------------//\n  private server: ServerExample;\n\n  public registerListeners() {\n    //---------------------------------------//\n    // The event 'onStarted' passes no data, //\n    // so the listener has no arguments.     //\n    //---------------------------------------//\n    this.server.onStarted.addListener(() =\u003e {\n      console.log(\"Server started!\");\n    });\n\n    //----------------------------------------------//\n    // But 'onPlayersCountUpdated' passes a number, //\n    // so the listener has one argument to hold it. //\n    //----------------------------------------------//\n    this.server.onPlayersCountUpdated.addListener((playersCount) =\u003e {\n      spawnEnemiesBasedOnPlayersCount(playersCount);\n\n      if (playersCount \u003e playersCountRecord) {\n        registerNewPlayersCountRecord(playersCount);\n      }\n    });\n\n    //------------------------------------------------//\n    // And the listener for 'onDebugModeToggled' also //\n    // has an argument, holding the boolean passed.   //\n    //------------------------------------------------//\n    this.server.onDebugModeToggled.addListener((isDebugModeActive) =\u003e {\n      debug(`Debug Mode set to ${isDebugModeActive}.`);\n\n      if (isDebugModeActive) {\n        debug(\"Messages using debug() will now be displayed on console.\");\n      }\n    });\n\n    //-------------------------------------//\n    // Same story for 'onPlayerConnected', //\n    // which passes the player info.       //\n    //-------------------------------------//\n    this.server.onPlayerConnected.addListener((player) =\u003e {\n      addToGlobalChat(player);\n      createCustomQuests(player);\n      prepareRandomEncounters(player);\n    });\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelladrin%2Ftyped-event-dispatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffelladrin%2Ftyped-event-dispatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelladrin%2Ftyped-event-dispatcher/lists"}