{"id":21995879,"url":"https://github.com/andria-dev/stanager","last_synced_at":"2025-03-23T04:22:44.610Z","repository":{"id":57369347,"uuid":"140658929","full_name":"andria-dev/stanager","owner":"andria-dev","description":"Stanager is a npm module that provides an immutable state manager with subscribe and unsubscribe features.","archived":false,"fork":false,"pushed_at":"2018-07-15T08:30:42.000Z","size":1129,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-28T10:46:30.582Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npm.im/stanager","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/andria-dev.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":"2018-07-12T03:59:53.000Z","updated_at":"2018-09-28T01:41:55.000Z","dependencies_parsed_at":"2022-08-26T06:00:35.346Z","dependency_job_id":null,"html_url":"https://github.com/andria-dev/stanager","commit_stats":null,"previous_names":["chrisbrownie55/stanager"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andria-dev%2Fstanager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andria-dev%2Fstanager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andria-dev%2Fstanager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andria-dev%2Fstanager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andria-dev","download_url":"https://codeload.github.com/andria-dev/stanager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245053715,"owners_count":20553381,"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":"2024-11-29T21:19:05.746Z","updated_at":"2025-03-23T04:22:44.558Z","avatar_url":"https://github.com/andria-dev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=center\u003e 👩‍💼 Stanager (stan) \u003c/h1\u003e\n\u003cp align=center\u003e\u003cem\u003eStanager is an immutable state manager with subscribe and unsubscribe features based on https://git.io/Valoo\u003c/em\u003e\u003c/p\u003e\n\n\u003cp align=center\u003e\u003cimg width=650 alt='Screenshot of stanager in action via carbon.now.sh' src='Screenshots/stanager-carbon.png' /\u003e\u003c/p\u003e\n\n## Installation\n\n```bash\nnpm i stanager\n```\n\n## Usage\n\nHotlink it from __https://unpkg.com/stanager__, or if you need to use ES6 modules try __https://unpkg.com/stanager/index.mjs__.\n\nSee [interactive Codepen demo.](https://codepen.io/chbchb55/pen/MBazqy)\n\n```js\nconst stan = require('stanager')\n// or\nimport stan from 'stanager'\n// or\nimport stan from 'https://unpkg.com/stanager'\n// or\nimport stan from 'https://unpkg.com/stanager/index.mjs'\n\n// create an observable value:\nconst shoppingCart = stan(['bread'])\n\n// assign new value:\nshoppingCart.value = shoppingCart.value.concat(['meatballs', 'flat bread'])\n\n// create listener function:\nconst changeLogger = (newCart, oldCart) =\u003e {\n  const changes = {\n    removed: oldCart.filter(item =\u003e newCart.indexOf(item) === -1),\n    added: newCart.filter(item =\u003e oldCart.indexOf(item) === -1),\n  }\n  \n  changes.removed.forEach(item =\u003e console.log(`removed: '${item}'`))\n  changes.added.forEach(item =\u003e console.log(`added: '${item}'`))\n}\n\n// subscribe to changes:\nshoppingCart.subscribe(changeLogger)\n\n// invoke listener(s):\nshoppingCart.value = shoppingCart.value.filter(item =\u003e item.indexOf('bread') === -1).concat('marinara sauce')\n\n/* output:\n\nremoved: 'bread'\nremoved: 'flat bread'\nadded: 'marinara sauce'\n\n*/\n\n// get current value:\nshoppingCart.value\n```\n### Version 1.1.0\nYou not only have _listeners_, but you also have **_modifiers_**. These are created the same way as listeners, but instead of just passing the function to `subscribe()`, you also need to pass in `true` as the second argument. `subscribe(func, true)` creates a modifier.\n\n```js\n// create an observabe value:\nconst shoppingCart = stan({\n  products: [],\n})\n\nlet discount = 1 - 0.25 // 25% off\n\n// create modifier function\nconst discountApplier = cart =\u003e ({\n  ...cart,\n  products: cart.products.map(product =\u003e ({\n    ...product,\n    discount: product.discount || product.price * discount,\n  })),\n})\n\n// subscribe to changes and modify them\nshoppingCart.subscribe(discountApplier, true)\n\nlet currentCart = shoppingCart.value\ncurrentCart.products.push({\n  name: 'blouse',\n  price: 38\n})\n\n// assign new value\nshoppingCart.value = currentCart\n\n/* output:\n\n{\n  products: [{\n    name: 'blouse',\n    price: 38,\n    discount: 28.5\n  }]\n}\n\n*/\n\n// unsubscribe modifier\nshoppingCart.unsubscribe(discountApplier)\n```\n\n## How it works:\n\nWhen you run `stan(value)` it returns an object with four methods:\n\n```js\n{\n  subscribe(...) {...},\n  unsubscribe(...) {...},\n  get value() {...},\n  set value(...) {...},\n}\n```\n\nYou can use these four methods to _listen_, and _unlisten_ to value changes, and to _set/get_ the value, as per their names. However, you __cannot__ mutate the data, hence the term immutable. Even if you pass in an array or object and you change some stuff elsewhere, it won't affect this because everything is recursively copied.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandria-dev%2Fstanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandria-dev%2Fstanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandria-dev%2Fstanager/lists"}