Skip to content

Commit

Permalink
fix: Angular adapter can now read updates in ngOnInit and selector's …
Browse files Browse the repository at this point in the history
…input.required reads
  • Loading branch information
crutchcorn committed Nov 25, 2024
1 parent 76f2352 commit 7db26c5
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/angular-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
Injector,
assertInInjectionContext,
effect,
inject,
linkedSignal,
runInInjectionContext,
signal,
effect,
DestroyRef,
} from '@angular/core'
import type { AnyUpdater, Store } from '@tanstack/store'
import type { CreateSignalOptions } from '@angular/core'
Expand Down Expand Up @@ -32,17 +33,16 @@ export function injectStore<
}

return runInInjectionContext(options.injector, () => {
const slice = signal(selector(store.state), options)
const destroyRef = inject(DestroyRef)
const slice = linkedSignal(() => selector(store.state), options)

const unsubscribe = store.subscribe(() => {
slice.set(selector(store.state))
})

effect(
(onCleanup) => {
const unsub = store.subscribe(() => {
slice.set(selector(store.state))
})
onCleanup(unsub)
},
{ allowSignalWrites: true },
)
destroyRef.onDestroy(() => {
unsubscribe()
})

return slice.asReadonly()
})
Expand Down

0 comments on commit 7db26c5

Please sign in to comment.