{"id":26529013,"url":"https://github.com/faustienf/subscription-stack","last_synced_at":"2025-03-21T16:20:17.922Z","repository":{"id":41506910,"uuid":"509563772","full_name":"faustienf/subscription-stack","owner":"faustienf","description":"📬 Register a subscription in LIFO stack","archived":false,"fork":false,"pushed_at":"2022-07-21T18:18:11.000Z","size":568,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-19T12:39:30.518Z","etag":null,"topics":["lifo","react","stack","subscriptions"],"latest_commit_sha":null,"homepage":"https://codesandbox.io/s/github/faustienf/subscription-stack","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/faustienf.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-07-01T19:10:13.000Z","updated_at":"2022-11-21T17:20:59.000Z","dependencies_parsed_at":"2022-08-10T02:35:13.996Z","dependency_job_id":null,"html_url":"https://github.com/faustienf/subscription-stack","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faustienf%2Fsubscription-stack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faustienf%2Fsubscription-stack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faustienf%2Fsubscription-stack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faustienf%2Fsubscription-stack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/faustienf","download_url":"https://codeload.github.com/faustienf/subscription-stack/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244825652,"owners_count":20516592,"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":["lifo","react","stack","subscriptions"],"created_at":"2025-03-21T16:20:16.802Z","updated_at":"2025-03-21T16:20:17.881Z","avatar_url":"https://github.com/faustienf.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/faustienf/subscription-stack/main/logo.png\" width=\"300\"\u003e\n\u003c/p\u003e\n\n# subscription-stack\n\n[![npm-version](https://img.shields.io/npm/v/subscription-stack.svg)](https://npmjs.org/package/subscription-stack)\n\n📬 Register a subscription in LIFO stack\n\n## Installation\n\n```bash\nnpm i subscription-stack\n```\n\n## Usage\n\n```js\n// 0. Import factory\nimport { createSubscriptionStack } from 'subscription-stack';\n// 1️. Create scoped stack\nconst stack = createSubscriptionStack();\n// 2️. Pass subscribe function\nstack(() =\u003e {\n  // 3️. Return unsubscribe function\n  return () =\u003e {};\n});\n```\n\n### Example\n\n```js\nimport { createSubscriptionStack } from 'subscription-stack';\n\nconst stack = createSubscriptionStack();\n\nstack(() =\u003e {\n  const handle = () =\u003e console.log('1️⃣');\n  window.addEventListener('click', handle);\n  return () =\u003e window.removeEventListener('click', handle);\n});\n\nstack(() =\u003e {\n  const handle = () =\u003e console.log('2️⃣');\n  window.addEventListener('click', handle);\n  return () =\u003e window.removeEventListener('click', handle);\n});\n\n// Console:\n// 2️⃣\n// 1️⃣\n```\n\n### ⚛️ React Hook\n\n```js\nimport { createSubscriptionStackHook } from 'subscription-stack/react';\n\nconst useStack = createSubscriptionStackHook();\n\nuseStack(() =\u003e {\n  const handle = () =\u003e console.log('1️⃣');\n  window.addEventListener('click', handle);\n  return () =\u003e window.removeEventListener('click', handle);\n});\n\nuseStack(() =\u003e {\n  const handle = () =\u003e console.log('2️⃣');\n  window.addEventListener('click', handle);\n  return () =\u003e window.removeEventListener('click', handle);\n});\n\n// Console:\n// 2️⃣\n// 1️⃣\n```\n\n## How does it work\n\n```js\nconst queue = new Set(); // []\n\n// Add 1 -\u003e [1]\nstack(() =\u003e {\n  queue.add(1);\n  return () =\u003e queue.delete(1);\n});\n\n// Delete 1 -\u003e []\n// Add 2 -\u003e [2]\n// Add 1 -\u003e [2, 1]\nconst unsubscribe = stack(() =\u003e {\n  queue.add(2);\n  return () =\u003e queue.delete(2);\n});\n\n// Delete 2 -\u003e [1]\n// Delete 1 -\u003e []\n// Add 3 -\u003e [3]\n// Add 2 -\u003e [3, 2]\n// Add 1 -\u003e [3, 2, 1]\nstack(() =\u003e {\n  queue.add(3);\n  return () =\u003e queue.delete(3);\n});\n\n// Delete 2 -\u003e [3, 1]\nunsubscribe();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaustienf%2Fsubscription-stack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffaustienf%2Fsubscription-stack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaustienf%2Fsubscription-stack/lists"}