{"id":23454933,"url":"https://github.com/lilbunnyrabbit/service-observer","last_synced_at":"2026-05-04T08:35:59.149Z","repository":{"id":173158679,"uuid":"650293289","full_name":"lilBunnyRabbit/service-observer","owner":"lilBunnyRabbit","description":"Convenient way to observe and react to events within a service","archived":false,"fork":false,"pushed_at":"2023-06-06T21:39:47.000Z","size":82,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-23T00:17:34.895Z","etag":null,"topics":["event-driven","function-wrapper","npm-package","observer-pattern","service-observer","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@lilbunnyrabbit/service-observer","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/lilBunnyRabbit.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-06-06T19:01:18.000Z","updated_at":"2024-09-25T14:56:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"18f0d8b5-3b46-47fa-8c34-0c92bb49a26b","html_url":"https://github.com/lilBunnyRabbit/service-observer","commit_stats":null,"previous_names":["lilbunnyrabbit/service-observer"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/lilBunnyRabbit/service-observer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilBunnyRabbit%2Fservice-observer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilBunnyRabbit%2Fservice-observer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilBunnyRabbit%2Fservice-observer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilBunnyRabbit%2Fservice-observer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lilBunnyRabbit","download_url":"https://codeload.github.com/lilBunnyRabbit/service-observer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lilBunnyRabbit%2Fservice-observer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274125625,"owners_count":25226490,"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-09-08T02:00:09.813Z","response_time":121,"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":["event-driven","function-wrapper","npm-package","observer-pattern","service-observer","typescript"],"created_at":"2024-12-24T03:17:53.051Z","updated_at":"2025-10-26T19:33:06.532Z","avatar_url":"https://github.com/lilBunnyRabbit.png","language":"TypeScript","funding_links":["https://www.buymeacoffee.com/lilBunnyRabbit"],"categories":[],"sub_categories":[],"readme":"# Service Observer\n\n[![npm version](https://img.shields.io/npm/v/@lilbunnyrabbit/service-observer.svg)](https://www.npmjs.com/package/@lilbunnyrabbit/service-observer)\n[![npm downloads](https://img.shields.io/npm/dt/@lilbunnyrabbit/service-observer.svg)](https://www.npmjs.com/package/@lilbunnyrabbit/service-observer)\n\nService Observer is an npm library that provides a convenient way to observe and react to events within a service or application. It allows you to define and listen for custom events, making it easier to manage communication and coordination between different components of your codebase.\n\n## Installation\n\n```sh\nnpm i @lilbunnyrabbit/service-observer\n```\n\n## Usage\n\n### Creating a ServiceObserver\n\nTo create a new instance of the `ServiceObserver` class, you can use the `ServiceObserver.bind` method. This method takes a callback function as its parameter, which will be invoked when the `ServiceObserverCallback` is invoked. The callback function receives an instance of the `ServiceObserver` and any additional arguments passed to it.\n\nHere's an example of creating a `ServiceObserver`:\n\n```ts\nconst serviceMethod = ServiceObserver.bind((observer: ServiceObserver\u003c{ status: string }\u003e, arg1, arg2, arg3) =\u003e {\n  // Your logic here\n});\n```\n\n### Emitting Events\n\nTo emit an event and notify the listeners, you can use the `emit` method on the `ServiceObserver` instance. The `emit` method takes two parameters: the event type and the value associated with the event.\n\nHere's an example of emitting an event:\n\n```ts\nobserver.emit(\"status\", \"success\");\n```\n\n### Observing Events\n\nTo listen for events and perform actions when they are emitted, you can use the `observe` method on the `ServiceObserverCallback` instance. The `observe` method takes an object that defines event listeners for specific event types.\n\nHere's an example of observing events:\n\n```ts\nconst listeners = {\n  status: (value) =\u003e {\n    // Handle status event\n  },\n};\n\nserviceMethod.observe(listeners)(1, 2, 3);\n```\n\n## Function Example\n\n```ts\nconst sumArray = ServiceObserver.bind((\n  observer: ServiceObserver\u003c{ progress: number }\u003e,\n  values: number[]\n) =\u003e {\n  let sum = 0;\n\n  for (let i = 0; i \u003c values.length; i++) {\n    sum += values[i];\n    observer.emit(\"progress\", (i + 1) / values.length);\n  }\n\n  return sum;\n});\n```\n\n```ts\n// Without observer\nconst sum1 = sumArray([1, 2, 3]);\n\n// With observer\nconst sum2 = sumArray.observe({\n  progress: (value) =\u003e console.log(\"Progress:\", value),\n})([4, 5, 6]);\n```\n\n## Service Class Example\n\n```ts\nclass ExampleService {\n  readonly sumArray = ServiceObserver.bind(this._sumArray.bind(this));\n  private _sumArray(\n    observer: ServiceObserver\u003c{ progress: number }\u003e,\n    values: number[],\n    initialSum = 0\n  ): number {\n    for (let i = 0; i \u003c values.length; i++) {\n      initialSum += values[i];\n      observer.emit(\"progress\", (i + 1) / values.length);\n    }\n  \n    return initialSum;\n  }\n}\n```\n\n```ts\nconst service = new ExampleService();\n\n// Without observer\nconst sum1 = service.sumArray([1, 2, 3]);\n\n// With observer\nconst sum2 = service.sumArray.observe({\n  progress: (value) =\u003e console.log(\"Progress:\", value),\n})([4, 5, 6]);\n```\n\n## Conclusion\n\nThe Service Observer library provides a flexible and straightforward way to implement event-based communication within your services or applications. By allowing you to define and observe custom events, it simplifies the coordination and interaction between different parts of your codebase.\n\n## License\n\nMIT © Andraž Mesarič-Sirec\n\n[![\"Buy Me A Coffee\"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/lilBunnyRabbit)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flilbunnyrabbit%2Fservice-observer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flilbunnyrabbit%2Fservice-observer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flilbunnyrabbit%2Fservice-observer/lists"}