{"id":18372701,"url":"https://github.com/pb33f/saddlebag-js","last_synced_at":"2025-04-06T19:32:09.748Z","repository":{"id":169392434,"uuid":"645319595","full_name":"pb33f/saddlebag-js","owner":"pb33f","description":"A tiny, pure JS in-memory object store, allowing for simple state management across a large application, in any framework.","archived":false,"fork":false,"pushed_at":"2024-04-05T21:39:05.000Z","size":125,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T05:51:07.269Z","etag":null,"topics":["cache","cache-storage","in-memory","in-memory-caching","in-memory-storage","javascript","javascript-library","js","store","typescript"],"latest_commit_sha":null,"homepage":"","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/pb33f.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}},"created_at":"2023-05-25T11:49:12.000Z","updated_at":"2024-11-02T16:42:52.000Z","dependencies_parsed_at":"2024-04-05T12:46:01.543Z","dependency_job_id":null,"html_url":"https://github.com/pb33f/saddlebag-js","commit_stats":null,"previous_names":["pb33f/saddlebag-js"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pb33f%2Fsaddlebag-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pb33f%2Fsaddlebag-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pb33f%2Fsaddlebag-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pb33f%2Fsaddlebag-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pb33f","download_url":"https://codeload.github.com/pb33f/saddlebag-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247539190,"owners_count":20955266,"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":["cache","cache-storage","in-memory","in-memory-caching","in-memory-storage","javascript","javascript-library","js","store","typescript"],"created_at":"2024-11-06T00:06:50.306Z","updated_at":"2025-04-06T19:32:07.745Z","avatar_url":"https://github.com/pb33f.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# saddlebag\n\nA tiny library for creating and managing stores and\nstate in any JavaScript Application running anywhere.\n\nIt is less than 1kb when gzipped and 3kb when minified.\n\nIt's called '_saddlebag_' because every cowboy needs a reliable and simple place to store their stuff.\n\n`saddlebag` is built in TypeScript and has full type support.\n\n---\n\n`saddlebag` has two parts:\n\n- `bags`\n- `bag manager`\n\n---\n\n## Installation\n\nnpm:\n```bash\nnpm install @pb33f/saddlebag\n```\n\nyarn:\n```bash\nyarn add @pb33f/saddlebag\n```\n---\n## A quick summary of saddlebag\n\nA `bag` is a store that holds state. A `bag manager` creates `bags` and provides\naccess to them from anywhere in the application.\n\n`bag manager` is a singleton, only one manager can exist.\n\nA `bag` is just a key-value `Map`. The values can be any object or primitive.\n\nA `bag` can be subscribed to. When state changes in the `bag`, the subscribers will\nbe notified and passed the updated state.\n\nThere are three event types that can be subscribed to:\n\n- When any individual value is updated.\n- When any value is updated.\n- When the `bag` is populated with initial state.\n\nIn order to subscribe to one of these event types, a callback function\nmust be provided. The callback function will be passed the updated state\nwhen the event occurs.\n\nA `bag` can be unsubscribed from when state is no-longer needed.\n\nA `bag` can be cleared of all state and all subscribers can be notified using\nthe `reset()` method.\n\nThe `bag manager` holds `bag` instances as another 'key-value' `Map`. \n\n\u003e Yes, this is a `Map` of `Map` instances (_bags_).\n\nOnly **one** instance of a bag with a given key can exist in the `bag manager`.\n\nThe `bag manager` can clear all state from all `bags` and notify all subscribers\nusing the `resetBags()` method.\n\n---\n\n## Basic Use\n\nAll imports are exposed as named exports via `@pb33f/saddlebag`.\n\nImport the `bag manager` and create an instance of it.\n\n```typescript\nimport {Bag, BagManager, CreateBagManager} from \"@pb33f/saddlebag\";\n\nconst bagManager = CreateBagManager();\n```\n\nCreate a `bag` using the `bag manager` instance.\n\n```typescript  \nconst bag = bagManager.createBag\u003cstring\u003e('foo');\n\n// set a value for the key 'foo'\nbag.set(\"foo\", \"bar\");\n\n```\n\nSubscribe to a `bag` using the `subscribe()` method.\n\n```typescript\nconst handleUpdate = (state: string) =\u003e {\n  console.log('value changed:', state);\n}\n\nconst subcription = bag.subscribe('foo', handleUpdate);\n```\n\nUpdate the value of a key in the `bag` using the `set()` method.\n\n```typescript\nbag.set('foo', 'baz');\n```\nAnd the console should output:\n\n```\nvalue changed: baz\n```\n\nTo unsubscribe from a `bag`, use the `unsubscribe()` method\nof the `Subscription` instance returned from the `subscribe()` method.\n\n```typescript\nsubcription.unsubscribe();\n```\n\n## Listening for all state updates\n\nTo listen for all state updates in a `bag`, use the `onAllChanges()` method.\n\nThis method only takes a callback function, no key required as all keys will\ntrigger the event.\n\n```typescript\nconst subcription = bag.onAllChanges(handleUpdate);\n```\n\n## Populating a store with initial state\n\nIf you already have the data you want to store, you can populate the `bag`\nby simply passing a `Map\u003cstring, any\u003e` to the `populate()` method. \n\nThis map will contain the key-value pairs to be stored.\n\n```typescript\nconst data = new Map\u003cstring, string\u003e([[\"foo\",\"bar\"],[\"cake\",\"burger\"],[\"nugget\",\"bucket\"]]);\nbag.populate(data);\n```\n\n### Listening for populated events\n\nTo listen for the `bag` being populated with initial state, use the `onPopulated()` method\nof the `bag` instance.\n\n```typescript\nconst subscription = bag.onPopulated((initialState) =\u003e {\n            // do something...\n        });\n```\n\n## Exporting the contents of a `bag`\n\nWant to dump the data into something or somewhere else? Use the `export()` method.\n\n```typescript\nconst bagData = bag.export();\n```\n\n## Getting an existing `bag` from the `bag manager`\n\nIf you already have a `bag` and want to get it from the `bag manager`, use the `getBag()` method.\n\n```typescript\nconst bag = bagManager.getBag\u003cstring\u003e('foo');\n```\n\n---\n\n`saddlebag` A product of [pb33f](https://pb33f.io).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpb33f%2Fsaddlebag-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpb33f%2Fsaddlebag-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpb33f%2Fsaddlebag-js/lists"}