File tree Expand file tree Collapse file tree 5 files changed +47
-17
lines changed
angular-redux-demo/src/app Expand file tree Collapse file tree 5 files changed +47
-17
lines changed Original file line number Diff line number Diff line change 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'
3
4
4
5
@Component ( {
5
6
selector : 'app-root' ,
6
7
standalone : true ,
7
8
imports : [ ] ,
8
9
template : `
9
10
<div>
10
- {{data.message }}
11
+ {{count }}
11
12
</div>
12
13
`
13
14
} )
14
15
export class AppComponent {
15
- data = inject ( ReduxProvider )
16
+ count = injectSelector ( ( state : RootState ) => state . counter . value )
16
17
}
Original file line number Diff line number Diff line change 1
- export function injectSelector ( ) {
1
+ import { EqualityFn } from './types'
2
+ import { inject } from '@angular/core'
3
+ import { ReduxProvider } from './provider'
2
4
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
3
30
}
Original file line number Diff line number Diff line change 1
- import { Injectable } from '@angular/core'
2
1
import type { Action , Store , UnknownAction } from 'redux'
2
+ import { ReduxProvider } from './provider'
3
3
4
4
export interface ProviderProps <
5
5
A extends Action < string > = UnknownAction ,
@@ -11,18 +11,11 @@ export interface ProviderProps<
11
11
store : Store < S , A >
12
12
}
13
13
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
-
22
14
export function provideRedux < A extends Action < string > = UnknownAction , S = unknown > ( {
23
15
store
24
16
} : ProviderProps < A , S > ) {
25
- return [
26
- new ReduxProvider < A , S > ( store )
27
- ]
17
+ return [ {
18
+ provide : ReduxProvider ,
19
+ useValue : new ReduxProvider ( store )
20
+ } ]
28
21
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ export type EqualityFn < T > = ( a : T , b : T ) => boolean
You can’t perform that action at this time.
0 commit comments