Skip to content

Commit effcaa9

Browse files
committed
feat: add non-reactive version of injectSelector
1 parent 1af10a0 commit effcaa9

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import { Component, inject } from '@angular/core'
2-
import { ReduxProvider } from 'angular-redux'
1+
import { Component } from '@angular/core'
2+
import { injectSelector } from 'angular-redux'
3+
import { RootState } from './store'
34

45
@Component({
56
selector: 'app-root',
67
standalone: true,
78
imports: [],
89
template: `
910
<div>
10-
{{data.message}}
11+
{{count}}
1112
</div>
1213
`
1314
})
1415
export class AppComponent {
15-
data = inject(ReduxProvider)
16+
count = injectSelector((state: RootState) => state.counter.value)
1617
}
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1-
export function injectSelector() {
1+
import { EqualityFn } from './types'
2+
import { inject } from '@angular/core'
3+
import { ReduxProvider } from './provider'
24

5+
export interface UseSelectorOptions<Selected = unknown> {
6+
equalityFn?: EqualityFn<Selected>
7+
}
8+
9+
const refEquality: EqualityFn<any> = (a, b) => a === b
10+
11+
// TODO: Add support for `withTypes`
12+
export function injectSelector<TState = unknown, Selected = unknown>(
13+
selector: (state: TState) => Selected,
14+
equalityFnOrOptions?: EqualityFn<Selected> | UseSelectorOptions<Selected>,
15+
): Selected {
16+
const reduxContext = inject(ReduxProvider);
17+
18+
// const { equalityFn = refEquality } =
19+
// typeof equalityFnOrOptions === 'function'
20+
// ? { equalityFn: equalityFnOrOptions }
21+
// : equalityFnOrOptions
22+
23+
const {
24+
store
25+
} = reduxContext
26+
27+
const selectedState = selector(store.getState())
28+
29+
return selectedState
330
}
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Injectable } from '@angular/core'
21
import type { Action, Store, UnknownAction } from 'redux'
2+
import { ReduxProvider } from './provider'
33

44
export interface ProviderProps<
55
A extends Action<string> = UnknownAction,
@@ -11,18 +11,11 @@ export interface ProviderProps<
1111
store: Store<S, A>
1212
}
1313

14-
15-
@Injectable()
16-
export class ReduxProvider<A extends Action<string> = UnknownAction, S = unknown> {
17-
constructor(private store: Store<S, A>) {
18-
}
19-
message = 'Hello, world'
20-
}
21-
2214
export function provideRedux<A extends Action<string> = UnknownAction, S = unknown>({
2315
store
2416
}: ProviderProps<A, S>) {
25-
return [
26-
new ReduxProvider<A, S>(store)
27-
]
17+
return [{
18+
provide: ReduxProvider,
19+
useValue: new ReduxProvider(store)
20+
}]
2821
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Injectable } from '@angular/core'
2+
import type { Action, Store, UnknownAction } from 'redux'
3+
4+
@Injectable()
5+
export class ReduxProvider<A extends Action<string> = UnknownAction, S = unknown> {
6+
constructor(public store: Store<S, A>) {
7+
}
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type EqualityFn<T> = (a: T, b: T) => boolean

0 commit comments

Comments
 (0)