1
1
import { ApplicationRef , Injectable , NgZone } from '@angular/core' ;
2
- import { Unsubscribe } from 'redux' ;
2
+ import { AnyAction , StoreEnhancer , Unsubscribe } from 'redux' ;
3
+ import { EnhancerOptions } from 'redux-devtools-extension' ;
3
4
import { NgRedux } from './ng-redux' ;
4
5
5
- declare const window : any ;
6
- const environment : any = typeof window !== 'undefined' ? window : { } ;
6
+ export interface ReduxDevTools {
7
+ ( options : EnhancerOptions ) : StoreEnhancer < any > ;
8
+ listen : (
9
+ onMessage : ( message : AnyAction ) => void ,
10
+ instanceId ?: string ,
11
+ ) => void ;
12
+ }
13
+
14
+ interface WindowWithReduxDevTools extends Window {
15
+ __REDUX_DEVTOOLS_EXTENSION__ ?: ReduxDevTools ;
16
+ devToolsExtension ?: ReduxDevTools ;
17
+ }
18
+
19
+ const environment : WindowWithReduxDevTools = ( typeof window !== 'undefined'
20
+ ? window
21
+ : { } ) as WindowWithReduxDevTools ;
7
22
8
23
/**
9
24
* An angular-2-ified version of the Redux DevTools chrome extension.
@@ -22,14 +37,14 @@ export class DevToolsExtension {
22
37
* format as described here:
23
38
* [zalmoxisus/redux-devtools-extension/blob/master/docs/API/Arguments.md]
24
39
*/
25
- enhancer = ( options ?: object ) => {
40
+ enhancer = ( options ?: EnhancerOptions ) => {
26
41
let subscription : Unsubscribe ;
27
42
if ( ! this . isEnabled ( ) ) {
28
43
return null ;
29
44
}
30
45
31
46
// Make sure changes from dev tools update angular's view.
32
- environment . devToolsExtension . listen ( ( { type } : any ) => {
47
+ this . getDevTools ( ) ! . listen ( ( { type } ) => {
33
48
if ( type === 'START' ) {
34
49
subscription = this . ngRedux . subscribe ( ( ) => {
35
50
if ( ! NgZone . isInAngularZone ( ) ) {
@@ -41,11 +56,18 @@ export class DevToolsExtension {
41
56
}
42
57
} ) ;
43
58
44
- return environment . devToolsExtension ( options ) ;
59
+ return this . getDevTools ( ) ! ( options || { } ) ;
45
60
} ;
46
61
47
62
/**
48
63
* Returns true if the extension is installed and enabled.
49
64
*/
50
- isEnabled = ( ) => environment && environment . devToolsExtension ;
65
+ isEnabled = ( ) => ! ! this . getDevTools ( ) ;
66
+
67
+ /**
68
+ * Returns the redux devtools enhancer.
69
+ */
70
+ getDevTools = ( ) =>
71
+ environment &&
72
+ ( environment . __REDUX_DEVTOOLS_EXTENSION__ || environment . devToolsExtension ) ;
51
73
}
0 commit comments