Skip to content

Commit

Permalink
feat: withDevTools disabled in prod docs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcindz88 committed Oct 17, 2024
1 parent 27d146e commit 9b2fc3a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ patchState(this.store, {loading: false});
updateState(this.store 'update loading', {loading: false});
```

`withDevtools()` is by default disabled in production mode, however if you want to tree-shake it from the application bundle you need to abstract it in your environment file.
`withDevtools()` is by default enabled in production mode, if you want to tree-shake it from the application bundle you need to abstract it in your environment file.

<details>

Expand All @@ -87,23 +87,25 @@ updateState(this.store 'update loading', {loading: false});
import { withDevtools } from '@angular-architects/ngrx-toolkit';

export const environment = {
storeDevToolsFeature: withDevtools
storeWithDevTools: withDevtools
}
```

environment.prod.ts
```typescript
import { withDevtoolsStub } from '@angular-architects/ngrx-toolkit';

export const environment = {
storeDevToolsFeature: (_: string): SignalStoreFeature => store => store
storeWithDevTools: withDevToolsStub
}
```

Then all you need to do is replace `withDevTools` everywhere in your app with `environment.storeDevToolsFeature`
Then all you need to do is replace `withDevTools` everywhere in your app with `environment.storeWithDevTools`
e.g.:
```typescript
export const SomeStore = signalStore(
withState({strings: [] as string[] }),
environment.storeDevToolsFeature('some')
environment.storeWithDevTools('featureName')
);
```

Expand Down
1 change: 1 addition & 0 deletions libs/ngrx-toolkit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {
withDevToolsStub,
withDevtools,
patchState,
updateState,
Expand Down
17 changes: 0 additions & 17 deletions libs/ngrx-toolkit/src/lib/with-devtools.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
let isDevMode = true;
jest.mock('@angular/core', () => ({ ...jest.requireActual('@angular/core'), isDevMode: () => isDevMode }))

import { signalStore } from '@ngrx/signals';
import { withEntities } from '@ngrx/signals/entities';
import { Action, withDevtools } from './with-devtools';
Expand Down Expand Up @@ -34,7 +31,6 @@ const createFlight = (flight: Partial<Flight> = {}) => ({
interface SetupOptions {
extensionsAvailable: boolean;
inSsr: boolean;
isDevMode: boolean
}

interface TestData {
Expand All @@ -52,12 +48,9 @@ function run(
const defaultOptions: SetupOptions = {
inSsr: false,
extensionsAvailable: true,
isDevMode: true,
};
const realOptions = { ...defaultOptions, ...options };

isDevMode = realOptions.isDevMode;

const sendSpy = jest.fn<void, [Action, Record<string, unknown>]>();
const connection = {
send: sendSpy,
Expand Down Expand Up @@ -123,16 +116,6 @@ describe('Devtools', () => {
)
);

it(
'should not connect if it runs in production',
run(
({ connectSpy }) => {
expect(connectSpy).toHaveBeenCalledTimes(0);
},
{ isDevMode: false }
)
);

it(
'should dispatch todo state',
run(({ sendSpy, runEffects }) => {
Expand Down
9 changes: 7 additions & 2 deletions libs/ngrx-toolkit/src/lib/with-devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SignalStoreFeature,
WritableStateSource,
} from '@ngrx/signals';
import { effect, inject, isDevMode, PLATFORM_ID, signal, Signal } from '@angular/core';
import { effect, inject, PLATFORM_ID, signal, Signal } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { Prettify } from './shared/prettify';

Expand Down Expand Up @@ -79,6 +79,11 @@ export function reset() {
storeRegistry.set({});
}

/**
* Stub for DevTools integration. Can be used to disable DevTools in production.
*/
export const withDevToolsStub: typeof withDevtools = () => store => store;

/**
* @param name store's name as it should appear in the DevTools
*/
Expand All @@ -87,7 +92,7 @@ export function withDevtools<Input extends EmptyFeatureResult>(
): SignalStoreFeature<Input, EmptyFeatureResult> {
return (store) => {
const isServer = isPlatformServer(inject(PLATFORM_ID));
if (isServer || !isDevMode()) {
if (isServer) {
return store;
}

Expand Down

0 comments on commit 9b2fc3a

Please sign in to comment.