Skip to content

Commit 6b0db4e

Browse files
authored
feat(store-devtools): add provideStoreDevtools function (#3537)
Closes #3527
1 parent 511b7cf commit 6b0db4e

File tree

4 files changed

+91
-74
lines changed

4 files changed

+91
-74
lines changed

modules/store-devtools/spec/store.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
StoreDevtoolsModule,
1616
StoreDevtoolsOptions,
1717
} from '../';
18-
import { IS_EXTENSION_OR_MONITOR_PRESENT } from '../src/instrument';
1918
import { RECOMPUTE } from '../src/reducer';
19+
import { IS_EXTENSION_OR_MONITOR_PRESENT } from '../src/provide-store-devtools';
2020

2121
const counter = jasmine
2222
.createSpy('counter')

modules/store-devtools/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export {
88
DevToolsFeatureOptions,
99
INITIAL_OPTIONS,
1010
} from './config';
11+
export { provideStoreDevtools } from './provide-store-devtools';
Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,8 @@
1-
import { InjectionToken, ModuleWithProviders, NgModule } from '@angular/core';
2-
import { ReducerManagerDispatcher, StateObservable } from '@ngrx/store';
1+
import { ModuleWithProviders, NgModule } from '@angular/core';
32
import { Observable } from 'rxjs';
4-
5-
import {
6-
INITIAL_OPTIONS,
7-
STORE_DEVTOOLS_CONFIG,
8-
StoreDevtoolsConfig,
9-
StoreDevtoolsOptions,
10-
noMonitor,
11-
createConfig,
12-
} from './config';
3+
import { StoreDevtoolsOptions } from './config';
134
import { StoreDevtools } from './devtools';
14-
import {
15-
DevtoolsExtension,
16-
REDUX_DEVTOOLS_EXTENSION,
17-
ReduxDevtoolsExtension,
18-
} from './extension';
19-
import { DevtoolsDispatcher } from './devtools-dispatcher';
20-
21-
export const IS_EXTENSION_OR_MONITOR_PRESENT = new InjectionToken<boolean>(
22-
'@ngrx/store-devtools Is Devtools Extension or Monitor Present'
23-
);
24-
25-
export function createIsExtensionOrMonitorPresent(
26-
extension: ReduxDevtoolsExtension | null,
27-
config: StoreDevtoolsConfig
28-
) {
29-
return Boolean(extension) || config.monitor !== noMonitor;
30-
}
31-
32-
export function createReduxDevtoolsExtension() {
33-
const extensionKey = '__REDUX_DEVTOOLS_EXTENSION__';
34-
35-
if (
36-
typeof window === 'object' &&
37-
typeof (window as any)[extensionKey] !== 'undefined'
38-
) {
39-
return (window as any)[extensionKey];
40-
} else {
41-
return null;
42-
}
43-
}
5+
import { provideStoreDevtools } from './provide-store-devtools';
446

457
export function createStateObservable(
468
devtools: StoreDevtools
@@ -55,38 +17,7 @@ export class StoreDevtoolsModule {
5517
): ModuleWithProviders<StoreDevtoolsModule> {
5618
return {
5719
ngModule: StoreDevtoolsModule,
58-
providers: [
59-
DevtoolsExtension,
60-
DevtoolsDispatcher,
61-
StoreDevtools,
62-
{
63-
provide: INITIAL_OPTIONS,
64-
useValue: options,
65-
},
66-
{
67-
provide: IS_EXTENSION_OR_MONITOR_PRESENT,
68-
deps: [REDUX_DEVTOOLS_EXTENSION, STORE_DEVTOOLS_CONFIG],
69-
useFactory: createIsExtensionOrMonitorPresent,
70-
},
71-
{
72-
provide: REDUX_DEVTOOLS_EXTENSION,
73-
useFactory: createReduxDevtoolsExtension,
74-
},
75-
{
76-
provide: STORE_DEVTOOLS_CONFIG,
77-
deps: [INITIAL_OPTIONS],
78-
useFactory: createConfig,
79-
},
80-
{
81-
provide: StateObservable,
82-
deps: [StoreDevtools],
83-
useFactory: createStateObservable,
84-
},
85-
{
86-
provide: ReducerManagerDispatcher,
87-
useExisting: DevtoolsDispatcher,
88-
},
89-
],
20+
providers: [...provideStoreDevtools(options).ɵproviders],
9021
};
9122
}
9223
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { InjectionToken } from '@angular/core';
2+
import {
3+
DevtoolsExtension,
4+
REDUX_DEVTOOLS_EXTENSION,
5+
ReduxDevtoolsExtension,
6+
} from './extension';
7+
import { DevtoolsDispatcher } from './devtools-dispatcher';
8+
import {
9+
createConfig,
10+
INITIAL_OPTIONS,
11+
noMonitor,
12+
STORE_DEVTOOLS_CONFIG,
13+
StoreDevtoolsConfig,
14+
StoreDevtoolsOptions,
15+
} from './config';
16+
import {
17+
EnvironmentProviders,
18+
ReducerManagerDispatcher,
19+
StateObservable,
20+
} from '@ngrx/store';
21+
import { createStateObservable } from './instrument';
22+
import { StoreDevtools } from './devtools';
23+
24+
export const IS_EXTENSION_OR_MONITOR_PRESENT = new InjectionToken<boolean>(
25+
'@ngrx/store-devtools Is Devtools Extension or Monitor Present'
26+
);
27+
28+
export function createIsExtensionOrMonitorPresent(
29+
extension: ReduxDevtoolsExtension | null,
30+
config: StoreDevtoolsConfig
31+
) {
32+
return Boolean(extension) || config.monitor !== noMonitor;
33+
}
34+
35+
export function createReduxDevtoolsExtension() {
36+
const extensionKey = '__REDUX_DEVTOOLS_EXTENSION__';
37+
38+
if (
39+
typeof window === 'object' &&
40+
typeof (window as any)[extensionKey] !== 'undefined'
41+
) {
42+
return (window as any)[extensionKey];
43+
} else {
44+
return null;
45+
}
46+
}
47+
48+
export function provideStoreDevtools(
49+
options: StoreDevtoolsOptions = {}
50+
): EnvironmentProviders {
51+
return {
52+
ɵproviders: [
53+
DevtoolsExtension,
54+
DevtoolsDispatcher,
55+
StoreDevtools,
56+
{
57+
provide: INITIAL_OPTIONS,
58+
useValue: options,
59+
},
60+
{
61+
provide: IS_EXTENSION_OR_MONITOR_PRESENT,
62+
deps: [REDUX_DEVTOOLS_EXTENSION, STORE_DEVTOOLS_CONFIG],
63+
useFactory: createIsExtensionOrMonitorPresent,
64+
},
65+
{
66+
provide: REDUX_DEVTOOLS_EXTENSION,
67+
useFactory: createReduxDevtoolsExtension,
68+
},
69+
{
70+
provide: STORE_DEVTOOLS_CONFIG,
71+
deps: [INITIAL_OPTIONS],
72+
useFactory: createConfig,
73+
},
74+
{
75+
provide: StateObservable,
76+
deps: [StoreDevtools],
77+
useFactory: createStateObservable,
78+
},
79+
{
80+
provide: ReducerManagerDispatcher,
81+
useExisting: DevtoolsDispatcher,
82+
},
83+
],
84+
};
85+
}

0 commit comments

Comments
 (0)