Skip to content

Commit 66cb2f6

Browse files
author
Ben Grynhaus
committed
Throttle the ngZone subscribe calls
1 parent ee11f92 commit 66cb2f6

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

libs/core/src/lib/renderer/react-template.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import { EmbeddedViewRef, NgZone, TemplateRef } from '@angular/core';
55
import * as React from 'react';
66
import * as ReactDOM from 'react-dom';
77
import { Subscription } from 'rxjs';
8+
import { throttleTime } from 'rxjs/operators';
89

910
const DEBUG = false;
11+
const TEMPLATE_DETECT_CHANGES_THROTTLE_MS = 250;
1012

1113
/**
1214
* @internal
@@ -79,11 +81,16 @@ export class ReactTemplate<TContext extends object | void> extends React.Compone
7981

8082
this._embeddedViewRef.rootNodes.forEach(child => hostElement.appendChild(child));
8183

82-
// Detect the first cycle's changes, and then subscribe for subsequent ones
84+
// Detect the first cycle's changes, and then subscribe for subsequent ones.
8385
this._embeddedViewRef.detectChanges();
84-
this._ngZoneSubscription = ngZone.onUnstable.subscribe(() => {
85-
this._embeddedViewRef.detectChanges();
86-
});
86+
87+
// Throttling the detect changes to an empirically selected value so we don't overload too much work.
88+
// TODO: This needs some better solution to listen to changes to the binding sources of the template.
89+
this._ngZoneSubscription = ngZone.onUnstable
90+
.pipe(throttleTime(TEMPLATE_DETECT_CHANGES_THROTTLE_MS))
91+
.subscribe(() => {
92+
this._embeddedViewRef.markForCheck();
93+
});
8794
}
8895

8996
componentWillUnmount() {

0 commit comments

Comments
 (0)