Skip to content

Commit

Permalink
perf(store): tree-shake ConfigValidator, HostEnvironment and `isA…
Browse files Browse the repository at this point in the history
…ngularInTestMode`
  • Loading branch information
arturovt committed May 18, 2021
1 parent d3bfd5e commit efa3097
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 191 deletions.
2 changes: 1 addition & 1 deletion integrations/hello-world-ng11-ivy/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"path": "./dist-integration/main.*.js",
"target": "es2015",
"maxSize": "251.86 kB",
"maxSize": "250.12 kB",
"compression": "none"
}
]
Expand Down
2 changes: 1 addition & 1 deletion integrations/hello-world-ng11-ivy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test:integration": "yarn install:ivy && yarn test:ci && yarn build:ssr:prod && yarn bundlesize && yarn e2e:ci && yarn e2e:ci:ssr",
"preinstall": "yarn --cwd ../../ cpx -v -C \"@ngxs/**/*\" integrations/hello-world-ng11-ivy/node_modules/@ngxs",
"install:ivy": "yarn --frozen-lockfile --non-interactive --no-progress",
"postinstall": "yarn ngcc --async false",
"postinstall": "ngcc",
"cy:open": "cypress open",
"cy:run": "cypress run",
"cy:run:ssr": "cypress run --config integrationFolder=cypress/integration-ssr",
Expand Down
2 changes: 1 addition & 1 deletion integrations/hello-world-ng12-ivy/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"path": "./dist-integration/main.*.js",
"target": "es2015",
"maxSize": "251.86 kB",
"maxSize": "235.72 kB",
"compression": "none"
}
]
Expand Down
4 changes: 2 additions & 2 deletions integrations/hello-world-ng12-ivy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"start-test": "start-server-and-test",
"start": "ng serve",
"build": "ng build",
"build:prod": "ng build --prod",
"build:prod": "ng build --configuration production",
"build:ssr:prod": "yarn build:prod && yarn ng run hello-world-ng12-ivy:server:production",
"test": "ng test",
"test:ci": "ng test --run-in-band --silent",
"test:integration": "yarn install:ivy && yarn test:ci && yarn build:ssr:prod && yarn bundlesize && yarn e2e:ci && yarn e2e:ci:ssr",
"preinstall": "yarn --cwd ../../ cpx -v -C \"@ngxs/**/*\" integrations/hello-world-ng12-ivy/node_modules/@ngxs",
"install:ivy": "yarn --frozen-lockfile --non-interactive --no-progress",
"postinstall": "yarn ngcc --async false",
"postinstall": "ngcc",
"cy:open": "cypress open",
"cy:run": "cypress run",
"cy:run:ssr": "cypress run --config integrationFolder=cypress/integration-ssr",
Expand Down
20 changes: 0 additions & 20 deletions packages/store/src/configs/messages.config.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
export enum VALIDATION_CODE {
INCORRECT_PRODUCTION = 'INCORRECT_PRODUCTION',
INCORRECT_DEVELOPMENT = 'INCORRECT_DEVELOPMENT'
}

// TODO: these messages might be tree-shaken away in the future.
export const CONFIG_MESSAGES = {
[VALIDATION_CODE.INCORRECT_PRODUCTION]: () =>
'Angular is running in production mode but NGXS is still running in the development mode!\n' +
'Please set developmentMode to false on the NgxsModule options when in production mode.\n' +
'NgxsModule.forRoot(states, { developmentMode: !environment.production })',
[VALIDATION_CODE.INCORRECT_DEVELOPMENT]: () =>
'RECOMMENDATION: Set developmentMode to true on the NgxsModule when Angular is running in development mode.\n' +
'NgxsModule.forRoot(states, { developmentMode: !environment.production })'
};

// The below functions are decoupled from the `CONFIG_MESSAGES` object for now, since object properties
// are not tree-shakable. That means that if the error is thrown only in development mode it still will get
// bundled into the final file. This is how Angular does error tree-shaking internally.

export function throwStateNameError(name: string): never {
throw new Error(
`${name} is not a valid state name. It needs to be a valid object property name.`
Expand Down
11 changes: 0 additions & 11 deletions packages/store/src/host-environment/host-environment.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/store/src/internal/config-validator.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/store/src/internal/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ export interface StatesAndDefaults {
states: MappedStore[];
}

export type Callback<T = any, V = any> = (...args: V[]) => T;

export interface RootStateDiff<T> {
currentAppState: T;
newAppState: T;
Expand Down
43 changes: 19 additions & 24 deletions packages/store/src/internal/state-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { InternalDispatcher } from '../internal/dispatcher';
import { StateStream } from './state-stream';
import { NgxsConfig } from '../symbols';
import { deepFreeze } from '../utils/freeze';
import { ConfigValidator } from '../internal/config-validator';

/**
* State Context factory class
Expand All @@ -16,11 +15,8 @@ export class InternalStateOperations {
constructor(
private _stateStream: StateStream,
private _dispatcher: InternalDispatcher,
private _config: NgxsConfig,
configValidator: ConfigValidator
) {
configValidator.verifyDevMode();
}
private _config: NgxsConfig
) {}

/**
* Returns the root state operators.
Expand All @@ -32,24 +28,9 @@ export class InternalStateOperations {
dispatch: (actionOrActions: any | any[]) => this._dispatcher.dispatch(actionOrActions)
};

if (this._config.developmentMode) {
return this.ensureStateAndActionsAreImmutable(rootStateOperations);
}

return rootStateOperations;
}

private ensureStateAndActionsAreImmutable(root: StateOperations<any>): StateOperations<any> {
return {
getState: () => root.getState(),
setState: value => {
const frozenValue = deepFreeze(value);
return root.setState(frozenValue);
},
dispatch: actions => {
return root.dispatch(actions);
}
};
return this._config.developmentMode
? ensureStateAndActionsAreImmutable(rootStateOperations)
: rootStateOperations;
}

setStateToTheCurrentWithNew(results: StatesAndDefaults): void {
Expand All @@ -61,3 +42,17 @@ export class InternalStateOperations {
stateOperations.setState({ ...currentState, ...results.defaults });
}
}

// We make it as a separate function and not the class method to tree-shake it in the future.
function ensureStateAndActionsAreImmutable(root: StateOperations<any>): StateOperations<any> {
return {
getState: () => root.getState(),
setState: value => {
const frozenValue = deepFreeze(value);
return root.setState(frozenValue);
},
dispatch: actions => {
return root.dispatch(actions);
}
};
}
16 changes: 0 additions & 16 deletions packages/store/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {
APP_BOOTSTRAP_LISTENER,
InjectionToken,
isDevMode,
ModuleWithProviders,
NgModule,
Provider
} from '@angular/core';
import {
INITIAL_STATE_TOKEN,
InitialState,
isAngularInTestMode,
NGXS_STATE_CONTEXT_FACTORY,
NGXS_STATE_FACTORY,
NgxsBootstrapper,
Expand All @@ -18,8 +16,6 @@ import {

import {
FEATURE_STATE_TOKEN,
NG_DEV_MODE,
NG_TEST_MODE,
NgxsConfig,
NgxsModuleOptions,
ROOT_STATE_TOKEN
Expand All @@ -39,8 +35,6 @@ import { NgxsRootModule } from './modules/ngxs-root.module';
import { NgxsFeatureModule } from './modules/ngxs-feature.module';
import { DispatchOutsideZoneNgxsExecutionStrategy } from './execution/dispatch-outside-zone-ngxs-execution-strategy';
import { InternalNgxsExecutionStrategy } from './execution/internal-ngxs-execution-strategy';
import { HostEnvironment } from './host-environment/host-environment';
import { ConfigValidator } from './internal/config-validator';
import { mergeDeep } from './utils/utils';

/**
Expand All @@ -65,8 +59,6 @@ export class NgxsModule {
Actions,
InternalActions,
NgxsBootstrapper,
ConfigValidator,
HostEnvironment,
LifecycleStateManager,
InternalDispatcher,
InternalDispatchedActionResults,
Expand Down Expand Up @@ -106,14 +98,6 @@ export class NgxsModule {
options: NgxsModuleOptions
): Provider[] {
return [
{
provide: NG_TEST_MODE,
useValue: isAngularInTestMode
},
{
provide: NG_DEV_MODE,
useValue: isDevMode
},
{
provide: NGXS_EXECUTION_STRATEGY,
useClass: options.executionStrategy || DispatchOutsideZoneNgxsExecutionStrategy
Expand Down
4 changes: 1 addition & 3 deletions packages/store/src/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import { Injectable, InjectionToken, Type } from '@angular/core';
import { Observable } from 'rxjs';

import { PlainObject, StateClass } from '@ngxs/store/internals';
import { SharedSelectorOptions, Callback } from './internal/internals';
import { SharedSelectorOptions } from './internal/internals';
import { NgxsExecutionStrategy } from './execution/symbols';
import { DispatchOutsideZoneNgxsExecutionStrategy } from './execution/dispatch-outside-zone-ngxs-execution-strategy';
import { StateToken } from './state-token/state-token';

export const ROOT_STATE_TOKEN = new InjectionToken<any>('ROOT_STATE_TOKEN');
export const FEATURE_STATE_TOKEN = new InjectionToken<any>('FEATURE_STATE_TOKEN');
export const NGXS_PLUGINS = new InjectionToken('NGXS_PLUGINS');
export const NG_TEST_MODE = new InjectionToken<Callback<boolean>>('NG_TEST_MODE');
export const NG_DEV_MODE = new InjectionToken<Callback<boolean>>('NG_DEV_MODE');

export const META_KEY = 'NGXS_META';
export const META_OPTIONS_KEY = 'NGXS_OPTIONS_META';
Expand Down
76 changes: 0 additions & 76 deletions packages/store/tests/config-validator.spec.ts

This file was deleted.

4 changes: 3 additions & 1 deletion setupJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ if (CI) {
});
}

((global as unknown) as { ngDevMode: boolean }).ngDevMode = true;
// Most of our tests are not aware of the `ngDevMode` Ivy and still rely on the old
// behavior (as it was in the View Engine).
((global as unknown) as { ngDevMode: undefined }).ngDevMode = undefined;

0 comments on commit efa3097

Please sign in to comment.