{"id":17215440,"url":"https://github.com/pixtron/ngxstate","last_synced_at":"2025-03-25T13:21:02.672Z","repository":{"id":191514109,"uuid":"679295114","full_name":"pixtron/ngxstate","owner":"pixtron","description":"A wrapper library to make using XState state machines in Angular easier","archived":false,"fork":false,"pushed_at":"2023-09-05T09:16:43.000Z","size":353,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-02T20:48:20.190Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pixtron.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,"publiccode":null,"codemeta":null}},"created_at":"2023-08-16T14:18:43.000Z","updated_at":"2023-08-16T14:19:45.000Z","dependencies_parsed_at":"2023-08-30T08:12:14.821Z","dependency_job_id":"29ff833d-3938-4f92-b63a-b1fe3a15e001","html_url":"https://github.com/pixtron/ngxstate","commit_stats":null,"previous_names":["pixtron/ngxstate"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fngxstate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fngxstate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fngxstate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pixtron%2Fngxstate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pixtron","download_url":"https://codeload.github.com/pixtron/ngxstate/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245467610,"owners_count":20620216,"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-15T03:24:25.354Z","updated_at":"2025-03-25T13:21:02.641Z","avatar_url":"https://github.com/pixtron.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @pxtrn/ngxstate\n\nThis library contains utilities for using [XState](https://github.com/statelyai/xstate) with [Angular](https://github.com/angular/angular#readme).\n\n## Usage\n\n### Use an actor\n\n```ts\nimport { Component, Injectable } from '@angular/core';\nimport { createMachine, assign } from 'xstate';\nimport { AbstractActor, useActor } from '@pxtrn/ngxstate';\n\nconst counterMachine = createMachine({\n  context: {\n    count: 0,\n  }\n  on: {\n    INC: {\n      actions: [assign({count: (context) =\u003e context.count + 1})]\n    }\n  }\n});\n\n@Injectable({\n  providedIn: 'root',\n})\nexport class CounterActor extends AbstractActor\u003ctypeof counterMachine\u003e {\n  public readonly actor = useActor(counterMachine);\n}\n\n@Component({\n  template: `\n    \u003ch1\u003e{{ count$ | async }}\u003c/h1\u003e\n    \u003cbutton (click)=\"increment()\"\u003e+\u003c/button\u003e\n  `\n})\nexport class MyComponent {\n  private counterActor = inject(CounterActor);\n\n  public count$ = this.counterActor.select(state =\u003e state.context.count);\n\n  public increment() {\n    this.counterActor.send({type: 'INC'});\n  }\n}\n```\n\n### Provide machine implementation\n\n```ts\nimport { Injectable } from '@angular/core';\nimport { createMachine, assign } from 'xstate';\nimport { AbstractActor, AbstractImplementation, useActor } from '@pxtrn/ngxstate';\n\nconst counterMachine = createMachine({\n  context: {\n    count: 0,\n  }\n  on: {\n    INC: {\n      actions: ['increment']\n    }\n  }\n});\n\ntype CounterMachine = typeof counterMachine;\n\n@Injectable({\n  providedIn: 'root',\n})\nexport class CounterImplementation extends AbstractImplementation\u003cCounterMachine\u003e {\n  public getImplementation(): MachineImplementation\u003cCounterMachine\u003e {\n    return {\n      actions: {\n        increment: assign({ count: context =\u003e context.count + 42 }),\n      },\n    };\n  }\n}\n\n@Injectable({\n  providedIn: 'root',\n})\nexport class CounterActor extends AbstractActor\u003cCounterMachine\u003e {\n  public readonly actor = useActor(counterMachine, {\n    implementation: inject(CounterImplementation)\n  });\n}\n```\n\n### Rehydrate state\n\n```ts\n@Injectable({\n  providedIn: 'root',\n})\nexport class CounterActor extends AbstractActor\u003cCounterMachine\u003e {\n  public readonly actor = useActor(counterMachine, {\n    state: JSON.parse(localStorage.getItem('counterSnapshot')) || undefined\n  });\n\n  public persistState() {\n    const snapshot = this.actor.getSnapshot();\n    localStorage.setItem('counterSnapshot', JSON.stringify(snapshot));\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpixtron%2Fngxstate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpixtron%2Fngxstate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpixtron%2Fngxstate/lists"}