Skip to content

Commit aa8bba4

Browse files
tboschmhevery
authored andcommitted
fix(core): allow to detach OnPush components (#16394)
Fixes #9720
1 parent 392d584 commit aa8bba4

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

packages/core/src/view/refs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ export class ViewRef_ implements EmbeddedViewRef<any>, InternalViewRef {
236236
get destroyed(): boolean { return (this._view.state & ViewState.Destroyed) !== 0; }
237237

238238
markForCheck(): void { markParentViewsForCheck(this._view); }
239-
detach(): void { this._view.state &= ~ViewState.ChecksEnabled; }
239+
detach(): void { this._view.state &= ~ViewState.Attached; }
240240
detectChanges(): void { Services.checkAndUpdateView(this._view); }
241241
checkNoChanges(): void { Services.checkNoChangesView(this._view); }
242242

243-
reattach(): void { this._view.state |= ViewState.ChecksEnabled; }
243+
reattach(): void { this._view.state |= ViewState.Attached; }
244244
onDestroy(callback: Function) {
245245
if (!this._view.disposables) {
246246
this._view.disposables = [];

packages/core/src/view/types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,12 @@ export interface ViewData {
324324
*/
325325
export const enum ViewState {
326326
FirstCheck = 1 << 0,
327-
ChecksEnabled = 1 << 1,
328-
Errored = 1 << 2,
329-
Destroyed = 1 << 3
327+
Attached = 1 << 1,
328+
ChecksEnabled = 1 << 2,
329+
Errored = 1 << 3,
330+
Destroyed = 1 << 4,
331+
332+
CatDetectChanges = Attached | ChecksEnabled,
330333
}
331334

332335
export interface DisposableFn { (): void; }

packages/core/src/view/view.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function createView(
211211
viewContainerParent: null, parentNodeDef,
212212
context: null,
213213
component: null, nodes,
214-
state: ViewState.FirstCheck | ViewState.ChecksEnabled, root, renderer,
214+
state: ViewState.FirstCheck | ViewState.CatDetectChanges, root, renderer,
215215
oldValues: new Array(def.bindingCount), disposables
216216
};
217217
return view;
@@ -542,13 +542,13 @@ function callViewAction(view: ViewData, action: ViewAction) {
542542
const viewState = view.state;
543543
switch (action) {
544544
case ViewAction.CheckNoChanges:
545-
if ((viewState & ViewState.ChecksEnabled) &&
545+
if ((viewState & ViewState.CatDetectChanges) === ViewState.CatDetectChanges &&
546546
(viewState & (ViewState.Errored | ViewState.Destroyed)) === 0) {
547547
checkNoChangesView(view);
548548
}
549549
break;
550550
case ViewAction.CheckAndUpdate:
551-
if ((viewState & ViewState.ChecksEnabled) &&
551+
if ((viewState & ViewState.CatDetectChanges) === ViewState.CatDetectChanges &&
552552
(viewState & (ViewState.Errored | ViewState.Destroyed)) === 0) {
553553
checkAndUpdateView(view);
554554
}

packages/core/test/linker/change_detection_integration_spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,21 @@ export function main() {
11751175
expect(renderLog.log).toEqual([]);
11761176
}));
11771177

1178+
it('Detached should disable OnPush', fakeAsync(() => {
1179+
const ctx = createCompFixture('<push-cmp [value]="value"></push-cmp>');
1180+
ctx.componentInstance.value = 0;
1181+
ctx.detectChanges();
1182+
renderLog.clear();
1183+
1184+
const cmp: CompWithRef = queryDirs(ctx.debugElement, PushComp)[0];
1185+
cmp.changeDetectorRef.detach();
1186+
1187+
ctx.componentInstance.value = 1;
1188+
ctx.detectChanges();
1189+
1190+
expect(renderLog.log).toEqual([]);
1191+
}));
1192+
11781193
it('Detached view can be checked locally', fakeAsync(() => {
11791194
const ctx = createCompFixture('<wrap-comp-with-ref></wrap-comp-with-ref>');
11801195
const cmp: CompWithRef = queryDirs(ctx.debugElement, CompWithRef)[0];
@@ -1225,7 +1240,6 @@ export function main() {
12251240

12261241
ctx.detectChanges();
12271242
expect(cmp.renderCount).toBe(count);
1228-
12291243
}));
12301244

12311245
});

0 commit comments

Comments
 (0)