refactor(core): make lockRunMode a noop and deprecate it.

BREAKING CHANGE:
- `lockRunMode` is deprecated and no more needed.

Closes #9878
This commit is contained in:
Tobias Bosch
2016-07-07 14:42:46 -07:00
parent 1426f680f5
commit 98d49d4ce3
6 changed files with 29 additions and 43 deletions

View File

@ -12,7 +12,3 @@ require('reflect-metadata');
require('zone.js/dist/zone-node.js'); require('zone.js/dist/zone-node.js');
require('zone.js/dist/long-stack-trace-zone.js'); require('zone.js/dist/long-stack-trace-zone.js');
import {lockRunMode} from '@angular/core';
// Need to lock the mode explicitely as this test is not using Angular's testing framework.
lockRunMode();

View File

@ -11,7 +11,7 @@
* Intended to be used in a build step. * Intended to be used in a build step.
*/ */
import * as compiler from '@angular/compiler'; import * as compiler from '@angular/compiler';
import {AppModuleMetadata, ComponentMetadata, ViewEncapsulation, lockRunMode} from '@angular/core'; import {AppModuleMetadata, ComponentMetadata, ViewEncapsulation} from '@angular/core';
import {AngularCompilerOptions} from '@angular/tsc-wrapped'; import {AngularCompilerOptions} from '@angular/tsc-wrapped';
import * as path from 'path'; import * as path from 'path';
import * as ts from 'typescript'; import * as ts from 'typescript';
@ -36,9 +36,7 @@ export class CodeGenerator {
private options: AngularCompilerOptions, private program: ts.Program, private options: AngularCompilerOptions, private program: ts.Program,
public host: ts.CompilerHost, private staticReflector: StaticReflector, public host: ts.CompilerHost, private staticReflector: StaticReflector,
private resolver: CompileMetadataResolver, private compiler: compiler.OfflineCompiler, private resolver: CompileMetadataResolver, private compiler: compiler.OfflineCompiler,
private reflectorHost: ReflectorHost) { private reflectorHost: ReflectorHost) {}
lockRunMode();
}
private readFileMetadata(absSourcePath: string): FileMetadata { private readFileMetadata(absSourcePath: string): FileMetadata {
const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath); const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath);

View File

@ -19,7 +19,7 @@ import * as ts from 'typescript';
import * as tsc from '@angular/tsc-wrapped'; import * as tsc from '@angular/tsc-wrapped';
import * as path from 'path'; import * as path from 'path';
import * as compiler from '@angular/compiler'; import * as compiler from '@angular/compiler';
import {ViewEncapsulation, lockRunMode} from '@angular/core'; import {ViewEncapsulation} from '@angular/core';
import {StaticReflector} from './static_reflector'; import {StaticReflector} from './static_reflector';
import {CompileMetadataResolver, HtmlParser, DirectiveNormalizer, Lexer, Parser, DomElementSchemaRegistry, TypeScriptEmitter, MessageExtractor, removeDuplicates, ExtractionResult, Message, ParseError, serializeXmb,} from './compiler_private'; import {CompileMetadataResolver, HtmlParser, DirectiveNormalizer, Lexer, Parser, DomElementSchemaRegistry, TypeScriptEmitter, MessageExtractor, removeDuplicates, ExtractionResult, Message, ParseError, serializeXmb,} from './compiler_private';
@ -41,9 +41,7 @@ class Extractor {
private _options: tsc.AngularCompilerOptions, private _program: ts.Program, private _options: tsc.AngularCompilerOptions, private _program: ts.Program,
public host: ts.CompilerHost, private staticReflector: StaticReflector, public host: ts.CompilerHost, private staticReflector: StaticReflector,
private _resolver: CompileMetadataResolver, private _normalizer: DirectiveNormalizer, private _resolver: CompileMetadataResolver, private _normalizer: DirectiveNormalizer,
private _reflectorHost: ReflectorHost, private _extractor: MessageExtractor) { private _reflectorHost: ReflectorHost, private _extractor: MessageExtractor) {}
lockRunMode();
}
private _extractCmpMessages(components: compiler.CompileDirectiveMetadata[]): ExtractionResult { private _extractCmpMessages(components: compiler.CompileDirectiveMetadata[]): ExtractionResult {
if (!components || !components.length) { if (!components || !components.length) {

View File

@ -64,30 +64,28 @@ export function enableProdMode(): void {
_devMode = false; _devMode = false;
} }
/**
* Returns whether Angular is in development mode.
* This can only be read after `lockRunMode` has been called.
*
* By default, this is true, unless a user calls `enableProdMode`.
*
* @experimental APIs related to application bootstrap are currently under review.
*/
export function isDevMode(): boolean {
if (!_runModeLocked) {
throw new BaseException(`Dev mode can't be read before bootstrap!`);
}
return _devMode;
}
/** /**
* Locks the run mode of Angular. After this has been called, * Locks the run mode of Angular. After this has been called,
* it can't be changed any more. I.e. `isDevMode()` will always * it can't be changed any more. I.e. `isDevMode()` will always
* return the same value. * return the same value.
* *
* @experimental APIs related to application bootstrap are currently under review. * @deprecated This is a noop now. {@link isDevMode} automatically locks the run mode on first call.
*/ */
export function lockRunMode(): void { export function lockRunMode(): void {
console.warn('lockRunMode() is deprecated and not needed any more.');
}
/**
* Returns whether Angular is in development mode. After called once,
* the value is locked and won't change any more.
*
* By default, this is true, unless a user calls `enableProdMode` before calling this.
*
* @experimental APIs related to application bootstrap are currently under review.
*/
export function isDevMode(): boolean {
_runModeLocked = true; _runModeLocked = true;
return _devMode;
} }
/** /**
@ -104,7 +102,6 @@ export function createPlatform(injector: Injector): PlatformRef {
throw new BaseException( throw new BaseException(
'There can be only one platform. Destroy the previous one to create a new one.'); 'There can be only one platform. Destroy the previous one to create a new one.');
} }
lockRunMode();
_inPlatformCreate = true; _inPlatformCreate = true;
try { try {
_platform = injector.get(PlatformRef); _platform = injector.get(PlatformRef);

View File

@ -8,7 +8,7 @@
import {COMMON_DIRECTIVES, COMMON_PIPES} from '@angular/common'; import {COMMON_DIRECTIVES, COMMON_PIPES} from '@angular/common';
import {COMPILER_PROVIDERS, CompilerConfig, XHR} from '@angular/compiler'; import {COMPILER_PROVIDERS, CompilerConfig, XHR} from '@angular/compiler';
import {AppModule, AppModuleRef, ApplicationRef, Compiler, ComponentRef, ComponentResolver, ExceptionHandler, PLATFORM_DIRECTIVES, PLATFORM_PIPES, ReflectiveInjector, Type, coreLoadAndBootstrap, isDevMode, lockRunMode} from '@angular/core'; import {AppModule, AppModuleRef, ApplicationRef, Compiler, ComponentRef, ComponentResolver, ExceptionHandler, PLATFORM_DIRECTIVES, PLATFORM_PIPES, ReflectiveInjector, Type, coreLoadAndBootstrap, isDevMode} from '@angular/core';
import {BROWSER_APP_PROVIDERS, BrowserModule, WORKER_APP_APPLICATION_PROVIDERS, WORKER_SCRIPT, WORKER_UI_APPLICATION_PROVIDERS, bootstrapModuleFactory, browserPlatform, workerAppPlatform, workerUiPlatform} from '@angular/platform-browser'; import {BROWSER_APP_PROVIDERS, BrowserModule, WORKER_APP_APPLICATION_PROVIDERS, WORKER_SCRIPT, WORKER_UI_APPLICATION_PROVIDERS, bootstrapModuleFactory, browserPlatform, workerAppPlatform, workerUiPlatform} from '@angular/platform-browser';
import {Console, ReflectionCapabilities, reflector} from './core_private'; import {Console, ReflectionCapabilities, reflector} from './core_private';
@ -44,7 +44,6 @@ export const CACHED_TEMPLATE_PROVIDER: Array<any /*Type | Provider | any[]*/> =
[{provide: XHR, useClass: CachedXHR}]; [{provide: XHR, useClass: CachedXHR}];
function _initGlobals() { function _initGlobals() {
lockRunMode();
initDomAdapter(); initDomAdapter();
reflector.reflectionCapabilities = new ReflectionCapabilities(); reflector.reflectionCapabilities = new ReflectionCapabilities();
} }
@ -212,13 +211,13 @@ export function bootstrap<C>(
precompile = normalizeArray(customProvidersOrDynamicModule.precompile); precompile = normalizeArray(customProvidersOrDynamicModule.precompile);
compiler = customProvidersOrDynamicModule.compiler; compiler = customProvidersOrDynamicModule.compiler;
} }
let deprecationMessages: string[] = []; const deprecationMessages: string[] = [];
if (providers && providers.length > 0) { if (providers && providers.length > 0) {
// Note: This is a hack to still support the old way // Note: This is a hack to still support the old way
// of configuring platform directives / pipes and the compiler xhr. // of configuring platform directives / pipes and the compiler xhr.
// This will soon be deprecated! // This will soon be deprecated!
let inj = ReflectiveInjector.resolveAndCreate(providers); const inj = ReflectiveInjector.resolveAndCreate(providers);
let compilerConfig: CompilerConfig = inj.get(CompilerConfig, null); const compilerConfig: CompilerConfig = inj.get(CompilerConfig, null);
if (compilerConfig) { if (compilerConfig) {
// Note: forms read the platform directives / pipes, modify them // Note: forms read the platform directives / pipes, modify them
// and provide a CompilerConfig out of it // and provide a CompilerConfig out of it
@ -229,20 +228,20 @@ export function bootstrap<C>(
} else { } else {
// If nobody provided a CompilerConfig, use the // If nobody provided a CompilerConfig, use the
// PLATFORM_DIRECTIVES / PLATFORM_PIPES values directly. // PLATFORM_DIRECTIVES / PLATFORM_PIPES values directly.
let platformDirectives = inj.get(PLATFORM_DIRECTIVES, []); const platformDirectives = inj.get(PLATFORM_DIRECTIVES, []);
if (platformDirectives.length > 0) { if (platformDirectives.length > 0) {
deprecationMessages.push( deprecationMessages.push(
`Passing PLATFORM_DIRECTIVES to "bootstrap()" as provider is deprecated. Use the new parameter "directives" of "bootstrap()" instead.`); `Passing PLATFORM_DIRECTIVES to "bootstrap()" as provider is deprecated. Use the new parameter "directives" of "bootstrap()" instead.`);
} }
directives = directives.concat(platformDirectives); directives = directives.concat(platformDirectives);
let platformPipes = inj.get(PLATFORM_PIPES, []); const platformPipes = inj.get(PLATFORM_PIPES, []);
if (platformPipes.length > 0) { if (platformPipes.length > 0) {
deprecationMessages.push( deprecationMessages.push(
`Passing PLATFORM_PIPES to "bootstrap()" as provider is deprecated. Use the new parameter "pipes" of "bootstrap()" instead.`); `Passing PLATFORM_PIPES to "bootstrap()" as provider is deprecated. Use the new parameter "pipes" of "bootstrap()" instead.`);
} }
pipes = pipes.concat(platformPipes); pipes = pipes.concat(platformPipes);
} }
let xhr = inj.get(XHR, null); const xhr = inj.get(XHR, null);
if (xhr) { if (xhr) {
compilerProviders.push([{provide: XHR, useValue: xhr}]); compilerProviders.push([{provide: XHR, useValue: xhr}]);
deprecationMessages.push( deprecationMessages.push(
@ -250,7 +249,7 @@ export function bootstrap<C>(
} }
// Need to copy console from providers to compiler // Need to copy console from providers to compiler
// as well so that we can test the above deprecation messages! // as well so that we can test the above deprecation messages!
let console = inj.get(Console, null); const console = inj.get(Console, null);
if (console) { if (console) {
compilerProviders.push([{provide: Console, useValue: console}]); compilerProviders.push([{provide: Console, useValue: console}]);
} }
@ -258,10 +257,8 @@ export function bootstrap<C>(
if (!compiler) { if (!compiler) {
compiler = browserCompiler({providers: compilerProviders}); compiler = browserCompiler({providers: compilerProviders});
} }
deprecationMessages.forEach((msg) => { const console: Console = compiler.injector.get(Console);
let console: Console = compiler.injector.get(Console); deprecationMessages.forEach((msg) => { console.warn(msg); });
console.warn(msg);
});
@AppModule({ @AppModule({
providers: providers, providers: providers,

View File

@ -895,7 +895,7 @@ export declare class KeyValueDiffers {
static extend(factories: KeyValueDifferFactory[]): Provider; static extend(factories: KeyValueDifferFactory[]): Provider;
} }
/** @experimental */ /** @deprecated */
export declare function lockRunMode(): void; export declare function lockRunMode(): void;
/** @experimental */ /** @experimental */