diff --git a/_tests/test/compiler_integration/invalid_change_detection_link_test.dart b/_tests/test/compiler_integration/invalid_change_detection_link_test.dart index cc010f3057..50148798cc 100644 --- a/_tests/test/compiler_integration/invalid_change_detection_link_test.dart +++ b/_tests/test/compiler_integration/invalid_change_detection_link_test.dart @@ -19,7 +19,7 @@ void main() { @Component( selector: 'test', template: '', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class OnPushComponent {} """); diff --git a/_tests/test/compiler_integration/on_push_validation_test.dart b/_tests/test/compiler_integration/on_push_validation_test.dart index c55d885f2f..67ff157358 100644 --- a/_tests/test/compiler_integration/on_push_validation_test.dart +++ b/_tests/test/compiler_integration/on_push_validation_test.dart @@ -24,7 +24,7 @@ void main() { ''', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, directives: [DefaultComponent], ) class TestComponent {} @@ -32,7 +32,7 @@ void main() { allOf([ contains(''), contains( - '"DefaultComponent" doesn\'t use "ChangeDetectionStrategy.OnPush"', + '"DefaultComponent" doesn\'t use "ChangeDetectionStrategy.onPush"', ), ]), ]); @@ -56,7 +56,7 @@ void main() { ''', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, directives: [DefaultComponent], ) class TestComponent {} @@ -73,7 +73,7 @@ void main() { template: '''
''', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class TestComponent {} """, errors: [ @@ -91,7 +91,7 @@ void main() { @Component( selector: 'on-push', template: '', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class OnPushComponent {} @@ -101,7 +101,7 @@ void main() { ''', directives: [OnPushComponent], - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class TestComponent {} """, errors: [ @@ -109,7 +109,7 @@ void main() { contains('@skipOnPushValidation'), contains( 'Can only be applied to a component using ' - '"ChangeDetectionStrategy.Default"', + '"ChangeDetectionStrategy.checkAlways"', ), ]), ]); @@ -138,7 +138,7 @@ void main() { contains('@skipOnPushValidation'), contains( 'Can only be used in the template of a component using ' - '"ChangeDetectionStrategy.OnPush"', + '"ChangeDetectionStrategy.onPush"', ), ]), ]); diff --git a/_tests/test/core/change_detection/change_detection_link_test.dart b/_tests/test/core/change_detection/change_detection_link_test.dart index 61477e17d5..522fb9352b 100644 --- a/_tests/test/core/change_detection/change_detection_link_test.dart +++ b/_tests/test/core/change_detection/change_detection_link_test.dart @@ -110,7 +110,7 @@ class DefaultComponent { @Component( selector: 'on-push-container', template: '', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class OnPushContainerComponent { @Input() @@ -142,7 +142,7 @@ class LoadInOnPush { ''', directives: [OnPushContainerComponent], - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class OnPushAncestorComponent { @Input() @@ -170,7 +170,7 @@ class LoadInOnPushDescendant { ''', directives: [NgIf], - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class OnPushEmbeddedContainerComponent { OnPushEmbeddedContainerComponent(this._changeDetectorRef, this._ngZone); diff --git a/_tests/test/core/change_detection/detached_lifecycle_test.dart b/_tests/test/core/change_detection/detached_lifecycle_test.dart deleted file mode 100644 index ade100db6c..0000000000 --- a/_tests/test/core/change_detection/detached_lifecycle_test.dart +++ /dev/null @@ -1,235 +0,0 @@ -import 'package:test/test.dart'; -import 'package:ngdart/angular.dart'; -import 'package:ngtest/angular_test.dart'; - -import 'detached_lifecycle_test.template.dart' as ng; - -void main() { - late List logs; - - tearDown(disposeAnyRunningTest); - - setUp(() => logs = []); - - test('ChangeDetectionStrategy.Detached should behave strangely', () async { - final testBed = NgTestBed( - ng.createTestDetachedViaStrategyFactory(), - ); - - final fixture = await testBed.create(beforeChangeDetection: (comp) { - comp.logs = logs; - }); - - expect( - logs, - [ - 'ngAfterChanges', - 'ngOnInit', - 'ngAfterContentInit', - 'ngAfterViewInit', - ], - reason: 'Despite starting detached, all events are invoked', - ); - - expect( - fixture.text, - contains('Hello World'), - reason: 'Despite starting detached, initial bindings were used', - ); - - await fixture.update((comp) { - comp.logs = logs = []; - comp.text = 'Hello Galaxy'; - }); - - expect( - logs, - ['ngAfterChanges'], - reason: 'Change detection events are still being executed', - ); - - expect( - fixture.text, - isNot(contains('Hello Galaxy')), - reason: 'Change detection bindings were not updated, though', - ); - - await fixture.update((comp) { - comp.logs = logs = []; - // ignore: deprecated_member_use - comp.child!.reattach(); - }); - - expect( - logs, - ['ngAfterChanges'], - reason: 'Change detection events are executed after attach', - ); - - expect( - fixture.text, - contains('Hello Galaxy'), - reason: 'Change detection bindings are updated after attach', - ); - }); - - test('ChangeDetectorRef.detach() shoud behave in an expected manner', - () async { - final testBed = NgTestBed( - ng.createTestDetachedViaRefFactory(), - ); - - final fixture = await testBed.create(beforeChangeDetection: (comp) { - comp.logs = logs; - }); - - expect( - logs, - [ - 'ngAfterChanges', - 'ngOnInit', - 'ngAfterContentInit', - 'ngAfterViewInit', - ], - reason: 'Despite starting detached, all events are invoked', - ); - - expect( - fixture.text, - isNot(contains('Hello World')), - reason: 'Initial bindings were never read', - ); - - await fixture.update((comp) { - comp.logs = logs = []; - comp.text = 'Hello Galaxy'; - }); - - expect( - logs, - ['ngAfterChanges'], - reason: 'Change detection events are still being executed', - ); - - expect( - fixture.text, - isNot(contains('Hello Galaxy')), - reason: 'Updated bindings were not read', - ); - - await fixture.update((comp) { - comp.logs = logs = []; - // ignore: deprecated_member_use - comp.child!.reattach(); - }); - - expect( - logs, - ['ngAfterChanges'], - reason: 'Change detection events are executed after attach', - ); - - expect( - fixture.text, - contains('Hello Galaxy'), - reason: 'Change detection bindings are updated after attach', - ); - }); -} - -@Component( - selector: 'test-1', - directives: [ - DetachedViaStrategy, - ], - template: r''' - - - - ''', -) -class TestDetachedViaStrategy { - List logs = []; - - var text = 'Hello World'; - - @ViewChild(DetachedViaStrategy, read: ChangeDetectorRef) - ChangeDetectorRef? child; -} - -@Component( - selector: 'test-2', - directives: [ - DetachedViaRef, - ], - template: r''' - - - - ''', -) -class TestDetachedViaRef { - List logs = []; - - var text = 'Hello World'; - - @ViewChild(DetachedViaRef, read: ChangeDetectorRef) - ChangeDetectorRef? child; -} - -class Logger implements OnInit, AfterChanges, AfterViewInit, AfterContentInit { - @Input() - List logs = []; - - @Input() - String? text; - - @override - void ngAfterChanges() { - logs.add('ngAfterChanges'); - } - - @override - void ngAfterContentInit() { - logs.add('ngAfterContentInit'); - } - - @override - void ngAfterViewInit() { - logs.add('ngAfterViewInit'); - } - - @override - void ngOnInit() { - logs.add('ngOnInit'); - } -} - -@Component( - selector: 'detached-via-strategy', - template: r''' - {{text}} - - - - ''', - // ignore: deprecated_member_use - changeDetection: ChangeDetectionStrategy.Detached, -) -class DetachedViaStrategy extends Logger {} - -@Component( - selector: 'detached-via-ref', - template: r''' - {{text}} - - - - ''', -) -class DetachedViaRef extends Logger { - DetachedViaRef(ChangeDetectorRef changeDetectorRef) { - // ignore: deprecated_member_use - changeDetectorRef.detach(); - } -} diff --git a/_tests/test/core/change_detection/mark_child_for_check_test.dart b/_tests/test/core/change_detection/mark_child_for_check_test.dart index 1fd3dc9ebd..a40e4c288f 100644 --- a/_tests/test/core/change_detection/mark_child_for_check_test.dart +++ b/_tests/test/core/change_detection/mark_child_for_check_test.dart @@ -109,7 +109,7 @@ void main() { @Component( selector: 'child', template: '{{value}}', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class Child { var value = ''; @@ -245,7 +245,7 @@ abstract class HasValue { providers: [ ExistingProvider(HasValue, ChildWithExistingProvider), ], - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class ChildWithExistingProvider implements HasValue { @override diff --git a/_tests/test/core/change_detection/mark_for_check_test.dart b/_tests/test/core/change_detection/mark_for_check_test.dart index 5f6ca5271a..623cfcd35d 100644 --- a/_tests/test/core/change_detection/mark_for_check_test.dart +++ b/_tests/test/core/change_detection/mark_for_check_test.dart @@ -76,7 +76,7 @@ class UsesOnPushComponent { directives: [ NgIf, ], - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class OnPushComponent { final ChangeDetectorRef _changeDetector; diff --git a/_tests/test/core/change_detection/on_push_embedded_view_test.dart b/_tests/test/core/change_detection/on_push_embedded_view_test.dart index 4c6d9f1f55..2010d89b14 100644 --- a/_tests/test/core/change_detection/on_push_embedded_view_test.dart +++ b/_tests/test/core/change_detection/on_push_embedded_view_test.dart @@ -124,7 +124,7 @@ class TestComponent { template: ''' ''', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, exportAs: 'templateProducer', ) class TemplateProducerComponent implements OnInit { @@ -149,7 +149,7 @@ class TemplateProducerComponent implements OnInit {
{{text}}
''', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class TemplateConsumerComponent implements OnInit { @ViewChild('container', read: ViewContainerRef) diff --git a/_tests/test/core/linker/component_loader_test.dart b/_tests/test/core/linker/component_loader_test.dart index b26ae08d46..04c00d6c62 100644 --- a/_tests/test/core/linker/component_loader_test.dart +++ b/_tests/test/core/linker/component_loader_test.dart @@ -317,7 +317,7 @@ class DynamicComp extends Lifecycles { @Component( selector: 'dynamic-comp', template: 'Dynamic{{input}}', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class DynamicOnPushComp extends Lifecycles { DynamicOnPushComp(super.log); diff --git a/_tests/test/core/linker/integration/on_push_test.dart b/_tests/test/core/linker/integration/on_push_test.dart index 88bd05fb45..efb7fbc73d 100644 --- a/_tests/test/core/linker/integration/on_push_test.dart +++ b/_tests/test/core/linker/integration/on_push_test.dart @@ -99,7 +99,7 @@ void main() { @Component( selector: 'push-cmp-with-ref', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, template: '{{field}}', ) class PushCmpWithRef { @@ -150,7 +150,7 @@ class ManualCheckLoadedComponent { @Component( selector: 'event-cmp', template: '
', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class EventCmp { void noop() {} @@ -158,7 +158,7 @@ class EventCmp { @Component( selector: 'push-cmp', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, template: '{{field}}
', directives: [EventCmp, NgIf], @@ -203,7 +203,7 @@ class PushCmpWithRefHostComponent { @Component( selector: 'push-cmp-with-async', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, template: r'{{$pipe.async(field)}}', pipes: [AsyncPipe], ) diff --git a/ngcompiler/lib/v1/src/angular_compiler/cli/messages.dart b/ngcompiler/lib/v1/src/angular_compiler/cli/messages.dart index b7543cf8a3..3cecc55a7f 100644 --- a/ngcompiler/lib/v1/src/angular_compiler/cli/messages.dart +++ b/ngcompiler/lib/v1/src/angular_compiler/cli/messages.dart @@ -42,7 +42,7 @@ abstract class Messages { /// What message should be used for OnPush compatibility warnings. String warningForOnPushCompatibility(String name) { return '' - '"$name" doesn\'t use "ChangeDetectionStrategy.OnPush", but ' + '"$name" doesn\'t use "ChangeDetectionStrategy.onPush", but ' 'is used by a component that does. This is unsupported and unlikely ' 'to work as expected.'; } diff --git a/ngcompiler/lib/v1/src/compiler/compile_metadata.dart b/ngcompiler/lib/v1/src/compiler/compile_metadata.dart index fa154c20b5..fe9ceefcc9 100644 --- a/ngcompiler/lib/v1/src/compiler/compile_metadata.dart +++ b/ngcompiler/lib/v1/src/compiler/compile_metadata.dart @@ -451,7 +451,7 @@ class CompileDirectiveMetadata implements CompileMetadataWithType { final CompileDirectiveMetadataType metadataType; final String? selector; final String? exportAs; - final int? changeDetection; + final ChangeDetectionStrategy? changeDetection; final Map inputs; final Map inputTypes; final Map outputs; @@ -529,13 +529,13 @@ class CompileDirectiveMetadata implements CompileMetadataWithType { name = name.substring(0, name.length - 4); } return '$name in ${type.moduleUrl} ' - '(changeDetection: ${ChangeDetectionStrategy.toPrettyString(changeDetection!)})'; + '(changeDetection: ${changeDetection!.name})'; } bool get isComponent => metadataType == CompileDirectiveMetadataType.Component; - bool get isOnPush => changeDetection == ChangeDetectionStrategy.OnPush; + bool get isOnPush => changeDetection == ChangeDetectionStrategy.onPush; /// Whether the directive requires a change detector class to be generated. /// @@ -625,7 +625,7 @@ CompileDirectiveMetadata createHostComponentMeta( styles: const [], styleUrls: const [], ngContentSelectors: const []), - changeDetection: ChangeDetectionStrategy.Default, + changeDetection: ChangeDetectionStrategy.checkAlways, analyzedClass: analyzedClass, inputs: const {}, inputTypes: const {}, diff --git a/ngcompiler/lib/v1/src/compiler/identifiers.dart b/ngcompiler/lib/v1/src/compiler/identifiers.dart index fff79b21d1..e4f9f2b259 100644 --- a/ngcompiler/lib/v1/src/compiler/identifiers.dart +++ b/ngcompiler/lib/v1/src/compiler/identifiers.dart @@ -1,6 +1,7 @@ import 'compile_metadata.dart' show CompileIdentifierMetadata, CompileTokenMetadata; +const _angularRootUrl = 'package:ngdart/angular.dart'; const _angularLib = 'asset:ngdart/lib'; const _appViewUtilsModuleUrl = @@ -8,7 +9,6 @@ const _appViewUtilsModuleUrl = const _proxiesModuleUrl = '$_angularLib/src/runtime/proxies.dart'; const _cdModuleUrl = '$_angularLib/src/core/change_detection/change_detection.dart'; -const _angularRootUrl = 'package:ngdart/angular.dart'; const _ngIfUrl = '$_angularLib/src/common/directives/ng_if.dart'; const _ngForUrl = '$_angularLib/src/common/directives/ng_for.dart'; const _profileRuntimeModuleUrl = '$_angularLib/src/debug/profile_runtime.dart'; @@ -194,11 +194,11 @@ class Identifiers { ); static final ViewContainer = CompileIdentifierMetadata( name: 'ViewContainer', - moduleUrl: 'asset:ngdart/lib/src/core/linker/view_container.dart'); + moduleUrl: '$_angularLib/src/core/linker/view_container.dart'); static final ViewContainerToken = identifierToken(ViewContainer); static final ElementRef = CompileIdentifierMetadata( name: 'ElementRef', - moduleUrl: 'asset:ngdart/lib/src/core/linker/element_ref.dart'); + moduleUrl: '$_angularLib/src/core/linker/element_ref.dart'); static final ElementRefToken = identifierToken(ElementRef); static final ViewContainerRef = CompileIdentifierMetadata( name: 'ViewContainerRef', @@ -225,16 +225,19 @@ class Identifiers { name: 'ComponentRef', moduleUrl: _angularRootUrl); static final TemplateRef = CompileIdentifierMetadata( name: 'TemplateRef', - moduleUrl: 'asset:ngdart/lib/src/core/linker/template_ref.dart'); + moduleUrl: '$_angularLib/src/core/linker/template_ref.dart'); static final TemplateRefToken = identifierToken(Identifiers.TemplateRef); static final Injector = CompileIdentifierMetadata( - name: 'Injector', moduleUrl: 'asset:ngdart/lib/src/di/injector.dart'); + name: 'Injector', moduleUrl: '$_angularLib/src/di/injector.dart'); static final InjectorToken = identifierToken(Identifiers.Injector); static final ViewType = CompileIdentifierMetadata( name: 'ViewType', - moduleUrl: 'asset:ngdart/lib/src/core/linker/view_type.dart'); + moduleUrl: '$_angularLib/src/core/linker/view_type.dart'); static final ChangeDetectionStrategy = CompileIdentifierMetadata( name: 'ChangeDetectionStrategy', moduleUrl: _cdModuleUrl); + static final ChangeDetectionCheckedState = CompileIdentifierMetadata( + name: 'ChangeDetectionCheckedState', + moduleUrl: '$_angularLib/src/meta/change_detection_constants.dart'); static final identical = CompileIdentifierMetadata(name: 'identical'); static final profileSetup = CompileIdentifierMetadata( name: 'profileSetup', moduleUrl: _profileRuntimeModuleUrl); @@ -356,7 +359,7 @@ class Identifiers { static final NgContentRef = CompileIdentifierMetadata( name: 'NgContentRef', - moduleUrl: 'asset:ngdart/lib/src/core/linker/ng_content_ref.dart', + moduleUrl: '$_angularLib/src/core/linker/ng_content_ref.dart', ); static final NgContentRefToken = identifierToken(Identifiers.NgContentRef); } diff --git a/ngcompiler/lib/v1/src/compiler/semantic_analysis/matched_directive_converter.dart b/ngcompiler/lib/v1/src/compiler/semantic_analysis/matched_directive_converter.dart index f6aec5e54b..a25b2fe2ad 100644 --- a/ngcompiler/lib/v1/src/compiler/semantic_analysis/matched_directive_converter.dart +++ b/ngcompiler/lib/v1/src/compiler/semantic_analysis/matched_directive_converter.dart @@ -73,7 +73,7 @@ ir.MatchedDirective convertMatchedDirective( hasHostProperties: directive.hasHostProperties, isComponent: directive.directive.isComponent, isOnPush: - directive.directive.changeDetection == ChangeDetectionStrategy.OnPush, + directive.directive.changeDetection == ChangeDetectionStrategy.onPush, lifecycles: _lifecycles(directive.directive), ); } diff --git a/ngcompiler/lib/v1/src/compiler/template_parser/ast_template_parser.dart b/ngcompiler/lib/v1/src/compiler/template_parser/ast_template_parser.dart index 3f06276962..e74e37850f 100644 --- a/ngcompiler/lib/v1/src/compiler/template_parser/ast_template_parser.dart +++ b/ngcompiler/lib/v1/src/compiler/template_parser/ast_template_parser.dart @@ -306,7 +306,7 @@ class _BindDirectivesVisitor CompileContext.current.reportAndRecover(BuildError.forSourceSpan( annotation.sourceSpan, 'Can only be used in the template of a component using ' - '"ChangeDetectionStrategy.OnPush"', + '"ChangeDetectionStrategy.onPush"', )); } var componentAst = _ParseContext._firstComponent(context.boundDirectives); @@ -319,7 +319,7 @@ class _BindDirectivesVisitor CompileContext.current.reportAndRecover(BuildError.forSourceSpan( annotation.sourceSpan, 'Can only be applied to a component using ' - '"ChangeDetectionStrategy.Default"', + '"ChangeDetectionStrategy.checkAlways"', )); } } @@ -1231,7 +1231,7 @@ class _ElementFilter extends ast.RecursiveTemplateAstVisitor { } } -/// Validates that all bound components use `ChangeDetectionStrategy.OnPush`. +/// Validates that all bound components use `ChangeDetectionStrategy.onPush`. class _OnPushValidator extends InPlaceRecursiveTemplateVisitor { @override void visitElement(ng.ElementAst ast, [_]) { diff --git a/ngcompiler/lib/v1/src/compiler/view_compiler/compile_element.dart b/ngcompiler/lib/v1/src/compiler/view_compiler/compile_element.dart index 119b57c708..fe403e7796 100644 --- a/ngcompiler/lib/v1/src/compiler/view_compiler/compile_element.dart +++ b/ngcompiler/lib/v1/src/compiler/view_compiler/compile_element.dart @@ -392,7 +392,7 @@ class CompileElement extends CompileNode implements ProviderResolverHost { if (resolvedProvider.providerType == ProviderAstType.Component) { if (directiveMetadata?.changeDetection == - ChangeDetectionStrategy.OnPush) { + ChangeDetectionStrategy.onPush) { changeDetectorRefExpr = componentView; } } diff --git a/ngcompiler/lib/v1/src/compiler/view_compiler/constants.dart b/ngcompiler/lib/v1/src/compiler/view_compiler/constants.dart index b8e5db38a7..bfef4683d5 100644 --- a/ngcompiler/lib/v1/src/compiler/view_compiler/constants.dart +++ b/ngcompiler/lib/v1/src/compiler/view_compiler/constants.dart @@ -12,23 +12,13 @@ const classAttrName = 'class'; const styleAttrName = 'style'; final parentRenderNodeVar = o.variable('parentRenderNode'); -const List _changeDetectionStrategies = [ - 'Default', - 'CheckOnce', - 'Checked', - 'CheckAlways', - 'Detached', - 'OnPush', -]; - /// Converts value of a `ChangeDetectionStrategy` to refer to the static field. /// /// Otherwise the generated code refers to arbitrary integer values. -o.Expression changeDetectionStrategyToConst(int value) { - final name = _changeDetectionStrategies[value]; +o.Expression changeDetectionCheckToConst(String name) { return o.importExpr(CompileIdentifierMetadata( - name: 'ChangeDetectionStrategy.$name', - moduleUrl: Identifiers.ChangeDetectionStrategy.moduleUrl, + name: 'ChangeDetectionCheckedState.$name', + moduleUrl: Identifiers.ChangeDetectionCheckedState.moduleUrl, )); } diff --git a/ngcompiler/lib/v1/src/compiler/view_compiler/ir/provider_source.dart b/ngcompiler/lib/v1/src/compiler/view_compiler/ir/provider_source.dart index de5535b035..ffabaa0ad9 100644 --- a/ngcompiler/lib/v1/src/compiler/view_compiler/ir/provider_source.dart +++ b/ngcompiler/lib/v1/src/compiler/view_compiler/ir/provider_source.dart @@ -17,7 +17,7 @@ abstract class ProviderSource { /// Returns a reference to this provider's `ChangeDetectorRef`, if necessary. /// /// Returns a non-null result only if this provider instance is a component - /// that uses `ChangeDetectionStrategy.OnPush`. This result is used to + /// that uses `ChangeDetectionStrategy.onPush`. This result is used to /// implement `ChangeDetectorRef.markChildForCheck()`. /// /// Returns null otherwise. diff --git a/ngcompiler/lib/v1/src/compiler/view_compiler/view_builder.dart b/ngcompiler/lib/v1/src/compiler/view_compiler/view_builder.dart index db02854979..c12eb7b50b 100644 --- a/ngcompiler/lib/v1/src/compiler/view_compiler/view_builder.dart +++ b/ngcompiler/lib/v1/src/compiler/view_compiler/view_builder.dart @@ -26,7 +26,7 @@ import 'compile_element.dart' show CompileElement, CompileNode; import 'compile_view.dart'; import 'constants.dart' show - changeDetectionStrategyToConst, + changeDetectionCheckToConst, parentRenderNodeVar, DetectChangesVars, ViewConstructorVars; @@ -649,7 +649,7 @@ o.Constructor _createComponentViewConstructor(CompileView view) { o.SUPER_EXPR.callFn([ ViewConstructorVars.parentView, ViewConstructorVars.parentIndex, - changeDetectionStrategyToConst(_getChangeDetectionMode(view)), + changeDetectionCheckToConst(_getChangeDetectionCheckMode(view)), ]).toStmt() ], body: body, @@ -982,9 +982,9 @@ o.OutputType contextType(CompileView view) { )!; } -int _getChangeDetectionMode(CompileView view) { +String _getChangeDetectionCheckMode(CompileView view) { return view.viewType == ViewType.component && - view.component.changeDetection != ChangeDetectionStrategy.Default - ? ChangeDetectionStrategy.CheckOnce - : ChangeDetectionStrategy.CheckAlways; + view.component.changeDetection != ChangeDetectionStrategy.checkAlways + ? ChangeDetectionCheckedState.checkOnce.name + : ChangeDetectionCheckedState.checkAlways.name; } diff --git a/ngcompiler/lib/v1/src/source_gen/template_compiler/find_components.dart b/ngcompiler/lib/v1/src/source_gen/template_compiler/find_components.dart index 357fa04d26..4289be8654 100644 --- a/ngcompiler/lib/v1/src/source_gen/template_compiler/find_components.dart +++ b/ngcompiler/lib/v1/src/source_gen/template_compiler/find_components.dart @@ -683,7 +683,7 @@ class _ComponentVisitor final bool isChangeDetectionLink = // ignore: omit_local_variable_types linkInfo != null; if (isChangeDetectionLink && - !(isComponent && changeDetection == ChangeDetectionStrategy.OnPush)) { + !(isComponent && changeDetection == ChangeDetectionStrategy.onPush)) { _exceptionHandler.handle(ErrorMessageForAnnotation(linkInfo, 'Only supported on components that use "OnPush" change detection')); } @@ -819,11 +819,13 @@ class _ComponentVisitor defaultTo: ViewEncapsulation.Emulated, ); - int _changeDetection(ClassElement clazz, DartObject? value) { - return coerceInt( + ChangeDetectionStrategy _changeDetection( + ClassElement clazz, DartObject? value) { + return coerceEnum( value, 'changeDetection', - defaultTo: ChangeDetectionStrategy.Default, + ChangeDetectionStrategy.values, + defaultTo: ChangeDetectionStrategy.checkAlways, ); } diff --git a/ngdart/lib/angular.dart b/ngdart/lib/angular.dart index 6744c6f208..b8eecb2b2c 100644 --- a/ngdart/lib/angular.dart +++ b/ngdart/lib/angular.dart @@ -21,7 +21,7 @@ export 'src/bootstrap/run.dart' show runApp, runAppAsync; export 'src/common/directives.dart'; export 'src/common/pipes.dart'; export 'src/core/application_ref.dart' show ApplicationRef; -export 'src/core/application_tokens.dart' show APP_ID; +export 'src/core/application_tokens.dart' show appId; export 'src/core/change_detection.dart'; export 'src/core/exception_handler.dart' show ExceptionHandler; export 'src/core/linker.dart'; diff --git a/ngdart/lib/src/bootstrap/modules.dart b/ngdart/lib/src/bootstrap/modules.dart index bda1f2583e..32301df40f 100644 --- a/ngdart/lib/src/bootstrap/modules.dart +++ b/ngdart/lib/src/bootstrap/modules.dart @@ -7,18 +7,18 @@ import 'package:ngdart/src/di/injector.dart'; /// Returns a simple application [Injector] that is hand-authored. /// -/// Some of the services provided below ([ExceptionHandler], [APP_ID]) may be +/// Some of the services provided below ([ExceptionHandler], [appId]) may be /// overriden by the user-supplied injector - the returned [Injector] is /// used as the "base" application injector. Injector minimalApp() { return Injector.map({ - APP_ID: _createRandomAppId(), + appId: _createRandomAppId(), ExceptionHandler: const ExceptionHandler(), ComponentLoader: const ComponentLoader(), }); } -/// Creates a random [APP_ID] for use in CSS encapsulation. +/// Creates a random [appId] for use in CSS encapsulation. String _createRandomAppId() { final random = Random(); String char() => String.fromCharCode(97 + random.nextInt(26)); diff --git a/ngdart/lib/src/bootstrap/run.dart b/ngdart/lib/src/bootstrap/run.dart index ee1b06b267..b5379cfc50 100644 --- a/ngdart/lib/src/bootstrap/run.dart +++ b/ngdart/lib/src/bootstrap/run.dart @@ -16,7 +16,7 @@ import 'modules.dart'; /// /// This is more complicated than just creating a new Injector, because we want /// to make sure we allow [userProvidedInjector] to override _some_ top-level -/// services (`APP_ID`, `ExceptionHandler`) _and_ to ensure that Angular-level +/// services (`appId`, `ExceptionHandler`) _and_ to ensure that Angular-level /// services (`ApplicationRef`) get the user-provided versions. /// /// May override [createNgZone] to provide a custom callback to create one. This @@ -42,7 +42,7 @@ Injector appInjector( final userInjector = userProvidedInjector(appGlobalInjector); // ... and then we add ApplicationRef, which has the unique property of - // injecting services (specifically, `ExceptionHandler` and `APP_ID`) that + // injecting services (specifically, `ExceptionHandler` and `appId`) that // might have come from the user-provided injector, instead of the minimal. // // We also add other top-level services with similar constraints: @@ -53,7 +53,7 @@ Injector appInjector( userInjector, ); appViewUtils = AppViewUtils( - userInjector.provideToken(APP_ID), + userInjector.provideToken(appId), EventManager(ngZone), ); return userInjector; diff --git a/ngdart/lib/src/core/application_tokens.dart b/ngdart/lib/src/core/application_tokens.dart index 34ee7436f6..b8e73e564c 100644 --- a/ngdart/lib/src/core/application_tokens.dart +++ b/ngdart/lib/src/core/application_tokens.dart @@ -9,8 +9,8 @@ import 'package:ngdart/src/meta.dart'; /// bootstrapStatic( /// YourAppComponent, /// const [ -/// const Provider(APP_ID, useValue: 'my-unique-id'), +/// const Provider(appId, useValue: 'my-unique-id'), /// ], /// ) /// ``` -const APP_ID = OpaqueToken('APP_ID'); +const appId = OpaqueToken('appId'); diff --git a/ngdart/lib/src/core/change_detection/change_detector_ref.dart b/ngdart/lib/src/core/change_detection/change_detector_ref.dart index 9f6f524be0..7c5b584d0c 100644 --- a/ngdart/lib/src/core/change_detection/change_detector_ref.dart +++ b/ngdart/lib/src/core/change_detection/change_detector_ref.dart @@ -3,16 +3,16 @@ import 'package:meta/meta.dart'; /// Provides influence over how change detection should execute for a component. /// /// In practice, this is often used just for [markForCheck], which sets a -/// component that uses `ChangeDetectionStrategy.OnPush` as dirty for future +/// component that uses `ChangeDetectionStrategy.onPush` as dirty for future /// change detection. /// /// **NOTE**: This API is currently _transitional_. Please use carefully, and /// avoid methods that are marked `@Deprecated(...)`, as they will be eventually /// removed entirely. abstract class ChangeDetectorRef { - /// Marks this and all `ChangeDetectionStrategy.OnPush` ancestors as dirty. + /// Marks this and all `ChangeDetectionStrategy.onPush` ancestors as dirty. /// - /// Components that use `changeDetection: ChangeDetectionStrategy.OnPush` are + /// Components that use `changeDetection: ChangeDetectionStrategy.onPush` are /// only checked once (after creation), and are no longer considered "dirty" /// until either: /// @@ -28,7 +28,7 @@ abstract class ChangeDetectorRef { /// @Component( /// selector: 'on-push-example', /// template: 'Number of ticks: {{ticks}}", - /// changeDetection: ChangeDetectionStrategy.OnPush, + /// changeDetection: ChangeDetectionStrategy.onPush, /// ) /// class OnPushExample implements OnDestroy { /// Timer timer; @@ -70,13 +70,13 @@ abstract class ChangeDetectorRef { /// On any other argument, this method is still safe to call, but has no /// effect. This allows the caller to use this method without explicit /// knowledge of whether or not [child] is backed by a component using - /// `ChangeDetectionStrategy.OnPush`. + /// `ChangeDetectionStrategy.onPush`. /// /// ``` /// @Component( /// selector: 'example', /// template: '', - /// changeDetection: ChangeDetectionStrategy.OnPush, + /// changeDetection: ChangeDetectionStrategy.onPush, /// ) /// class ExampleComponent { /// ExampleComponent(this._changeDetectorRef); @@ -100,16 +100,16 @@ abstract class ChangeDetectorRef { /// /// Prefer propagating updates to children through the template over this /// method when possible. This method is intended as a last resort to - /// facilitate migrating components to use `ChangeDetectionStrategy.OnPush`. + /// facilitate migrating components to use `ChangeDetectionStrategy.onPush`. void markChildForCheck(Object child); /// See [DeprecatedChangeDetectorRef.detach] for details. - @Deprecated('Use "changeDetection: ChangeDetectionStrategy.OnPush" instead') + @Deprecated('Use "changeDetection: ChangeDetectionStrategy.onPush" instead') @protected void detachDeprecated(); /// See [DeprecatedChangeDetectorRef.reattach] for details. - @Deprecated('Use "changeDetection: ChangeDetectionStrategy.OnPush" instead') + @Deprecated('Use "changeDetection: ChangeDetectionStrategy.onPush" instead') @protected void reattachDeprecated(); @@ -131,14 +131,14 @@ extension DeprecatedChangeDetectorRef on ChangeDetectorRef { /// on) are still called if the component has been detached. We may consider /// changing this behavior in the future: b/129780288. /// - /// In most cases simply using `ChangeDetectionStrategy.OnPush` and calling + /// In most cases simply using `ChangeDetectionStrategy.onPush` and calling /// [markForCheck] is preferred as it provides the same contract around not /// checking a component until it is dirtied. /// /// **WARNING**: This API should be considered rather _rare_. Strongly /// consider reaching out if you have a bug or performance issue that leads - /// to using [detach] over `ChangeDetectionStrategy.OnPush` / [markForCheck]. - @Deprecated('Use "changeDetection: ChangeDetectionStrategy.OnPush" instead') + /// to using [detach] over `ChangeDetectionStrategy.onPush` / [markForCheck]. + @Deprecated('Use "changeDetection: ChangeDetectionStrategy.onPush" instead') void detach() { detachDeprecated(); } @@ -149,7 +149,7 @@ extension DeprecatedChangeDetectorRef on ChangeDetectorRef { /// will be checked for changes during the next change detection run. See the /// docs around [detach] for details of how detaching works and why this /// method invocation should be rare. - @Deprecated('Use "changeDetection: ChangeDetectionStrategy.OnPush" instead') + @Deprecated('Use "changeDetection: ChangeDetectionStrategy.onPush" instead') void reattach() { reattachDeprecated(); } @@ -165,7 +165,7 @@ extension DeprecatedDetectChanges on ChangeDetectorRef { /// Try instead: /// /// * Simply removing it, and seeing if it breaks your app. - /// * Using `ChangeDetectionStrategy.OnPush` and [markForCheck] instead. + /// * Using `ChangeDetectionStrategy.onPush` and [markForCheck] instead. /// /// If all else fails, it is strongly preferable to use our explicit API for /// forcing more change detection, `NgZone.runAfterChangesObserved`. It is diff --git a/ngdart/lib/src/core/linker/app_view_utils.dart b/ngdart/lib/src/core/linker/app_view_utils.dart index e485c4e1f9..4258326ea5 100644 --- a/ngdart/lib/src/core/linker/app_view_utils.dart +++ b/ngdart/lib/src/core/linker/app_view_utils.dart @@ -1,6 +1,6 @@ import 'dart:html' show DocumentFragment, NodeTreeSanitizer; -import 'package:ngdart/src/core/application_tokens.dart' show APP_ID; +import 'package:ngdart/src/core/application_tokens.dart' as tokens show appId; import 'package:ngdart/src/runtime/dom_events.dart' show EventManager; /// Application wide view utilities. @@ -13,7 +13,7 @@ class AppViewUtils { final EventManager eventManager; AppViewUtils( - @APP_ID this.appId, + @tokens.appId this.appId, this.eventManager, ); } diff --git a/ngdart/lib/src/core/linker/component_factory.dart b/ngdart/lib/src/core/linker/component_factory.dart index d9c9fb30e8..df43db9f59 100644 --- a/ngdart/lib/src/core/linker/component_factory.dart +++ b/ngdart/lib/src/core/linker/component_factory.dart @@ -10,7 +10,7 @@ import 'package:ngdart/src/utilities.dart'; import 'view_ref.dart' show ViewRef; import 'views/host_view.dart'; -/// Returns whether [componentRef] uses [ChangeDetectionStrategy.Default]. +/// Returns whether [componentRef] uses [ChangeDetectionStrategy.checkAlways]. /// /// In practice this can be used to assert that a component does *not* use /// default change detection in non-default or performance sensitive contexts. diff --git a/ngdart/lib/src/core/linker/views/component_view.dart b/ngdart/lib/src/core/linker/views/component_view.dart index 75e21e890b..bd9d050643 100644 --- a/ngdart/lib/src/core/linker/views/component_view.dart +++ b/ngdart/lib/src/core/linker/views/component_view.dart @@ -34,12 +34,8 @@ abstract class ComponentView extends RenderView { ComponentView( View parentView, int parentIndex, - int changeDetectionMode, - ) : _data = _ComponentViewData( - parentView, - parentIndex, - changeDetectionMode, - ); + ChangeDetectionCheckedState changeDetectionMode, + ) : _data = _ComponentViewData(parentView, parentIndex, changeDetectionMode); @override late final T ctx; @@ -70,7 +66,7 @@ abstract class ComponentView extends RenderView { /// be referenced by any other means. @experimental bool get usesDefaultChangeDetection => - _data.changeDetectionMode == ChangeDetectionStrategy.CheckAlways; + _data.changeDetectionMode == ChangeDetectionCheckedState.checkAlways; // Initialization ------------------------------------------------------------ @@ -139,7 +135,8 @@ abstract class ComponentView extends RenderView { @override void detectChangesDeprecated() { if (_data.shouldSkipChangeDetection) { - if (_data.changeDetectionMode == ChangeDetectionStrategy.Checked) { + if (_data.changeDetectionMode == + ChangeDetectionCheckedState.waitingForMarkForCheck) { detectChangesInCheckAlwaysViews(); } return; @@ -159,8 +156,9 @@ abstract class ComponentView extends RenderView { } // If we are a 'CheckOnce' component, we are done being checked. - if (_data.changeDetectionMode == ChangeDetectionStrategy.CheckOnce) { - _data.changeDetectionMode = ChangeDetectionStrategy.Checked; + if (_data.changeDetectionMode == ChangeDetectionCheckedState.checkOnce) { + _data.changeDetectionMode = + ChangeDetectionCheckedState.waitingForMarkForCheck; } // Set the state to already checked at least once. @@ -185,14 +183,18 @@ abstract class ComponentView extends RenderView { /// serves to propagate input changes down the component tree during a single /// change detection pass. void markAsCheckOnce() { - _data.changeDetectionMode = ChangeDetectionStrategy.CheckOnce; + _data.changeDetectionMode = ChangeDetectionCheckedState.checkOnce; } @override void markForCheck() { final changeDetectionMode = _data.changeDetectionMode; - if (changeDetectionMode == ChangeDetectionStrategy.Detached) return; - if (changeDetectionMode == ChangeDetectionStrategy.Checked) { + if (changeDetectionMode == + ChangeDetectionCheckedState.waitingToBeAttached) { + return; + } + if (changeDetectionMode == + ChangeDetectionCheckedState.waitingForMarkForCheck) { markAsCheckOnce(); } parentView!.markForCheck(); @@ -200,12 +202,12 @@ abstract class ComponentView extends RenderView { @override void detachDeprecated() { - _data.changeDetectionMode = ChangeDetectionStrategy.Detached; + _data.changeDetectionMode = ChangeDetectionCheckedState.waitingToBeAttached; } @override void reattachDeprecated() { - _data.changeDetectionMode = ChangeDetectionStrategy.CheckAlways; + _data.changeDetectionMode = ChangeDetectionCheckedState.checkAlways; markForCheck(); } @@ -247,7 +249,7 @@ class _ComponentViewData implements RenderViewData { factory _ComponentViewData( View parentView, int parentIndex, - int changeDetectionMode, + ChangeDetectionCheckedState changeDetectionMode, ) { return _ComponentViewData._(parentView, parentIndex, changeDetectionMode); } @@ -271,9 +273,9 @@ class _ComponentViewData implements RenderViewData { List>? subscriptions; @override - int get changeDetectionMode => _changeDetectionMode; - int _changeDetectionMode; - set changeDetectionMode(int mode) { + ChangeDetectionCheckedState get changeDetectionMode => _changeDetectionMode; + ChangeDetectionCheckedState _changeDetectionMode; + set changeDetectionMode(ChangeDetectionCheckedState mode) { if (_changeDetectionMode != mode) { _changeDetectionMode = mode; _updateShouldSkipChangeDetection(); @@ -310,9 +312,10 @@ class _ComponentViewData implements RenderViewData { } void _updateShouldSkipChangeDetection() { - _shouldSkipChangeDetection = - _changeDetectionMode == ChangeDetectionStrategy.Checked || - _changeDetectionMode == ChangeDetectionStrategy.Detached || - _changeDetectorState == ChangeDetectorState.errored; + _shouldSkipChangeDetection = _changeDetectionMode == + ChangeDetectionCheckedState.waitingForMarkForCheck || + _changeDetectionMode == + ChangeDetectionCheckedState.waitingToBeAttached || + _changeDetectorState == ChangeDetectorState.errored; } } diff --git a/ngdart/lib/src/core/linker/views/embedded_view.dart b/ngdart/lib/src/core/linker/views/embedded_view.dart index 550407dfd6..7809947be3 100644 --- a/ngdart/lib/src/core/linker/views/embedded_view.dart +++ b/ngdart/lib/src/core/linker/views/embedded_view.dart @@ -159,19 +159,20 @@ abstract class EmbeddedView extends RenderView @override void markForCheck() { // TODO(b/129780288): remove check for whether this view is detached. - if (_data.changeDetectionMode != ChangeDetectionStrategy.Detached) { + if (_data.changeDetectionMode != + ChangeDetectionCheckedState.waitingToBeAttached) { _data.viewContainer?.parentView?.markForCheck(); } } @override void detachDeprecated() { - _data.changeDetectionMode = ChangeDetectionStrategy.Detached; + _data.changeDetectionMode = ChangeDetectionCheckedState.waitingToBeAttached; } @override void reattachDeprecated() { - _data.changeDetectionMode = ChangeDetectionStrategy.CheckAlways; + _data.changeDetectionMode = ChangeDetectionCheckedState.checkAlways; markForCheck(); } @@ -264,9 +265,10 @@ class _EmbeddedViewData implements DynamicViewData, RenderViewData { List? _onDestroyCallbacks; @override - int get changeDetectionMode => _changeDetectionMode; - int _changeDetectionMode = ChangeDetectionStrategy.CheckAlways; - set changeDetectionMode(int mode) { + ChangeDetectionCheckedState get changeDetectionMode => _changeDetectionMode; + ChangeDetectionCheckedState _changeDetectionMode = + ChangeDetectionCheckedState.checkAlways; + set changeDetectionMode(ChangeDetectionCheckedState mode) { if (_changeDetectionMode != mode) { _changeDetectionMode = mode; _updateShouldSkipChangeDetection(); @@ -314,8 +316,8 @@ class _EmbeddedViewData implements DynamicViewData, RenderViewData { } void _updateShouldSkipChangeDetection() { - _shouldSkipChangeDetection = - _changeDetectionMode == ChangeDetectionStrategy.Detached || - _changeDetectorState == ChangeDetectorState.errored; + _shouldSkipChangeDetection = _changeDetectionMode == + ChangeDetectionCheckedState.waitingToBeAttached || + _changeDetectorState == ChangeDetectorState.errored; } } diff --git a/ngdart/lib/src/core/linker/views/host_view.dart b/ngdart/lib/src/core/linker/views/host_view.dart index 3b8475ed8d..5f481ceb5a 100644 --- a/ngdart/lib/src/core/linker/views/host_view.dart +++ b/ngdart/lib/src/core/linker/views/host_view.dart @@ -190,19 +190,20 @@ abstract class HostView extends View implements DynamicView { @override void markForCheck() { // TODO(b/129780288): remove check for whether this view is detached. - if (_data.changeDetectionMode != ChangeDetectionStrategy.Detached) { + if (_data.changeDetectionMode != + ChangeDetectionCheckedState.waitingToBeAttached) { _data.viewContainer?.parentView?.markForCheck(); } } @override void detachDeprecated() { - _data.changeDetectionMode = ChangeDetectionStrategy.Detached; + _data.changeDetectionMode = ChangeDetectionCheckedState.waitingToBeAttached; } @override void reattachDeprecated() { - _data.changeDetectionMode = ChangeDetectionStrategy.CheckAlways; + _data.changeDetectionMode = ChangeDetectionCheckedState.checkAlways; markForCheck(); } @@ -257,9 +258,10 @@ class _HostViewData implements DynamicViewData { List? _onDestroyCallbacks; @override - int get changeDetectionMode => _changeDetectionMode; - int _changeDetectionMode = ChangeDetectionStrategy.CheckAlways; - set changeDetectionMode(int mode) { + ChangeDetectionCheckedState get changeDetectionMode => _changeDetectionMode; + ChangeDetectionCheckedState _changeDetectionMode = + ChangeDetectionCheckedState.checkAlways; + set changeDetectionMode(ChangeDetectionCheckedState mode) { if (_changeDetectionMode != mode) { _changeDetectionMode = mode; _updateShouldSkipChangeDetection(); @@ -302,8 +304,8 @@ class _HostViewData implements DynamicViewData { } void _updateShouldSkipChangeDetection() { - _shouldSkipChangeDetection = - _changeDetectionMode == ChangeDetectionStrategy.Detached || - _changeDetectorState == ChangeDetectorState.errored; + _shouldSkipChangeDetection = _changeDetectionMode == + ChangeDetectionCheckedState.waitingToBeAttached || + _changeDetectorState == ChangeDetectorState.errored; } } diff --git a/ngdart/lib/src/core/linker/views/view.dart b/ngdart/lib/src/core/linker/views/view.dart index 682edb4614..b5898426d8 100644 --- a/ngdart/lib/src/core/linker/views/view.dart +++ b/ngdart/lib/src/core/linker/views/view.dart @@ -6,6 +6,7 @@ import 'package:ngdart/src/core/change_detection/change_detection.dart'; import 'package:ngdart/src/core/change_detection/host.dart'; import 'package:ngdart/src/di/errors.dart'; import 'package:ngdart/src/di/injector.dart'; +import 'package:ngdart/src/meta.dart'; import 'package:ngdart/src/utilities.dart'; /// The base implementation of all views. @@ -246,7 +247,7 @@ abstract class View implements ChangeDetectorRef { abstract class ViewData { /// Tracks this view's [ChangeDetectionStrategy]. // TODO(b/132122866): host and embedded views only need a detached bit. - int get changeDetectionMode; + ChangeDetectionCheckedState get changeDetectionMode; /// Tracks this view's [ChangeDetectorState]. // TODO(b/132122866): host views only need an error bit. diff --git a/ngdart/lib/src/meta/change_detection_constants.dart b/ngdart/lib/src/meta/change_detection_constants.dart index 58bc298d01..bfcbd46355 100644 --- a/ngdart/lib/src/meta/change_detection_constants.dart +++ b/ngdart/lib/src/meta/change_detection_constants.dart @@ -19,29 +19,17 @@ enum ChangeDetectorState { /// time change detection is triggered. /// /// ! Changes to this class require updates to view_compiler/constants.dart. -class ChangeDetectionStrategy { +enum ChangeDetectionStrategy { /// The default type of change detection, always checking for changes. /// /// When an asynchronous event (such as user interaction or an RPC) occurs /// within the app, the root component of the app is checked for changes, /// and then all children in a depth-first search. - static const Default = 0; - - @Deprecated('Not intended to be a public API. Use "OnPush"') - static const CheckOnce = ChangeDetectionCheckedState.checkOnce; - - @Deprecated('Not intended to be a public API. Use "OnPush"') - static const Checked = ChangeDetectionCheckedState.waitingForMarkForCheck; - - @Deprecated('Not intended to be a public API. Use "Default"') - static const CheckAlways = ChangeDetectionCheckedState.checkAlways; - - @Deprecated('Not intended to be a public API. Use "ChangeDetectorRef.detach"') - static const Detached = ChangeDetectionCheckedState.waitingToBeAttached; + checkAlways, /// An optimized form of change detection, skipping some checks for changes. /// - /// Unlike [Default], [OnPush] waits for the following signals to check a + /// Unlike [checkAlways], [onPush] waits for the following signals to check a /// component: /// * An `@Input()` on the component being changed. /// * An `@Output()` or event listener (i.e. `(click)="..."`) being executed @@ -50,47 +38,36 @@ class ChangeDetectionStrategy { /// descendant. /// /// Otherwise, change detection is skipped for this component and its - /// descendants. An [OnPush] configured component as a result can afford to be + /// descendants. An [onPush] configured component as a result can afford to be /// a bit less defensive about caching the result of bindings, for example. /// - /// **WARNING**: It is currently _undefined behavior_ to have a [Default] + /// **WARNING**: It is currently _undefined behavior_ to have a [checkAlways] /// configured component as a child (or directive) of a component that is /// using [OnPush]. We hope to introduce more guidance here in the future. - static const OnPush = 5; - - static String toPrettyString(int strategy) { - switch (strategy) { - case Default: - return 'Default'; - case OnPush: - return 'OnPush'; - default: - return 'Internal'; - } - } + onPush, } /// **TRANSITIONAL**: These are runtime internal states to the `AppView`. /// /// TODO(b/128441899): Refactor into a change detection state machine. -class ChangeDetectionCheckedState { +enum ChangeDetectionCheckedState { /// `AppView.detectChanges` should be invoked once. /// /// The next state is [waitingForMarkForCheck]. - static const checkOnce = 1; + checkOnce, /// `AppView.detectChanges` should bail out. /// /// Upon use of `AppView.markForCheck`, the next state is [checkOnce]. - static const waitingForMarkForCheck = 2; + waitingForMarkForCheck, /// `AppView.detectChanges` should always be invoked. - static const checkAlways = 3; + checkAlways, /// `AppView.detectChanges` should bail out. /// /// Attaching a view should transition to either [checkOnce] or [checkAlways] /// depending on whether `OnPush` or `Default` change detection strategies are /// configured for the view. - static const waitingToBeAttached = 4; + waitingToBeAttached, } diff --git a/ngdart/lib/src/meta/change_detection_link.dart b/ngdart/lib/src/meta/change_detection_link.dart index 91a5e91cb3..a9588b9180 100644 --- a/ngdart/lib/src/meta/change_detection_link.dart +++ b/ngdart/lib/src/meta/change_detection_link.dart @@ -28,7 +28,7 @@ import 'package:meta/meta_meta.dart'; /// @Component( /// selector: 'example', /// template: '', -/// changeDetection: ChangeDetectionStrategy.OnPush, +/// changeDetection: ChangeDetectionStrategy.onPush, /// ) /// class ExampleComponent { /// @Input() diff --git a/ngdart/lib/src/meta/directives.dart b/ngdart/lib/src/meta/directives.dart index 5d9eb2608c..649af22409 100644 --- a/ngdart/lib/src/meta/directives.dart +++ b/ngdart/lib/src/meta/directives.dart @@ -156,7 +156,7 @@ class Component extends Directive { /// /// The [changeDetection] property defines, whether the change detection will /// be checked every time or only when the component tells it to do so. - final int changeDetection; + final ChangeDetectionStrategy changeDetection; /// Defines the set of injectable objects that are visible to its view /// DOM children. @@ -237,7 +237,7 @@ class Component extends Directive { super.visibility = Visibility.local, this.viewProviders = const [], this.exports = const [], - this.changeDetection = ChangeDetectionStrategy.Default, + this.changeDetection = ChangeDetectionStrategy.checkAlways, this.templateUrl, this.template, this.preserveWhitespace = false, diff --git a/ngforms/test/ng_form_test.dart b/ngforms/test/ng_form_test.dart index ed1c8c831a..f965164e32 100644 --- a/ngforms/test/ng_form_test.dart +++ b/ngforms/test/ng_form_test.dart @@ -193,7 +193,7 @@ class NgFormTest { ''', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class OnPushControlTest { var requiresName = false; @@ -218,7 +218,7 @@ class OnPushControlTest { ''', - changeDetection: ChangeDetectionStrategy.OnPush, + changeDetection: ChangeDetectionStrategy.onPush, ) class OnPushControlGroupTest { var requiresGroup = false;