{"version":3,"file":"state-adapt-ngrx.mjs","sources":["../../../../libs/ngrx/src/lib/adapt-ngrx.service.ts","../../../../libs/ngrx/src/lib/adapt-ngrx.function.ts","../../../../libs/ngrx/src/lib/watch-ngrx.function.ts","../../../../libs/ngrx/src/cheat-sheet.const.ts","../../../../libs/ngrx/src/state-adapt-ngrx.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport { Store } from '@ngrx/store';\nimport { StateAdapt } from '@state-adapt/rxjs';\n\n@Injectable({ providedIn: 'root' })\nexport class AdaptNgrx extends StateAdapt<Store> {\n  constructor(private store: Store) {\n    super(store);\n  }\n}\n","import { inject } from '@angular/core';\nimport { StateAdapt } from '@state-adapt/rxjs';\nimport { AdaptNgrx } from './adapt-ngrx.service';\nimport { watchNgrx } from './watch-ngrx.function';\nimport { Action, Adapter, getId, createAdapter } from '@state-adapt/core';\nimport { Source } from '@state-adapt/rxjs';\n\n// Differences between adapt and adaptNgrx jsdoc:\n//  - Replace beginning with the phrase \"`adaptNgrx` wraps {@link StateAdapt.adapt}, calling `inject(AdaptNgrx)` to get an instance of {@link StateAdapt} that uses NgRx for the global store.\"\n//  - Replace `adapt` with `adaptNgrx`\n//  - Replace adapt( with adaptNgrx(\n//  - Import all the {@links to other functions and services\n/**\n  ## ![StateAdapt](https://miro.medium.com/max/4800/1*qgM6mFM2Qj6woo5YxDMSrA.webp|width=14) `adaptNgrx`\n\n  > Copilot tip: Copy examples into your file or click to definition to open file with context for better Copilot suggestions.\n\n  `adaptNgrx` wraps {@link StateAdapt.adapt}, calling `inject(AdaptNgrx)` to get an instance of {@link StateAdapt} that uses NgRx for the global store.\n\n  `adaptNgrx` creates a store that will manage state while it has subscribers.\n\n  ### Example: initialState only\n  `adaptNgrx(initialState)`\n\n  The simplest way to use `adaptNgrx` is to only pass it an initial state. `adaptNgrx` returns a store object that is ready to start managing state once it has subscribers.\n  The store object comes with `set` and `reset` methods for updating state, and a `state$` observable of the store's state.\n\n  ```typescript\n  export class MyComponent {\n    name = adaptNgrx('John');\n\n    constructor() {\n      this.name.state$.subscribe(console.log); // logs 'John'\n\n      this.name.set('Johnsh'); // logs 'Johnsh'\n      this.name.reset(); // logs 'John'\n    }\n  }\n  ```\n\n  Usually you won't manually subscribe to state like this, but you can if you want the store to immediately start managing state\n  and never clean it up.\n\n  ### Example: Using an adapter\n  `adaptNgrx(initialState, adapter)`\n\n  You can also pass in a state {@link Adapter} object to customize the state change functions and selectors.\n\n  ```typescript\n  export class MyComponent {\n    name = adaptNgrx('John', {\n      concat: (state, payload: string) => state + payload,\n      selectors: {\n        length: state => state.length,\n      },\n    });\n\n    constructor() {\n      this.name.state$.subscribe(console.log); // Logs 'John'\n      this.name.length$.subscribe(console.log); // Logs 4\n\n      this.name.concat('sh'); // logs 'Johnsh' and 6\n      this.name.reset(); // logs 'John' and 4\n    }\n  }\n  ```\n\n  ### Example: Using {@link AdaptOptions}\n  `adaptNgrx(initialState, { adapter, sources, path })`\n\n  You can also define an adapter, sources, and/or a state path as part of an {@link AdaptOptions} object.\n\n  Sources allow the store to declaratively react to external events rather than being commanded\n  by imperative code in callback functions.\n\n  ```typescript\n  export class MyService {\n    tick$ = interval(1000);\n\n    clock = adaptNgrx(0, {\n      adapter: {\n        increment: state => state + 1,\n      },\n      sources: this.tick$, // or [this.tick$], or { set: this.tick$ }, or { set: [this.tick$] }\n      path: 'clock',\n    });\n\n    constructor() {\n      this.clock.state$.subscribe(console.log); // Logs 0, 1, 2, 3, etc.\n    }\n  }\n  ```\n\n  When a store is subscribed to, it passes the subscriptions up to its sources.\n  For example, if a store has an HTTP source, it will be triggered when the store\n  receives its first subscriber, and it will be canceled when the store loses its\n  last subscriber.\n\n  There are 4 possible ways sources can be defined:\n\n  1\\. A source can be a single source or [Observable](https://rxjs.dev/guide/observable)<{@link State}>. When the source emits, it triggers the store's `set` method\n  with the payload.\n\n  #### Example: Single source or observable\n\n  ```typescript\n  export class MyService {\n    nameChange$ = source<string>();\n\n    name = adaptNgrx('John', {\n      sources: this.nameChange$,\n      path: 'name',\n    });\n\n    constructor() {\n      this.name.state$.subscribe(console.log); // Logs 'John'\n\n      this.nameChange$.next('Johnsh'); // logs 'Johnsh'\n    }\n  }\n  ```\n\n  2\\. A source can be an array of sources or [Observable](https://rxjs.dev/guide/observable)<{@link State}>. When any of the sources emit, it triggers the store's `set`\n   method with the payload.\n\n  #### Example: Array of sources or observables\n\n  ```typescript\n  export class MyService {\n    nameChange$ = source<string>();\n    nameChange2$ = source<string>();\n\n    name = adaptNgrx('John', {\n      sources: [this.nameChange$, this.nameChange2$],\n      path: 'name',\n    });\n\n    constructor() {\n      this.name.state$.subscribe(console.log); // Logs 'John'\n\n      this.nameChange$.next('Johnsh'); // logs 'Johnsh'\n      this.nameChange2$.next('Johnsh2'); // logs 'Johnsh2'\n    }\n  }\n  ```\n\n  3\\. A source can be an object with keys that match the names of the {@link Adapter} state change functions, with a corresponding source or array of\n  sources that trigger the store's reaction with the payload.\n\n  #### Example: Object of sources or observables\n\n  ```typescript\n  export class MyService {\n    nameChange$ = source<string>();\n    nameReset$ = source<void>();\n\n    name = adaptNgrx('John', {\n      sources: {\n        set: this.nameChange$,\n        reset: this.nameReset$,\n      },\n      path: 'name',\n    });\n\n    constructor() {\n      this.name.state$.subscribe(console.log); // Logs 'John'\n\n      this.nameChange$.next('Johnsh'); // logs 'Johnsh'\n      this.nameReset$.next(); // logs 'John'\n    }\n  }\n  ```\n\n  4\\. A source can be a function that takes in a detached store (doesn't chain off of sources) and returns any of the above\n  types of sources or observables.\n\n  #### Example: Function that returns an observable\n\n  ```typescript\n  export class MyService {\n    name = adaptNgrx('John', {\n      sources: store => store.state$.pipe(\n        delay(1000),\n        map(name => `${name}sh`),\n      ),\n      path: 'name',\n    });\n\n    constructor() {\n      this.name.state$.subscribe(console.log);\n      // Logs 'John', then 'Johnsh' after 1 second, 'Johnshsh' after 2 seconds, etc.\n    }\n  }\n  ```\n\n  Defining a path alongside sources is recommended to enable debugging with Redux DevTools. It's easy to trace\n  singular state changes caused by user events, but it's much harder to trace state changes caused by RxJS streams.\n\n  The path string specifies the location in the global store you will find the state for the store being created\n  (while the store has subscribers). StateAdapt splits this string at periods `'.'` to create an object path within\n  the global store. Here are some example paths and the resulting global state objects:\n\n  #### Example: Paths and global state\n\n  ```typescript\n  export class MyComponent {\n    store = adaptNgrx(0, { path: 'number' });\n\n    constructor() {\n      this.store.state$.subscribe();\n      // global state: { number: 0 }\n    }\n  }\n  ```\n\n  ```typescript\n  export class MyComponent {\n    store = adaptNgrx(0, { path: 'featureA.number' });\n\n    constructor() {\n      this.store.state$.subscribe();\n      // global state: { featureA: { number: 0 } }\n    }\n  }\n  ```\n\n  ```typescript\n  export class MyComponent {\n    store = adaptNgrx(0, { path: 'featureA.featureB.number' });\n\n    constructor() {\n      this.store.state$.subscribe();\n      // global state: { featureA: { featureB: { number: 0 } } }\n    }\n  }\n  ```\n\n  Each store completely owns its own state. If more than one store tries to use the same path, StateAdapt will throw this error:\n\n  `Path '${path}' collides with '${existingPath}', which has already been initialized as a state path.`\n\n  This applies both to paths that are identical as well as paths that are substrings of each other. For example, if `'featureA'`\n  is already being used by a store and then another store tried to initialize at `'featureA.number'`, that error would be thrown.\n\n  To help avoid this error, StateAdapt provides a {@link getId} function that can be used to generate unique paths:\n\n  #### Example: getId for unique paths\n\n  ```typescript\n  import { getId } from '@state-adapt/core';\n\n  export class MyComponent {\n    store1 = adaptNgrx(0, { path: 'number' + getId() });\n    store2 = adaptNgrx(0, { path: 'number' + getId() });\n\n    constructor() {\n      this.store1.state$.subscribe();\n      this.store2.state$.subscribe();\n      // global state includes both: { number0: 0, number1: 0 }\n    }\n  }\n  ```\n\n  ### No path\n\n  If no path is provided, then the store's path defaults to the result of calling {@link getId}.\n\n  ### Remember!\n\n  The store needs to have subscribers in order to start managing state,\n  and it only subscribes to sources when it has subscribers itself.\n*/\nexport const adaptNgrx: StateAdapt['adapt'] = <T extends any[]>(...args: T) => {\n  const adaptDep = inject(AdaptNgrx); // Needs it from NgRx store\n  return (adaptDep.adapt as any)(...args);\n};\n","import { inject } from '@angular/core';\nimport { StateAdapt } from '@state-adapt/rxjs';\nimport { AdaptNgrx } from './adapt-ngrx.service';\n\n// Differences between watch and watchNgrx jsdoc:\n//  - The phrase \"`watchNgrx` wraps {@link StateAdapt.watch}, calling `inject(AdaptNgrx)` to get an instance of {@link StateAdapt}\n//   that uses NgRx for the global store.\"\n//  - Replace `watch` with `watchNgrx`\n//  - Replace watch( with watchNgrx(\n/**\n   ## ![StateAdapt](https://miro.medium.com/max/4800/1*qgM6mFM2Qj6woo5YxDMSrA.webp|width=14) `watchNgrx`\n\n   > Copilot tip: Copy examples into your file or click to definition to open file with context for better Copilot suggestions.\n\n  `watchNgrx` wraps {@link StateAdapt.watch}, calling `inject(AdaptNgrx)` to get an instance of {@link StateAdapt}\n  that uses NgRx for the global store.\n\n  `watchNgrx` returns a detached store (doesn't chain off of sources). This allows you to watch state without affecting anything.\n  It takes 2 arguments: The path of the state you are interested in, and the adapter containing the selectors you want to use.\n\n  ```tsx\n  watchNgrx(path, adapter)\n  ```\n\n  path — Object path in Redux Devtools\n\n  adapter — Object with state change functions and selectors\n\n  ### Usage\n\n  `watchNgrx` enables accessing state without subscribing to sources. For example, if your adapter manages the loading state\n  for an HTTP request and you need to know if the request is loading before the user is interested in the data,\n  `watchNgrx` can give you access to it without triggering the request.\n\n  #### Example: Accessing loading state\n\n  ```tsx\n  watchNgrx('data', httpAdapter).loading$.subscribe(console.log);\n  ```\n*/\nexport const watchNgrx: StateAdapt['watch'] = <T extends any[]>(...args: T) => {\n  const adaptDep = inject(AdaptNgrx); // Needs it from NgRx store\n  return (adaptDep.watch as any)(...args);\n};\n","/**\n  This will be replaced by the cheat sheet aggregator after the build.\n  scripts/aggregate-cheat-sheet.js\n*/\nexport const ngrxCheatSheet = {};\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;AAKM,MAAO,SAAU,SAAQ,UAAiB,CA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