From 4bb9c46cf1ce422e4f260975bf0d0c6f076889f0 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Tue, 3 Nov 2015 15:19:18 -0800 Subject: [PATCH] fix(core): Provide setDevMode() to enable/disable development mode in Javascript. --- modules/angular2/core.ts | 1 + .../core/ts/dev_mode/dev_mode_example.ts | 7 +++++ .../examples/core/ts/dev_mode/my_component.ts | 5 ++++ .../angular2/manual_typings/globals-es6.d.ts | 1 + modules/angular2/src/core/application_ref.ts | 2 ++ modules/angular2/src/core/dev_mode.dart | 1 + modules/angular2/src/core/dev_mode.ts | 1 + modules/angular2/src/core/facade/lang.dart | 8 ++++++ modules/angular2/src/core/facade/lang.ts | 28 ++++++++++++++++++- 9 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 modules/angular2/examples/core/ts/dev_mode/dev_mode_example.ts create mode 100644 modules/angular2/examples/core/ts/dev_mode/my_component.ts create mode 100644 modules/angular2/src/core/dev_mode.dart create mode 100644 modules/angular2/src/core/dev_mode.ts diff --git a/modules/angular2/core.ts b/modules/angular2/core.ts index b5eb20c3f0..390ed4b5ea 100644 --- a/modules/angular2/core.ts +++ b/modules/angular2/core.ts @@ -20,3 +20,4 @@ export * from './src/common/forms'; export * from './src/core/debug'; export * from './src/core/change_detection'; export * from './src/core/ambient'; +export * from './src/core/dev_mode'; diff --git a/modules/angular2/examples/core/ts/dev_mode/dev_mode_example.ts b/modules/angular2/examples/core/ts/dev_mode/dev_mode_example.ts new file mode 100644 index 0000000000..d3ec8bcbdc --- /dev/null +++ b/modules/angular2/examples/core/ts/dev_mode/dev_mode_example.ts @@ -0,0 +1,7 @@ +// #docregion enableDevMode +import {bootstrap, enableDevMode} from 'angular2/core'; +import {MyComponent} from 'my_component'; + +enableDevMode(); +bootstrap(MyComponent); +// #enddocregion diff --git a/modules/angular2/examples/core/ts/dev_mode/my_component.ts b/modules/angular2/examples/core/ts/dev_mode/my_component.ts new file mode 100644 index 0000000000..e265b80648 --- /dev/null +++ b/modules/angular2/examples/core/ts/dev_mode/my_component.ts @@ -0,0 +1,5 @@ +import {Component} from 'angular2/core'; + +@Component({selector: 'my-component', template: '

My Component

'}) +export class MyComponent { +} diff --git a/modules/angular2/manual_typings/globals-es6.d.ts b/modules/angular2/manual_typings/globals-es6.d.ts index eb8614f505..02540be14c 100644 --- a/modules/angular2/manual_typings/globals-es6.d.ts +++ b/modules/angular2/manual_typings/globals-es6.d.ts @@ -24,6 +24,7 @@ interface BrowserNodeGlobal { zone: Zone; getAngularTestability: Function; getAllAngularTestabilities: Function; + angularDevMode: boolean; setTimeout: Function; clearTimeout: Function; setInterval: Function; diff --git a/modules/angular2/src/core/application_ref.ts b/modules/angular2/src/core/application_ref.ts index c0ef8483c3..bde6c2d533 100644 --- a/modules/angular2/src/core/application_ref.ts +++ b/modules/angular2/src/core/application_ref.ts @@ -48,6 +48,7 @@ import {Compiler_} from "./linker/compiler"; import {wtfLeave, wtfCreateScope, WtfScopeFn} from './profile/profile'; import {ChangeDetectorRef} from 'angular2/src/core/change_detection/change_detector_ref'; import {AMBIENT_DIRECTIVES, AMBIENT_PIPES} from "angular2/src/core/ambient"; +import {lockDevMode} from 'angular2/src/core/facade/lang'; import {COMMON_DIRECTIVES, COMMON_PIPES} from "angular2/common"; /** @@ -126,6 +127,7 @@ var _platform: PlatformRef; export function platformCommon(providers?: Array, initializer?: () => void): PlatformRef { + lockDevMode(); if (isPresent(_platform)) { if (isBlank(providers)) { return _platform; diff --git a/modules/angular2/src/core/dev_mode.dart b/modules/angular2/src/core/dev_mode.dart new file mode 100644 index 0000000000..ad6c2091ea --- /dev/null +++ b/modules/angular2/src/core/dev_mode.dart @@ -0,0 +1 @@ +// Dart development mode is determined by checked mode. \ No newline at end of file diff --git a/modules/angular2/src/core/dev_mode.ts b/modules/angular2/src/core/dev_mode.ts new file mode 100644 index 0000000000..b3fa5ea65d --- /dev/null +++ b/modules/angular2/src/core/dev_mode.ts @@ -0,0 +1 @@ +export {enableDevMode} from 'angular2/src/core/facade/lang'; \ No newline at end of file diff --git a/modules/angular2/src/core/facade/lang.dart b/modules/angular2/src/core/facade/lang.dart index 6f0887deaf..44d343d493 100644 --- a/modules/angular2/src/core/facade/lang.dart +++ b/modules/angular2/src/core/facade/lang.dart @@ -241,6 +241,14 @@ bool isJsObject(o) { return false; } +void lockDevMode() { + // lockDevMode() has no effect in Dart. +} + +void enableDevMode() { + // enableDevMode() has no effect in Dart. +} + bool assertionsEnabled() { var k = false; assert((k = true)); diff --git a/modules/angular2/src/core/facade/lang.ts b/modules/angular2/src/core/facade/lang.ts index 96c5b8393d..4a6351bf7a 100644 --- a/modules/angular2/src/core/facade/lang.ts +++ b/modules/angular2/src/core/facade/lang.ts @@ -39,8 +39,34 @@ export function getTypeNameForDebugging(type: Type): string { export var Math = _global.Math; export var Date = _global.Date; +var _devMode: boolean = !!_global.angularDevMode; +var _devModeLocked: boolean = false; + +export function lockDevMode() { + _devModeLocked = true; +} + +/** + * Enable Angular's development mode, which turns on assertions and other + * checks within the framework. + * + * One important assertion this enables verifies that a change detection pass + * does not result in additional changes to any bindings (also known as + * unidirectional data flow). + * + * {@example core/ts/dev_mode/dev_mode_example.ts region='enableDevMode'} + */ +export function enableDevMode() { + // TODO(alxhub): Refactor out of facade/lang as per issue #5157. + if (_devModeLocked) { + // Cannot use BaseException as that ends up importing from facade/lang. + throw 'Cannot enable dev mode after platform setup.'; + } + _devMode = true; +} + export function assertionsEnabled(): boolean { - return false; + return _devMode; } // TODO: remove calls to assert in production environment