forked from angular-redux/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-tools.ts
73 lines (64 loc) · 2.03 KB
/
dev-tools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { ApplicationRef, Injectable, NgZone } from '@angular/core';
import { AnyAction, StoreEnhancer, Unsubscribe } from 'redux';
import { EnhancerOptions } from 'redux-devtools-extension';
import { NgRedux } from './ng-redux';
export interface ReduxDevTools {
(options: EnhancerOptions): StoreEnhancer<any>;
listen: (
onMessage: (message: AnyAction) => void,
instanceId?: string,
) => void;
}
interface WindowWithReduxDevTools extends Window {
__REDUX_DEVTOOLS_EXTENSION__?: ReduxDevTools;
devToolsExtension?: ReduxDevTools;
}
const environment: WindowWithReduxDevTools = (typeof window !== 'undefined'
? window
: {}) as WindowWithReduxDevTools;
/**
* An angular-2-ified version of the Redux DevTools chrome extension.
*/
@Injectable()
export class DevToolsExtension {
/** @hidden */
constructor(private appRef: ApplicationRef, private ngRedux: NgRedux<any>) {}
/**
* A wrapper for the Chrome Extension Redux DevTools.
* Makes sure state changes triggered by the extension
* trigger Angular2's change detector.
*
* @argument options: dev tool options; same
* format as described here:
* [zalmoxisus/redux-devtools-extension/blob/master/docs/API/Arguments.md]
*/
enhancer = (options?: EnhancerOptions) => {
let subscription: Unsubscribe;
if (!this.isEnabled()) {
return null;
}
// Make sure changes from dev tools update angular's view.
this.getDevTools()!.listen(({ type }) => {
if (type === 'START') {
subscription = this.ngRedux.subscribe(() => {
if (!NgZone.isInAngularZone()) {
this.appRef.tick();
}
});
} else if (type === 'STOP') {
subscription();
}
});
return this.getDevTools()!(options || {});
};
/**
* Returns true if the extension is installed and enabled.
*/
isEnabled = () => !!this.getDevTools();
/**
* Returns the redux devtools enhancer.
*/
getDevTools = () =>
environment &&
(environment.__REDUX_DEVTOOLS_EXTENSION__ || environment.devToolsExtension);
}