Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe68f6f

Browse files
committed
feat(cdk): add native postTask scheduling support
1 parent b7dc736 commit fe68f6f

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

libs/cdk/render-strategies/src/lib/concurrent-strategies.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { NgZone } from '@angular/core';
22
import { MonoTypeOperatorFunction, Observable } from 'rxjs';
33
import { filter, mapTo, switchMap } from 'rxjs/operators';
44
import {
5+
cancelCallback,
6+
scheduleCallback,
57
unstable_cancelCallback,
68
unstable_scheduleCallback,
79
PriorityLevel,
@@ -89,6 +91,21 @@ const idleStrategy: RxStrategyCredentials = {
8991
},
9092
};
9193

94+
const postTaskUserVisibleStrategy: RxStrategyCredentials = {
95+
name: 'userVisible',
96+
work: (cdRef) => cdRef.detectChanges(),
97+
behavior: ({ work, scope, ngZone }) => {
98+
return (o$) =>
99+
o$.pipe(
100+
scheduleOnPostTaskQueue(work, {
101+
ngZone,
102+
priority: PriorityLevel.IdlePriority,
103+
scope,
104+
})
105+
);
106+
},
107+
};
108+
92109
function scheduleOnQueue<T>(
93110
work: (...args: any[]) => void,
94111
options: {
@@ -97,6 +114,39 @@ function scheduleOnQueue<T>(
97114
delay?: number;
98115
ngZone: NgZone;
99116
}
117+
): MonoTypeOperatorFunction<T> {
118+
const scope = (options.scope as Record<string, unknown>) || {};
119+
return (o$: Observable<T>): Observable<T> =>
120+
o$.pipe(
121+
filter(() => !coalescingManager.isCoalescing(scope)),
122+
switchMap((v) =>
123+
new Observable<T>((subscriber) => {
124+
coalescingManager.add(scope);
125+
const task = scheduleCallback(
126+
options.priority,
127+
() => {
128+
work();
129+
coalescingManager.remove(scope);
130+
subscriber.next(v);
131+
},
132+
{ delay: options.delay }
133+
);
134+
return () => {
135+
coalescingManager.remove(scope);
136+
cancelCallback(task);
137+
};
138+
}).pipe(mapTo(v))
139+
)
140+
);
141+
}
142+
function scheduleOnPostTaskQueue<T>(
143+
work: (...args: any[]) => void,
144+
options: {
145+
priority: PriorityLevel;
146+
scope: coalescingObj;
147+
delay?: number;
148+
ngZone: NgZone;
149+
}
100150
): MonoTypeOperatorFunction<T> {
101151
const scope = (options.scope as Record<string, unknown>) || {};
102152
return (o$: Observable<T>): Observable<T> =>
@@ -131,4 +181,5 @@ export const RX_CONCURRENT_STRATEGIES: RxConcurrentStrategies = {
131181
normal: normalStrategy,
132182
low: lowStrategy,
133183
idle: idleStrategy,
184+
userVisible: postTaskUserVisibleStrategy,
134185
};

libs/cdk/render-strategies/src/lib/model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export type RxConcurrentStrategyNames =
3636
| 'userBlocking'
3737
| 'normal'
3838
| 'low'
39-
| 'idle';
39+
| 'idle'
40+
| 'userVisible';
4041
export type RxDefaultStrategyNames =
4142
| RxNativeStrategyNames
4243
| RxConcurrentStrategyNames;

0 commit comments

Comments
 (0)