Skip to content

Commit

Permalink
fix(core): store original markForCheck fn to prevent double patch
Browse files Browse the repository at this point in the history
  • Loading branch information
trotyl committed Apr 2, 2018
1 parent b3c9d54 commit 7da0ee0
Showing 1 changed file with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApplicationRef, ChangeDetectorRef, Component, Injectable, NgZone } from '@angular/core';

const SCHEDULER_PATCHED = new WeakMap<ChangeDetectorRef, ApplicationRef>();
const ORIGINAL_MARK_FOR_CHECK = '__markForCheck__';
let tickScheduled = false;

export abstract class ChangeDetectionScheduler {
Expand All @@ -25,22 +26,19 @@ export class ChangeDetectionScheduler_ implements ChangeDetectionScheduler {
})
export class ChangeDetectionSchedulerInitializer {
constructor(appRef: ApplicationRef, cdRef: ChangeDetectorRef, ngZone: NgZone, scheduler: ChangeDetectionScheduler) {
const markForCheck = cdRef.markForCheck;

const scheduleTickIfNeeded = () => {
if (!(ngZone instanceof NgZone) && !tickScheduled) {
scheduler.schedule();
}
};

while (typeof cdRef === 'object' && !cdRef.hasOwnProperty('markForCheck') && cdRef.constructor.prototype) {
cdRef = cdRef.constructor.prototype;
}

if (!SCHEDULER_PATCHED.has(cdRef) || SCHEDULER_PATCHED.get(cdRef) !== appRef) {
if ((cdRef as any)[ORIGINAL_MARK_FOR_CHECK] == null) {
(cdRef as any)[ORIGINAL_MARK_FOR_CHECK] = cdRef.markForCheck;
}
cdRef.markForCheck = function () {
markForCheck.call(this);
scheduleTickIfNeeded();
(cdRef as any)[ORIGINAL_MARK_FOR_CHECK].call(this);
if (!(ngZone instanceof NgZone) && !tickScheduled) {
scheduler.schedule();
}
};

SCHEDULER_PATCHED.set(cdRef, appRef);
Expand Down

0 comments on commit 7da0ee0

Please sign in to comment.