From 98d49d4ce30436496105a6b159549ff54f670858 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Thu, 7 Jul 2016 14:42:46 -0700 Subject: [PATCH] refactor(core): make `lockRunMode` a noop and deprecate it. BREAKING CHANGE: - `lockRunMode` is deprecated and no more needed. Closes #9878 --- .../compiler-cli/integrationtest/test/init.ts | 4 --- modules/@angular/compiler-cli/src/codegen.ts | 6 ++-- .../@angular/compiler-cli/src/extract_i18n.ts | 6 ++-- modules/@angular/core/src/application_ref.ts | 31 +++++++++---------- .../platform-browser-dynamic/index.ts | 23 ++++++-------- tools/public_api_guard/core/index.d.ts | 2 +- 6 files changed, 29 insertions(+), 43 deletions(-) diff --git a/modules/@angular/compiler-cli/integrationtest/test/init.ts b/modules/@angular/compiler-cli/integrationtest/test/init.ts index a1f5703231..9834ae7648 100644 --- a/modules/@angular/compiler-cli/integrationtest/test/init.ts +++ b/modules/@angular/compiler-cli/integrationtest/test/init.ts @@ -12,7 +12,3 @@ require('reflect-metadata'); require('zone.js/dist/zone-node.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(); diff --git a/modules/@angular/compiler-cli/src/codegen.ts b/modules/@angular/compiler-cli/src/codegen.ts index 7354087736..4ea60f9dc2 100644 --- a/modules/@angular/compiler-cli/src/codegen.ts +++ b/modules/@angular/compiler-cli/src/codegen.ts @@ -11,7 +11,7 @@ * Intended to be used in a build step. */ 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 * as path from 'path'; import * as ts from 'typescript'; @@ -36,9 +36,7 @@ export class CodeGenerator { private options: AngularCompilerOptions, private program: ts.Program, public host: ts.CompilerHost, private staticReflector: StaticReflector, private resolver: CompileMetadataResolver, private compiler: compiler.OfflineCompiler, - private reflectorHost: ReflectorHost) { - lockRunMode(); - } + private reflectorHost: ReflectorHost) {} private readFileMetadata(absSourcePath: string): FileMetadata { const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath); diff --git a/modules/@angular/compiler-cli/src/extract_i18n.ts b/modules/@angular/compiler-cli/src/extract_i18n.ts index ca42ddc000..f69c616443 100644 --- a/modules/@angular/compiler-cli/src/extract_i18n.ts +++ b/modules/@angular/compiler-cli/src/extract_i18n.ts @@ -19,7 +19,7 @@ import * as ts from 'typescript'; import * as tsc from '@angular/tsc-wrapped'; import * as path from 'path'; import * as compiler from '@angular/compiler'; -import {ViewEncapsulation, lockRunMode} from '@angular/core'; +import {ViewEncapsulation} from '@angular/core'; import {StaticReflector} from './static_reflector'; 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, public host: ts.CompilerHost, private staticReflector: StaticReflector, private _resolver: CompileMetadataResolver, private _normalizer: DirectiveNormalizer, - private _reflectorHost: ReflectorHost, private _extractor: MessageExtractor) { - lockRunMode(); - } + private _reflectorHost: ReflectorHost, private _extractor: MessageExtractor) {} private _extractCmpMessages(components: compiler.CompileDirectiveMetadata[]): ExtractionResult { if (!components || !components.length) { diff --git a/modules/@angular/core/src/application_ref.ts b/modules/@angular/core/src/application_ref.ts index 5e4a7ce4ae..1ddd1dd2c8 100644 --- a/modules/@angular/core/src/application_ref.ts +++ b/modules/@angular/core/src/application_ref.ts @@ -64,30 +64,28 @@ export function enableProdMode(): void { _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, * it can't be changed any more. I.e. `isDevMode()` will always * 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 { + 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; + return _devMode; } /** @@ -104,7 +102,6 @@ export function createPlatform(injector: Injector): PlatformRef { throw new BaseException( 'There can be only one platform. Destroy the previous one to create a new one.'); } - lockRunMode(); _inPlatformCreate = true; try { _platform = injector.get(PlatformRef); diff --git a/modules/@angular/platform-browser-dynamic/index.ts b/modules/@angular/platform-browser-dynamic/index.ts index bf898f2e59..782a524d99 100644 --- a/modules/@angular/platform-browser-dynamic/index.ts +++ b/modules/@angular/platform-browser-dynamic/index.ts @@ -8,7 +8,7 @@ import {COMMON_DIRECTIVES, COMMON_PIPES} from '@angular/common'; 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 {Console, ReflectionCapabilities, reflector} from './core_private'; @@ -44,7 +44,6 @@ export const CACHED_TEMPLATE_PROVIDER: Array = [{provide: XHR, useClass: CachedXHR}]; function _initGlobals() { - lockRunMode(); initDomAdapter(); reflector.reflectionCapabilities = new ReflectionCapabilities(); } @@ -212,13 +211,13 @@ export function bootstrap( precompile = normalizeArray(customProvidersOrDynamicModule.precompile); compiler = customProvidersOrDynamicModule.compiler; } - let deprecationMessages: string[] = []; + const deprecationMessages: string[] = []; if (providers && providers.length > 0) { // Note: This is a hack to still support the old way // of configuring platform directives / pipes and the compiler xhr. // This will soon be deprecated! - let inj = ReflectiveInjector.resolveAndCreate(providers); - let compilerConfig: CompilerConfig = inj.get(CompilerConfig, null); + const inj = ReflectiveInjector.resolveAndCreate(providers); + const compilerConfig: CompilerConfig = inj.get(CompilerConfig, null); if (compilerConfig) { // Note: forms read the platform directives / pipes, modify them // and provide a CompilerConfig out of it @@ -229,20 +228,20 @@ export function bootstrap( } else { // If nobody provided a CompilerConfig, use the // PLATFORM_DIRECTIVES / PLATFORM_PIPES values directly. - let platformDirectives = inj.get(PLATFORM_DIRECTIVES, []); + const platformDirectives = inj.get(PLATFORM_DIRECTIVES, []); if (platformDirectives.length > 0) { deprecationMessages.push( `Passing PLATFORM_DIRECTIVES to "bootstrap()" as provider is deprecated. Use the new parameter "directives" of "bootstrap()" instead.`); } directives = directives.concat(platformDirectives); - let platformPipes = inj.get(PLATFORM_PIPES, []); + const platformPipes = inj.get(PLATFORM_PIPES, []); if (platformPipes.length > 0) { deprecationMessages.push( `Passing PLATFORM_PIPES to "bootstrap()" as provider is deprecated. Use the new parameter "pipes" of "bootstrap()" instead.`); } pipes = pipes.concat(platformPipes); } - let xhr = inj.get(XHR, null); + const xhr = inj.get(XHR, null); if (xhr) { compilerProviders.push([{provide: XHR, useValue: xhr}]); deprecationMessages.push( @@ -250,7 +249,7 @@ export function bootstrap( } // Need to copy console from providers to compiler // 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) { compilerProviders.push([{provide: Console, useValue: console}]); } @@ -258,10 +257,8 @@ export function bootstrap( if (!compiler) { compiler = browserCompiler({providers: compilerProviders}); } - deprecationMessages.forEach((msg) => { - let console: Console = compiler.injector.get(Console); - console.warn(msg); - }); + const console: Console = compiler.injector.get(Console); + deprecationMessages.forEach((msg) => { console.warn(msg); }); @AppModule({ providers: providers, diff --git a/tools/public_api_guard/core/index.d.ts b/tools/public_api_guard/core/index.d.ts index ac362694cf..07465bc6b6 100644 --- a/tools/public_api_guard/core/index.d.ts +++ b/tools/public_api_guard/core/index.d.ts @@ -895,7 +895,7 @@ export declare class KeyValueDiffers { static extend(factories: KeyValueDifferFactory[]): Provider; } -/** @experimental */ +/** @deprecated */ export declare function lockRunMode(): void; /** @experimental */