{"id":17056320,"url":"https://github.com/biophoton/rxjs-state","last_synced_at":"2025-11-06T23:03:03.197Z","repository":{"id":38459672,"uuid":"257416848","full_name":"BioPhoton/rxjs-state","owner":"BioPhoton","description":"Vanilla TypeScript reactive state written in RxJS","archived":false,"fork":false,"pushed_at":"2023-03-07T16:28:18.000Z","size":551,"stargazers_count":14,"open_issues_count":19,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T11:51:04.553Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BioPhoton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-20T22:18:11.000Z","updated_at":"2025-02-05T02:14:05.000Z","dependencies_parsed_at":"2023-02-15T08:31:44.012Z","dependency_job_id":null,"html_url":"https://github.com/BioPhoton/rxjs-state","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BioPhoton%2Frxjs-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BioPhoton%2Frxjs-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BioPhoton%2Frxjs-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BioPhoton%2Frxjs-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BioPhoton","download_url":"https://codeload.github.com/BioPhoton/rxjs-state/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248603595,"owners_count":21131827,"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-10-14T10:24:03.679Z","updated_at":"2025-11-06T23:02:58.178Z","avatar_url":"https://github.com/BioPhoton.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rxjs-state\n![rxjs-state logo](https://raw.githubusercontent.com/BioPhoton/rxjs-state/master/projects/rxjs-state/images/rxjs-state_logo.png)\n\n#### Flexible Reactive State Written in RxJS \n\nRxJsState is a light-weight reactive state management service especially useful for component state in Angular.\nFurthermore, a global service is provided and can act as a small global state manager.\n\n## Description\n\nA flexible and lighe-weight lib to manage ephemeral state in component oriented frameworks.\n\nFind an implementation for Angular: (@rx-angular/state)[https://github.com/BioPhoton/rx-angular/tree/master/libs/state]\n\nTOC\n\n- Install\n- Setup\n- API\n\n---\n\n## Install\n\n`npm i rxjs-state -S`\n\n## Setup\n\nAs the RxJsState class is just a plain vanilla Javascript Class\n\n```typescript\nimport { RxJsState } from 'rxjs-state';\n\ninterface MyState {\n  foo: string;\n  bar: number;\n  loo: {\n    boo: string;\n    baz: number;\n  };\n}\n\nconst state = new RxJsState\u003cMyState\u003e();\nconst subscription = state.subscribe();\n```\n\n## API\n\nThe API in a nutshell\n- `$` - The complete state observable\n- `set` - Set state imperatively\n- `connect` - Connect state reactively \n- `get` - Get current state imperatively\n- `select` - Select state changes reactively\n- `hold` - maintaining the subscription of a side effect\n\nThe best practices in a nutshell\n- **Don't nest one of `set`, `connect`, `get`, `select` or `hold` into each other**\n- Use `connect` over `set` \n- In most of the cases `get` is not needed. The old state is always available.\n\n\n\n### set\n\n**Add new slices to the state by providing an object**\n\n```typescript\nconst state = new RxJsState\u003c{ foo: string; bar: number }\u003e();\nconst subscription = state.subscribe();\n\nstate.setState({ foo: 'boo' });\n// new base-state =\u003e { foo: 'boo' }\n\nstate.setState({ bar: 2 });\n// new base-state =\u003e { foo: 'boo', bar: 2 }\n```\n\n**Add new Slices to the state by providing a projection function**\n\n```typescript\nconst state = new RxJsState\u003c{ bar: number }\u003e();\nconst subscription = state.subscribe();\n\nstate.setState({ bar: 1 });\nstate.setState(currentState =\u003e ({ bar: currentState.bar + 2 }));\n// new base-state =\u003e {bar: 3}\n```\n\n### connect\n\nConnect is one of the really cool thingy of this service.\nIt helps to write the output of an `Observable` to the state and  \nhandles subscription as well as unsubscription.\n\n**Connect to a single property**\n\nTo understand that lets take a look at a normal implementation first:\n\n```typescript\nconst state = new RxJsState\u003c{ bar: number }\u003e();\nconst subscription = state.subscribe();\n\nconst newBar$ = range(1, 5);\nconst subscription = newBar$.subscribe(bar =\u003e state.setState({ bar }));\nsubscription.unsubscribe();\n```\n\nNow lets compare that example with the connect usage:\n\n```typescript\nstate.connect('bar', newBar$);\n// the property bar will get values 1, 2, 3, 4, 5\n```\n\n**Connect multiple properties**\n\n```typescript\nconst state = new RxJsState\u003c{ foo: string; bar: number }\u003e();\nconst subscription = state.subscribe();\n\nconst slice$ = of({\n  bar: 5,\n  foo: 'foo'\n});\nstate.connect(slice$);\n// new base-state =\u003e { foo: 'foo', bar: 5}\n```\n\n### select\n\nSelecting state and extend the selection behavior with RxJS operators.\nOther state management libs provide selector functions like react. The downside is they are not compossable.\n`RxJsState` provides state selection fully reactive.\n\n**State is lazy!**\n\nState is lazy! If nothing is set yet, nothing will emit.\nThis comes in especially handy for lazy view rendering!\n\n```typescript\nconst state = new RxJsState\u003c{ foo: string; bar: number }\u003e();\nconst subscription = state.subscribe();\n\nconst bar$ = state.select();\nbar$.subscribe(console.log);\n// Never emits\n```\n\n**Selecting the full state**\n\n```typescript\nconst state = new RxJsState\u003c{ foo: string; bar: number }\u003e();\nconst subscription = state.subscribe();\n\nconst bar$ = state.select();\nbar$.subscribe(console.log);\n// Does not emit\nstate.setState({ foo: 'boo' });\n// emits { foo: 'boo'} for all old ane new subscriber\n```\n\n**Access a single property**\n\n```typescript\nconst state = new RxJsState\u003c{ bar: number }\u003e();\nconst subscription = state.subscribe();\n\nstate.setState({ bar: 3 });\n\nconst bar$ = state.select('bar');\nbar$.subscribe(console.log); // 3\n```\n\n**Access a nested property**\n\n```typescript\nconst state = new RxJsState\u003c{ loo: { boo: number } }\u003e();\nconst subscription = state.subscribe();\n\nstate.setState({ loo: { boo: 42 } });\n\nconst boo$ = state.select('loo', 'boo');\nboo$.subscribe(console.log); // '42'\n```\n\n**Access by providing rxjs operators**\n\n```typescript\nconst state = new RxJsState\u003c{ loo: { bar: string } }\u003e();\nconst subscription = state.subscribe();\n\nstate.setState({ bar: 'boo' });\n\nconst customProp$ = state.select(map(state =\u003e state?.loo?.bar));\ncustomProp$.subscribe(console.log); // 'boo'\n\nconst customProp$ = state.select(map(state =\u003e ({ customProp: state.bar })));\ncustomProp$.subscribe(console.log); // { customProp: 'boo' }\n```\n\n### hold\n\nManaging side effects is core of every application.\nThe `hold` method takes care of handling them.\n\nIt helps to handles subscription as well as unsubscription od side-effects\n\n**Hold a local observable side-effect**\n\nTo understand that lets take a look at a normal implementation first:\n\n```typescript\nconst sideEffect$ = btnClick$.pipe(\n  tap(clickEvent =\u003e this.store.dispatch(loadAction()))\n);\nconst subscription = sideEffect$.subscribe();\nsubscription.unsubscribe();\n```\n\nIf you would hold to achieve the same thing it would look like this:\n\n```typescript\nconst subscription = state.subscribe();\nconst state = new RxJsState\u003c{ loo: { bar: string } }\u003e();\n\nconst sideEffect$ = btnClick$.pipe(\n  tap(clickEvent =\u003e this.store.dispatch(loadAction()))\n);\n\nstate.hold(sideEffect$);\n```\n\n**Connect an observable trigger and provide an project function that runs the side effect**\n\n```typescript\nimport { fromEvent } from 'rxjs/observable';\nstate.hold(btnClick$, clickEvent =\u003e console.log(clickEvent));\n```\n\n---\n\n**Resources**\n\nVideos:\n\n- [🎥 Tackling Component State Reactively (Live Demo at 24:47)](https://www.youtube.com/watch?v=I8uaHMs8rw0)\n\nArticles:\n\n- [💾 Research on Reactive Ephemeral State](https://dev.to/rxjs/research-on-reactive-ephemeral-state-in-component-oriented-frameworks-38lk)\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbiophoton%2Frxjs-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbiophoton%2Frxjs-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbiophoton%2Frxjs-state/lists"}