Skip to content

Commit f5edf9b

Browse files
committed
chore: add inject store support
1 parent effcaa9 commit f5edf9b

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Component } from '@angular/core'
2-
import { injectSelector } from 'angular-redux'
1+
import { Component, effect, signal } from '@angular/core'
2+
import {injectSelector, injectDispatch, injectStore} from "angular-redux";
3+
import { decrement, increment } from './store/counter-slice'
34
import { RootState } from './store'
45

56
@Component({
@@ -8,10 +9,43 @@ import { RootState } from './store'
89
imports: [],
910
template: `
1011
<div>
11-
{{count}}
12+
<div>
13+
<button
14+
aria-label="Increment value"
15+
(click)="dispatch(increment())"
16+
>
17+
Increment
18+
</button>
19+
<span>{{ count }}</span>
20+
<button
21+
aria-label="Decrement value"
22+
(click)="dispatch(decrement())"
23+
>
24+
Decrement
25+
</button>
26+
</div>
1227
</div>
1328
`
1429
})
1530
export class AppComponent {
1631
count = injectSelector((state: RootState) => state.counter.value)
32+
dispatch = injectDispatch()
33+
34+
store = injectStore()
35+
36+
val = signal(0);
37+
_test = effect(() => {
38+
if (this.val()) {
39+
console.log((this.store.getState() as any).counter.value)
40+
}
41+
})
42+
43+
increment = () => {
44+
setTimeout(() => this.val.set(this.val() + 1), 100)
45+
return increment()
46+
};
47+
decrement = () => {
48+
setTimeout(() => this.val.set(this.val() + 1), 100)
49+
return decrement()
50+
};
1751
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
export function injectDispatch() {
1+
import type { Action, Dispatch, UnknownAction } from 'redux'
2+
import { ReduxProvider } from './provider'
3+
import { inject } from '@angular/core'
24

5+
// TODO: Add `withTypes` support
6+
export function injectDispatch<AppDispatch extends Dispatch<UnknownAction> = Dispatch<UnknownAction>>(): AppDispatch {
7+
const context = inject(ReduxProvider)
8+
// TODO: assertInInjectionContext
9+
const { store } = context
10+
11+
return store.dispatch as AppDispatch
312
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { inject } from "@angular/core";
2+
import { ReduxProvider } from './provider'
3+
import type { Store, Action, UnknownAction } from 'redux'
4+
5+
export function injectStore<A extends Action<string> = UnknownAction, S = unknown>(): Store<S, A> {
6+
const context = inject(ReduxProvider)
7+
// TODO: assertInInjectionContext
8+
const { store } = context
9+
return store
10+
}

projects/angular-redux/src/lib/provide-redux.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ export interface ProviderProps<
1414
export function provideRedux<A extends Action<string> = UnknownAction, S = unknown>({
1515
store
1616
}: ProviderProps<A, S>) {
17-
return [{
17+
return {
1818
provide: ReduxProvider,
19-
useValue: new ReduxProvider(store)
20-
}]
19+
useValue: (() => {
20+
const provider = new ReduxProvider<A, S>();
21+
provider.store = store;
22+
return provider;
23+
})()
24+
}
2125
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Injectable } from '@angular/core'
22
import type { Action, Store, UnknownAction } from 'redux'
33

4-
@Injectable()
4+
@Injectable({providedIn: null})
55
export class ReduxProvider<A extends Action<string> = UnknownAction, S = unknown> {
6-
constructor(public store: Store<S, A>) {
7-
}
6+
store!: Store<S, A>;
87
}

projects/angular-redux/src/public-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
export * from './lib/inject-dispatch';
66
export * from './lib/inject-selector';
7+
export * from './lib/inject-store';
78
export * from './lib/provide-redux';
9+
export * from "./lib/provider"

0 commit comments

Comments
 (0)