{"id":27976996,"url":"https://github.com/lemoncode/publisher-subscriber-provider","last_synced_at":"2026-01-12T02:27:01.790Z","repository":{"id":54359058,"uuid":"340584848","full_name":"Lemoncode/publisher-subscriber-provider","owner":"Lemoncode","description":null,"archived":false,"fork":false,"pushed_at":"2021-02-23T12:18:21.000Z","size":16,"stargazers_count":3,"open_issues_count":7,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-08T01:44:04.160Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lemoncode.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-02-20T06:53:57.000Z","updated_at":"2024-02-04T04:17:26.000Z","dependencies_parsed_at":"2022-08-13T13:20:22.219Z","dependency_job_id":null,"html_url":"https://github.com/Lemoncode/publisher-subscriber-provider","commit_stats":null,"previous_names":["v-borrego/publisher-subscriber-provider"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemoncode%2Fpublisher-subscriber-provider","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemoncode%2Fpublisher-subscriber-provider/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemoncode%2Fpublisher-subscriber-provider/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemoncode%2Fpublisher-subscriber-provider/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lemoncode","download_url":"https://codeload.github.com/Lemoncode/publisher-subscriber-provider/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252983757,"owners_count":21835758,"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":[],"created_at":"2025-05-08T01:44:06.845Z","updated_at":"2026-01-12T02:27:01.784Z","avatar_url":"https://github.com/Lemoncode.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# publisher-subscriber-provider\n\nThis package is an implementation of the publisher-subscriber pattern, using react hooks and context. It provides the necessary tools to publish events and subscribe to them. Control that no re-renders are performed on components that are not affected by published events.\n\n## Features\n\n- Publish-subscriber pattern.\n- Persistence of subscriptions in context.\n- Do not re-render on components not affected by the publication of an event.\n- Ability to automatically unsubscribe via useSubscribe hook.\n- Javascript \u0026 Typescript.\n\n## Usage\n\nYou can check the examples folder.\n\n### Add the provider\n\nFirst, you need to add the Publisher Subscriber Provider by wrapping your components that need to publish / subscribe to events:\n\n```javascript\nimport React from \"react\";\nimport { PublisherSubscriberProvider } from \"publisher-subscriber-provider\";\nimport { Content } from \"./components/content.component\";\nimport { Header } from \"./components/header.component\";\nimport { Other } from \"./components/other.component\";\n\nexport const App = () =\u003e {\n  return (\n    \u003cPublisherSubscriberProvider\u003e\n      \u003cHeader /\u003e\n      \u003cContent /\u003e\n      \u003cOther /\u003e\n    \u003c/PublisherSubscriberProvider\u003e\n  );\n};\n```\n\nNow you can publish events from any child component:\n\n```javascript\nimport React from \"react\";\nimport { usePublisherSubscriber } from \"publisher-subscriber-provider\";\n\nexport const Header = () =\u003e {\n  const [value, setValue] = React.useState(\"\");\n\n  const { publish } = usePublisherSubscriber();\n\n  React.useEffect(() =\u003e {\n    publish(\"onChangeText\", value);\n  }, [value]);\n\n  return (\n    \u003cdiv\u003e\n      \u003cspan\u003eHeader\u003c/span\u003e\n      \u003cinput type=\"text\" value={value} onChange={e =\u003e setValue(e.target.value)} /\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\nAnd subscribe/unsubscribe to those events from other components:\n\n```javascript\nexport const Content = () =\u003e {\n  const [text, setText] = React.useState(\"\");\n  const { subscribe } = usePublisherSubscriber();\n\n  React.useEffect(() =\u003e {\n    const unsubscribe = subscribe(\"onChangeText\", setText);\n\n    // Return cleanup function\n    return () =\u003e unsubscribe();\n  }, []);\n\n  return (\n    \u003cdiv\u003e\n      \u003ch3\u003eContent\u003c/h3\u003e\n      \u003ch5\u003eThe value has changed: {text}\u003c/h5\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\nYou can auto-unsubscribe events using useSubscribe hook:\n\n```javascript\nexport const ContentAutoUnsubscribe = () =\u003e {\n  const [text, setText] = React.useState(\"\");\n  const { useSubscribe } = usePublisherSubscriber();\n\n  useSubscribe(\"onChangeText\", setText);\n\n  return (\n    \u003cdiv\u003e\n      \u003ch3\u003eContent\u003c/h3\u003e\n      \u003ch5\u003eThe value has changed: {text}\u003c/h5\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\n## Installing\n\nUsing npm:\n\n```bash\n$ npm install publisher-subscriber-provider\n```\n\nUsing yarn:\n\n```bash\n$ yarn install publisher-subscriber-provider\n```\n\n## Peer dependencies\n\n**publisher-subscriber-provider** needs the following peer dependencies:\n\n```\n\"react\": \"\u003e=16.8.6\",\n\"react-dom\": \"\u003e=16.8.6\"\n```\n\n## Typescript\n\n**publisher-subscriber-provider** includes TypeScript definitions.\n\n## Example\n\nYou can check the examples folder.\n\n## Credits\n\n[Link to author github](https://github.com/v-borrego)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemoncode%2Fpublisher-subscriber-provider","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemoncode%2Fpublisher-subscriber-provider","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemoncode%2Fpublisher-subscriber-provider/lists"}