From 8c1adabe1cbcb4f2c7ba643d7060f8793aaf69dd Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 14 Apr 2015 07:47:12 -0700 Subject: [PATCH] refactor(change_detection): renamed BindingPropagationConfig to ChangeDetectorRef --- modules/angular2/change_detection.js | 2 +- .../abstract_change_detector.js | 6 +-- .../binding_propagation_config.js | 29 ------------ .../change_detection_jit_generator.es6 | 4 +- .../change_detection/change_detector_ref.js | 45 +++++++++++++++++++ .../dynamic_change_detector.js | 8 ++-- .../pipes/iterable_changes.js | 2 +- .../pipes/keyvalue_changes.js | 2 +- .../src/change_detection/pipes/null_pipe.js | 2 +- .../change_detection/pipes/pipe_registry.js | 6 +-- .../src/core/compiler/element_injector.js | 14 +++--- modules/angular2/src/core/compiler/view.js | 2 +- .../src/core/compiler/view_factory.js | 8 ++-- .../change_detection/change_detection_spec.js | 8 ++-- .../change_detection/pipe_registry_spec.js | 2 +- .../core/compiler/element_injector_spec.js | 8 ++-- .../test/core/compiler/integration_spec.js | 14 +++--- 17 files changed, 89 insertions(+), 73 deletions(-) delete mode 100644 modules/angular2/src/change_detection/binding_propagation_config.js create mode 100644 modules/angular2/src/change_detection/change_detector_ref.js diff --git a/modules/angular2/change_detection.js b/modules/angular2/change_detection.js index 2f10fe2dc2..ec559d1e83 100644 --- a/modules/angular2/change_detection.js +++ b/modules/angular2/change_detection.js @@ -20,7 +20,7 @@ export {DynamicProtoChangeDetector, JitProtoChangeDetector} from './src/change_d export {BindingRecord} from './src/change_detection/binding_record'; export {DirectiveRecord} from './src/change_detection/directive_record'; export {DynamicChangeDetector} from './src/change_detection/dynamic_change_detector'; -export {BindingPropagationConfig} from './src/change_detection/binding_propagation_config'; +export {ChangeDetectorRef} from './src/change_detection/change_detector_ref'; export {PipeRegistry} from './src/change_detection/pipes/pipe_registry'; export {uninitialized} from './src/change_detection/change_detection_util'; export {NO_CHANGE, Pipe} from './src/change_detection/pipes/pipe'; diff --git a/modules/angular2/src/change_detection/abstract_change_detector.js b/modules/angular2/src/change_detection/abstract_change_detector.js index ab416d9e86..97f854b236 100644 --- a/modules/angular2/src/change_detection/abstract_change_detector.js +++ b/modules/angular2/src/change_detection/abstract_change_detector.js @@ -1,6 +1,6 @@ import {isPresent} from 'angular2/src/facade/lang'; import {List, ListWrapper} from 'angular2/src/facade/collection'; -import {BindingPropagationConfig} from './binding_propagation_config'; +import {ChangeDetectorRef} from './change_detector_ref'; import {ChangeDetector} from './interfaces'; import {CHECK_ALWAYS, CHECK_ONCE, CHECKED, DETACHED, ON_PUSH} from './constants'; @@ -9,13 +9,13 @@ export class AbstractChangeDetector extends ChangeDetector { shadowDomChildren:List; parent:ChangeDetector; mode:string; - bindingPropagationConfig:BindingPropagationConfig; + changeDetectorRef:ChangeDetectorRef; constructor() { super(); this.lightDomChildren = []; this.shadowDomChildren = []; - this.bindingPropagationConfig = new BindingPropagationConfig(this); + this.changeDetectorRef = new ChangeDetectorRef(this); this.mode = null; } diff --git a/modules/angular2/src/change_detection/binding_propagation_config.js b/modules/angular2/src/change_detection/binding_propagation_config.js deleted file mode 100644 index 66730d19d4..0000000000 --- a/modules/angular2/src/change_detection/binding_propagation_config.js +++ /dev/null @@ -1,29 +0,0 @@ -import {ChangeDetector} from './interfaces'; -import {CHECK_ONCE, DETACHED, CHECK_ALWAYS} from './constants'; - -/** - * @exportedAs angular2/change_detection - */ -export class BindingPropagationConfig { - _cd:ChangeDetector; - - constructor(cd:ChangeDetector) { - this._cd = cd; - } - - shouldBePropagated() { - this._cd.mode = CHECK_ONCE; - } - - shouldBePropagatedFromRoot() { - this._cd.markPathToRootAsCheckOnce(); - } - - shouldNotPropagate() { - this._cd.mode = DETACHED; - } - - shouldAlwaysPropagate() { - this._cd.mode = CHECK_ALWAYS; - } -} \ No newline at end of file diff --git a/modules/angular2/src/change_detection/change_detection_jit_generator.es6 b/modules/angular2/src/change_detection/change_detection_jit_generator.es6 index e0c8c098ba..c8a774e4c8 100644 --- a/modules/angular2/src/change_detection/change_detection_jit_generator.es6 +++ b/modules/angular2/src/change_detection/change_detection_jit_generator.es6 @@ -362,13 +362,13 @@ export class ChangeDetectorJITGenerator { var change = this.changeNames[r.selfIndex]; var pipe = this.pipeNames[r.selfIndex]; - var bpc = r.mode === RECORD_TYPE_BINDING_PIPE ? "this.bindingPropagationConfig" : "null"; + var cdRef = r.mode === RECORD_TYPE_BINDING_PIPE ? "this.changeDetectorRef" : "null"; var update = this.genUpdateDirectiveOrElement(r); var addToChanges = this.genAddToChanges(r); var lastInDirective = this.genNotifyOnChanges(r); - return pipeCheckTemplate(r.selfIndex - 1, context, bpc, pipe, r.name, oldValue, newValue, change, + return pipeCheckTemplate(r.selfIndex - 1, context, cdRef, pipe, r.name, oldValue, newValue, change, update, addToChanges, lastInDirective); } diff --git a/modules/angular2/src/change_detection/change_detector_ref.js b/modules/angular2/src/change_detection/change_detector_ref.js new file mode 100644 index 0000000000..c7f2acefa7 --- /dev/null +++ b/modules/angular2/src/change_detection/change_detector_ref.js @@ -0,0 +1,45 @@ +import {ChangeDetector} from './interfaces'; +import {CHECK_ONCE, DETACHED, CHECK_ALWAYS} from './constants'; + +/** + * Controls change detection. + * + * [ChangeDetectorRef] allows requesting checks for detectors that rely on observables. It also allows detaching and + * attaching change detector subtrees. + * + * @exportedAs angular2/change_detection + */ +export class ChangeDetectorRef { + _cd:ChangeDetector; + + constructor(cd:ChangeDetector) { + this._cd = cd; + } + + /** + * Request to check all ON_PUSH ancestors. + */ + requestCheck() { + this._cd.markPathToRootAsCheckOnce(); + } + + /** + * Detaches the change detector from the change detector tree. + * + * The detached change detector will not be checked until it is reattached. + */ + detach() { + this._cd.mode = DETACHED; + } + + /** + * Reattach the change detector to the change detector tree. + * + * This also requests a check of this change detector. This reattached change detector will be checked during the + * next change detection run. + */ + reattach() { + this._cd.mode = CHECK_ALWAYS; + this.requestCheck(); + } +} \ No newline at end of file diff --git a/modules/angular2/src/change_detection/dynamic_change_detector.js b/modules/angular2/src/change_detection/dynamic_change_detector.js index af4446fb84..3d5504d189 100644 --- a/modules/angular2/src/change_detection/dynamic_change_detector.js +++ b/modules/angular2/src/change_detection/dynamic_change_detector.js @@ -245,12 +245,12 @@ export class DynamicChangeDetector extends AbstractChangeDetector { } // Currently, only pipes that used in bindings in the template get - // the bindingPropagationConfig of the encompassing component. + // the changeDetectorRef of the encompassing component. // // In the future, pipes declared in the bind configuration should - // be able to access the bindingPropagationConfig of that component. - var bpc = proto.mode === RECORD_TYPE_BINDING_PIPE ? this.bindingPropagationConfig : null; - var pipe = this.pipeRegistry.get(proto.name, context, bpc); + // be able to access the changeDetectorRef of that component. + var cdr = proto.mode === RECORD_TYPE_BINDING_PIPE ? this.changeDetectorRef : null; + var pipe = this.pipeRegistry.get(proto.name, context, cdr); this._writePipe(proto, pipe); return pipe; } diff --git a/modules/angular2/src/change_detection/pipes/iterable_changes.js b/modules/angular2/src/change_detection/pipes/iterable_changes.js index 4ab8bf0110..a3dac9e50d 100644 --- a/modules/angular2/src/change_detection/pipes/iterable_changes.js +++ b/modules/angular2/src/change_detection/pipes/iterable_changes.js @@ -21,7 +21,7 @@ export class IterableChangesFactory { return IterableChanges.supportsObj(obj); } - create(bpc):Pipe { + create(cdRef):Pipe { return new IterableChanges(); } } diff --git a/modules/angular2/src/change_detection/pipes/keyvalue_changes.js b/modules/angular2/src/change_detection/pipes/keyvalue_changes.js index 155dd21ab5..9cff9b9f8e 100644 --- a/modules/angular2/src/change_detection/pipes/keyvalue_changes.js +++ b/modules/angular2/src/change_detection/pipes/keyvalue_changes.js @@ -11,7 +11,7 @@ export class KeyValueChangesFactory { return KeyValueChanges.supportsObj(obj); } - create(bpc):Pipe { + create(cdRef):Pipe { return new KeyValueChanges(); } } diff --git a/modules/angular2/src/change_detection/pipes/null_pipe.js b/modules/angular2/src/change_detection/pipes/null_pipe.js index de6728ebad..1f78551ff6 100644 --- a/modules/angular2/src/change_detection/pipes/null_pipe.js +++ b/modules/angular2/src/change_detection/pipes/null_pipe.js @@ -9,7 +9,7 @@ export class NullPipeFactory { return NullPipe.supportsObj(obj); } - create(bpc):Pipe { + create(cdRef):Pipe { return new NullPipe(); } } diff --git a/modules/angular2/src/change_detection/pipes/pipe_registry.js b/modules/angular2/src/change_detection/pipes/pipe_registry.js index 3674b7d03e..f4edc72caf 100644 --- a/modules/angular2/src/change_detection/pipes/pipe_registry.js +++ b/modules/angular2/src/change_detection/pipes/pipe_registry.js @@ -1,7 +1,7 @@ import {List, ListWrapper} from 'angular2/src/facade/collection'; import {isBlank, isPresent, BaseException, CONST} from 'angular2/src/facade/lang'; import {Pipe} from './pipe'; -import {BindingPropagationConfig} from '../binding_propagation_config'; +import {ChangeDetectorRef} from '../change_detector_ref'; export class PipeRegistry { config; @@ -10,7 +10,7 @@ export class PipeRegistry { this.config = config; } - get(type:string, obj, bpc:BindingPropagationConfig):Pipe { + get(type:string, obj, cdRef:ChangeDetectorRef):Pipe { var listOfConfigs = this.config[type]; if (isBlank(listOfConfigs)) { throw new BaseException(`Cannot find a pipe for type '${type}' object '${obj}'`); @@ -23,6 +23,6 @@ export class PipeRegistry { throw new BaseException(`Cannot find a pipe for type '${type}' object '${obj}'`); } - return matchingConfig.create(bpc); + return matchingConfig.create(cdRef); } } \ No newline at end of file diff --git a/modules/angular2/src/core/compiler/element_injector.js b/modules/angular2/src/core/compiler/element_injector.js index 3b97228574..50fd68b65a 100644 --- a/modules/angular2/src/core/compiler/element_injector.js +++ b/modules/angular2/src/core/compiler/element_injector.js @@ -8,7 +8,7 @@ import * as viewModule from 'angular2/src/core/compiler/view'; import {ViewContainer} from 'angular2/src/core/compiler/view_container'; import {NgElement} from 'angular2/src/core/compiler/ng_element'; import {Directive, Component, onChange, onDestroy, onAllChangesDone} from 'angular2/src/core/annotations/annotations'; -import {BindingPropagationConfig} from 'angular2/change_detection'; +import {ChangeDetectorRef} from 'angular2/change_detection'; import {QueryList} from './query_list'; var _MAX_DIRECTIVE_CONSTRUCTION_COUNTER = 10; @@ -46,7 +46,7 @@ class StaticKeys { viewId:number; ngElementId:number; viewContainerId:number; - bindingPropagationConfigId:number; + changeDetectorRefId:number; elementRefId:number; constructor() { @@ -54,7 +54,7 @@ class StaticKeys { this.viewId = Key.get(viewModule.AppView).id; this.ngElementId = Key.get(NgElement).id; this.viewContainerId = Key.get(ViewContainer).id; - this.bindingPropagationConfigId = Key.get(BindingPropagationConfig).id; + this.changeDetectorRefId = Key.get(ChangeDetectorRef).id; this.elementRefId = Key.get(ElementRef).id; } @@ -298,13 +298,13 @@ export class PreBuiltObjects { view:viewModule.AppView; element:NgElement; viewContainer:ViewContainer; - bindingPropagationConfig:BindingPropagationConfig; + changeDetectorRef:ChangeDetectorRef; constructor(view, element:NgElement, viewContainer:ViewContainer, - bindingPropagationConfig:BindingPropagationConfig) { + changeDetectorRef:ChangeDetectorRef) { this.view = view; this.element = element; this.viewContainer = viewContainer; - this.bindingPropagationConfig = bindingPropagationConfig; + this.changeDetectorRef = changeDetectorRef; } } @@ -885,7 +885,7 @@ export class ElementInjector extends TreeNode { if (keyId === staticKeys.viewId) return this._preBuiltObjects.view; if (keyId === staticKeys.ngElementId) return this._preBuiltObjects.element; if (keyId === staticKeys.viewContainerId) return this._preBuiltObjects.viewContainer; - if (keyId === staticKeys.bindingPropagationConfigId) return this._preBuiltObjects.bindingPropagationConfig; + if (keyId === staticKeys.changeDetectorRefId) return this._preBuiltObjects.changeDetectorRef; //TODO add other objects as needed return _undefined; diff --git a/modules/angular2/src/core/compiler/view.js b/modules/angular2/src/core/compiler/view.js index ce2e40158c..6e68c644cb 100644 --- a/modules/angular2/src/core/compiler/view.js +++ b/modules/angular2/src/core/compiler/view.js @@ -1,6 +1,6 @@ import {ListWrapper, MapWrapper, Map, StringMapWrapper, List} from 'angular2/src/facade/collection'; import {AST, Locals, ChangeDispatcher, ProtoChangeDetector, ChangeDetector, - ChangeRecord, BindingRecord, DirectiveRecord, BindingPropagationConfig} from 'angular2/change_detection'; + ChangeRecord, BindingRecord, DirectiveRecord, ChangeDetectorRef} from 'angular2/change_detection'; import {ProtoElementInjector, ElementInjector, PreBuiltObjects, DirectiveBinding} from './element_injector'; import {ElementBinder} from './element_binder'; diff --git a/modules/angular2/src/core/compiler/view_factory.js b/modules/angular2/src/core/compiler/view_factory.js index 1996111a9e..036bbd35b3 100644 --- a/modules/angular2/src/core/compiler/view_factory.js +++ b/modules/angular2/src/core/compiler/view_factory.js @@ -5,7 +5,7 @@ import {isPresent, isBlank, BaseException} from 'angular2/src/facade/lang'; import {NgElement} from 'angular2/src/core/compiler/ng_element'; import * as vcModule from './view_container'; import * as viewModule from './view'; -import {BindingPropagationConfig} from 'angular2/change_detection'; +import {ChangeDetectorRef} from 'angular2/change_detection'; // TODO(tbosch): Make this an OpaqueToken as soon as our transpiler supports this! export const VIEW_POOL_CAPACITY = 'ViewFactory.viewPoolCapacity'; @@ -77,12 +77,12 @@ export class ViewFactory { elementInjectors[binderIdx] = elementInjector; // componentChildViews - var bindingPropagationConfig = null; + var changeDetectorRef = null; if (binder.hasStaticComponent()) { var childView = this._createView(binder.nestedProtoView); changeDetector.addShadowDomChild(childView.changeDetector); - bindingPropagationConfig = new BindingPropagationConfig(childView.changeDetector); + changeDetectorRef = new ChangeDetectorRef(childView.changeDetector); componentChildViews[binderIdx] = childView; } @@ -97,7 +97,7 @@ export class ViewFactory { // preBuiltObjects if (isPresent(elementInjector)) { preBuiltObjects[binderIdx] = new eli.PreBuiltObjects(view, new NgElement(view, binderIdx), viewContainer, - bindingPropagationConfig); + changeDetectorRef); } } diff --git a/modules/angular2/test/change_detection/change_detection_spec.js b/modules/angular2/test/change_detection/change_detection_spec.js index fb28ee07c3..93f6d43b00 100644 --- a/modules/angular2/test/change_detection/change_detection_spec.js +++ b/modules/angular2/test/change_detection/change_detection_spec.js @@ -673,7 +673,7 @@ export function main() { cd.detectChanges(); - expect(registry.bpc).toBe(cd.bindingPropagationConfig); + expect(registry.cdRef).toBe(cd.changeDetectorRef); }); }); @@ -762,7 +762,7 @@ class FakePipeRegistry extends PipeRegistry { numberOfLookups:number; pipeType:string; factory:Function; - bpc:any; + cdRef:any; constructor(pipeType, factory) { super({}); @@ -771,10 +771,10 @@ class FakePipeRegistry extends PipeRegistry { this.numberOfLookups = 0; } - get(type:string, obj, bpc) { + get(type:string, obj, cdRef) { if (type != this.pipeType) return null; this.numberOfLookups ++; - this.bpc = bpc; + this.cdRef = cdRef; return this.factory(); } } diff --git a/modules/angular2/test/change_detection/pipe_registry_spec.js b/modules/angular2/test/change_detection/pipe_registry_spec.js index 76ee6efa8e..c345f36043 100644 --- a/modules/angular2/test/change_detection/pipe_registry_spec.js +++ b/modules/angular2/test/change_detection/pipe_registry_spec.js @@ -51,7 +51,7 @@ class PipeFactory { return this.shouldSupport; } - create(bpc):Pipe { + create(cdRef):Pipe { return this.pipe; } } \ No newline at end of file diff --git a/modules/angular2/test/core/compiler/element_injector_spec.js b/modules/angular2/test/core/compiler/element_injector_spec.js index 78beabf87e..c52ab8a0a8 100644 --- a/modules/angular2/test/core/compiler/element_injector_spec.js +++ b/modules/angular2/test/core/compiler/element_injector_spec.js @@ -11,7 +11,7 @@ import {AppProtoView, AppView} from 'angular2/src/core/compiler/view'; import {ViewContainer} from 'angular2/src/core/compiler/view_container'; import {NgElement} from 'angular2/src/core/compiler/ng_element'; import {Directive} from 'angular2/src/core/annotations/annotations'; -import {BindingPropagationConfig, Parser, Lexer} from 'angular2/change_detection'; +import {ChangeDetectorRef, Parser, Lexer} from 'angular2/change_detection'; import {ViewRef, Renderer, EventBinding} from 'angular2/src/render/api'; import {QueryList} from 'angular2/src/core/compiler/query_list'; @@ -620,11 +620,11 @@ export function main() { expect(inj.get(ViewContainer)).toEqual(viewContainer); }); - it('should return bindingPropagationConfig', function () { - var config = new BindingPropagationConfig(null); + it('should return changeDetectorRef', function () { + var config = new ChangeDetectorRef(null); var inj = injector([], null, null, new PreBuiltObjects(null, null, null, config)); - expect(inj.get(BindingPropagationConfig)).toEqual(config); + expect(inj.get(ChangeDetectorRef)).toEqual(config); }); }); diff --git a/modules/angular2/test/core/compiler/integration_spec.js b/modules/angular2/test/core/compiler/integration_spec.js index c6c0020c8e..02898c2d23 100644 --- a/modules/angular2/test/core/compiler/integration_spec.js +++ b/modules/angular2/test/core/compiler/integration_spec.js @@ -22,7 +22,7 @@ import {PromiseWrapper} from 'angular2/src/facade/async'; import {Injector, bind} from 'angular2/di'; import {dynamicChangeDetection, - ChangeDetection, DynamicChangeDetection, Pipe, PipeRegistry, BindingPropagationConfig, ON_PUSH} from 'angular2/change_detection'; + ChangeDetection, DynamicChangeDetection, Pipe, PipeRegistry, ChangeDetectorRef, ON_PUSH} from 'angular2/change_detection'; import {Decorator, Component, Viewport, DynamicComponent} from 'angular2/src/core/annotations/annotations'; import {View} from 'angular2/src/core/annotations/view'; @@ -390,7 +390,7 @@ export function main() { }) })); - describe("BindingPropagationConfig", () => { + describe("ChangeDetectorRef", () => { it("can be used to disable the change detection of the component's template", inject([TestBed, AsyncTestCompleter], (tb, async) => { @@ -802,12 +802,12 @@ class MyDir { @View({template: '{{field}}'}) class PushBasedComp { numberOfChecks:number; - bpc:BindingPropagationConfig; + ref:ChangeDetectorRef; prop; - constructor(bpc:BindingPropagationConfig) { + constructor(ref:ChangeDetectorRef) { this.numberOfChecks = 0; - this.bpc = bpc; + this.ref = ref; } get field(){ @@ -816,7 +816,7 @@ class PushBasedComp { } propagate() { - this.bpc.shouldBePropagatedFromRoot(); + this.ref.requestCheck(); } } @@ -943,7 +943,7 @@ class DoublePipeFactory { return true; } - create(bpc) { + create(cdRef) { return new DoublePipe(); } }