Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strong-lines-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/angular-store': patch
---

support input signals in angular store
69 changes: 14 additions & 55 deletions docs/framework/angular/reference/functions/injectStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,39 @@ title: injectStore

# Function: injectStore()

## Call Signature

```ts
function injectStore<TState, TSelected>(
store,
selector?,
options?): Signal<TSelected>;
```

Defined in: [index.ts:16](https://github.com/TanStack/store/blob/main/packages/angular-store/src/index.ts#L16)

### Type Parameters

#### TState

`TState`

#### TSelected

`TSelected` = `NoInfer`\<`TState`\>

### Parameters

#### store

`Atom`\<`TState`\>

#### selector?

(`state`) => `TSelected`

#### options?

`CreateSignalOptions`\<`TSelected`\> & `object`

### Returns

`Signal`\<`TSelected`\>

## Call Signature

```ts
function injectStore<TState, TSelected>(
store,
selector?,
options?): Signal<TSelected>;
storeOrStoreSignal,
selector,
options): Signal<TSelected>;
```

Defined in: [index.ts:21](https://github.com/TanStack/store/blob/main/packages/angular-store/src/index.ts#L21)
Defined in: [index.ts:14](https://github.com/TanStack/store/blob/main/packages/angular-store/src/index.ts#L14)

### Type Parameters
## Type Parameters

#### TState
### TState

`TState`

#### TSelected
### TSelected

`TSelected` = `NoInfer`\<`TState`\>

### Parameters
## Parameters

#### store
### storeOrStoreSignal

`Atom`\<`TState`\> | `ReadonlyAtom`\<`TState`\>
`Atom`\<`TState`\> | `ReadonlyAtom`\<`TState`\> | () => `Atom`\<`TState`\> \| `ReadonlyAtom`\<`TState`\>

#### selector?
### selector

(`state`) => `TSelected`

#### options?
### options

`CreateSignalOptions`\<`TSelected`\> & `object`
`CreateSignalOptions`\<`TSelected`\> & `object` = `...`

### Returns
## Returns

`Signal`\<`TSelected`\>
6 changes: 5 additions & 1 deletion packages/angular-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@
},
"devDependencies": {
"@analogjs/vite-plugin-angular": "^2.2.3",
"@analogjs/vitest-angular": "^2.3.1",
"@angular/common": "^21.1.2",
"@angular/compiler": "^21.1.2",
"@angular/compiler-cli": "^21.1.2",
"@angular/core": "^21.1.2",
"@angular/platform-browser": "^21.1.2",
"@angular/platform-browser-dynamic": "^21.1.2",
"@testing-library/angular": "^19.1.1",
"@testing-library/dom": "^10.4.1",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/user-event": "^14.6.1",
"ng-packagr": "^21.1.0",
"zone.js": "^0.15.1"
},
Expand Down
39 changes: 15 additions & 24 deletions packages/angular-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
import {
DestroyRef,
Injector,
assertInInjectionContext,
effect,
inject,
linkedSignal,
runInInjectionContext,
} from '@angular/core'
import type { Atom, ReadonlyAtom } from '@tanstack/store'
import type { CreateSignalOptions, Signal } from '@angular/core'

type StoreContext = Record<string, unknown>

export * from '@tanstack/store'

export function injectStore<TState, TSelected = NoInfer<TState>>(
store: Atom<TState>,
selector?: (state: NoInfer<TState>) => TSelected,
options?: CreateSignalOptions<TSelected> & { injector?: Injector },
): Signal<TSelected>
export function injectStore<TState, TSelected = NoInfer<TState>>(
store: Atom<TState> | ReadonlyAtom<TState>,
selector?: (state: NoInfer<TState>) => TSelected,
options?: CreateSignalOptions<TSelected> & { injector?: Injector },
): Signal<TSelected>
export function injectStore<
TState extends StoreContext,
TSelected = NoInfer<TState>,
>(
store: Atom<TState> | ReadonlyAtom<TState>,
storeOrStoreSignal:
| Atom<TState>
| ReadonlyAtom<TState>
| (() => Atom<TState> | ReadonlyAtom<TState>),
selector: (state: NoInfer<TState>) => TSelected = (d) =>
d as unknown as TSelected,
options: CreateSignalOptions<TSelected> & { injector?: Injector } = {
Expand All @@ -41,15 +29,18 @@ export function injectStore<
}

return runInInjectionContext(options.injector, () => {
const destroyRef = inject(DestroyRef)
const slice = linkedSignal(() => selector(store.get()), options)
const storeSignal =
typeof storeOrStoreSignal === 'function'
? storeOrStoreSignal
: () => storeOrStoreSignal

const { unsubscribe } = store.subscribe((s) => {
slice.set(selector(s))
})
const slice = linkedSignal(() => selector(storeSignal().get()), options)

destroyRef.onDestroy(() => {
unsubscribe()
effect((onCleanup) => {
const { unsubscribe } = storeSignal().subscribe((s) => {
slice.set(selector(s))
})
onCleanup(() => unsubscribe())
})

return slice.asReadonly()
Expand Down
Loading
Loading