-
-
Notifications
You must be signed in to change notification settings - Fork 205
feat(cdk): add rxScheduleTask function #1344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c262165
feat(cdk): add rxScheduleTask function
Karnaukhov-kh 15762a4
fix(cdk): improve usage examples of rxShceduleTask
Karnaukhov-kh e1b8629
fix(cdk): fix rxScheduleTask imports
Karnaukhov-kh c6bc9fa
fix(cdk): fix rxScheduleTask readme
Karnaukhov-kh fcd71c2
fix(cdk): correct callback in rxScheduleTask docs
Karnaukhov-kh ef416e6
Update libs/cdk/render-strategies/docs/rx-schedule-task.md
Karnaukhov-kh 604ff8a
refactor(cdk): provide strategy in options
Karnaukhov-kh 9ff6d9d
fix(cdk): proper options passing in rxScheduleTask
Karnaukhov-kh 4aa53ee
Merge branch 'main' into introduce-rx-schedule-task
BioPhoton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# rxScheduleTask | ||
|
||
`rxScheduleTask` provides a helper function to schedule function execution. It is a minimal building block for making performance optimizations in your code. | ||
|
||
## Motivation | ||
|
||
Chromium based browsers considers all tasks that taking more than 50ms as long tasks. If task runs more than 50ms, users will start noticing lags. Optimally all user interactions should happen at 30 fps framerate with 32ms budget per browser task. In ideal world it should be 60 fps and 16ms budget. | ||
|
||
> 💡 In reality browser has a reserved overhead of 4ms, try to stick to 28ms of work for 30 fps and 12ms for 60 fps. | ||
|
||
## Scheduling mechanisms in browser | ||
|
||
Most common ways of delaying task execution are: | ||
|
||
- `setTimeout` | ||
- `requestAnimationFrame` | ||
- `requestIdleCallback` | ||
|
||
`rxScheduleTask` provides similar API but comes with huge benefits of notion of frame budget and priority configuration. | ||
|
||
## Concurrent strategies | ||
|
||
> 💡 Under the hood all our concurrent strategies are based on MessageChannel technology. | ||
|
||
To address the problem of long tasks and help browser split the work @rx-angular/cdk provides concurrent strategies. This strategies will help browser to chunk the work into non-blocking tasks whenever it's possible. | ||
|
||
You can read detailed information about concurrent strategies [here](https://github.com/rx-angular/rx-angular/blob/main/libs/cdk/render-strategies/docs/concurrent-strategies.md). | ||
|
||
## Usage examples | ||
|
||
### Input params | ||
|
||
- Just as common delaying apis this method `accepts` a work function that should be scheduled. | ||
- Second important parameter is `strategy` which will be used for scheduling (`normal` is default). | ||
- Third argument is `options` that can hold `delay` for the task and `ngZone` where task should run. | ||
|
||
### Return type | ||
|
||
Function returns a callback that you can use to cancel already scheduled tasks. | ||
|
||
### Default usage | ||
|
||
```typescript | ||
import { rxScheduleTask } from '@rx-angular/cdk/render-strategies'; | ||
... | ||
|
||
function updateStateAndBackup<T>(data: T) { | ||
this.stateService.set(data); | ||
|
||
rxScheduleTask(() => localStorage.setItem('state', JSON.stringify(state))); | ||
} | ||
``` | ||
|
||
### Usage with non-default strategy | ||
|
||
```typescript | ||
import { rxScheduleTask } from '@rx-angular/cdk/render-strategies'; | ||
... | ||
|
||
function updateStateAndBackup<T>(data: T) { | ||
this.stateService.set(data); | ||
|
||
rxScheduleTask( | ||
() => localStorage.setItem('state', JSON.stringify(state)), | ||
{strategy: 'idle'} | ||
); | ||
} | ||
``` | ||
|
||
### Usage with options | ||
|
||
```typescript | ||
import { rxScheduleTask } from '@rx-angular/cdk/render-strategies'; | ||
... | ||
|
||
function updateStateAndBackup<T>(data: T) { | ||
this.stateService.set(data); | ||
|
||
rxScheduleTask( | ||
() => localStorage.setItem('state', JSON.stringify(state)), | ||
{ delay: 200, zone: this.ngZone, strategy: 'idle' } | ||
); | ||
} | ||
``` | ||
|
||
### Cancel scheduled task | ||
|
||
```typescript | ||
import { rxScheduleTask } from '@rx-angular/cdk/render-strategies'; | ||
... | ||
|
||
let saveToLocalStorageCallback; | ||
|
||
function updateStateAndBackup<T>(data: T) { | ||
this.stateService.set(data); | ||
|
||
if (saveToLocalStorageCallback) { | ||
saveToLocalStorageCallback(); | ||
} | ||
|
||
saveToLocalStorageCallback = rxScheduleTask(() => | ||
localStorage.setItem('state', JSON.stringify(state)) | ||
); | ||
} | ||
``` | ||
|
||
## Links | ||
|
||
- [Detailed information about strategies](https://github.com/rx-angular/rx-angular/tree/master/libs/cdk/render-strategies) | ||
- [MessageChannel documentation](https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { NgZone } from '@angular/core'; | ||
import { | ||
cancelCallback, | ||
scheduleCallback, | ||
PriorityLevel, | ||
} from '@rx-angular/cdk/internals/scheduler'; | ||
import { RxConcurrentStrategyNames } from './model'; | ||
|
||
type StrategiesPriorityRecord = Record< | ||
RxConcurrentStrategyNames, | ||
PriorityLevel | ||
>; | ||
|
||
const strategiesPrio: StrategiesPriorityRecord = { | ||
immediate: PriorityLevel.ImmediatePriority, | ||
userBlocking: PriorityLevel.UserBlockingPriority, | ||
normal: PriorityLevel.NormalPriority, | ||
low: PriorityLevel.LowPriority, | ||
idle: PriorityLevel.IdlePriority, | ||
}; | ||
|
||
const defaultStrategy: keyof StrategiesPriorityRecord = 'normal'; | ||
|
||
/** | ||
* @description | ||
* This function is used to schedule a task with a certain priority. | ||
* It is useful for tasks that can be done asynchronously. | ||
* | ||
* ```ts | ||
* const task = rxScheduleTask(() => localStorage.setItem(state, JSON.stringify(state)); | ||
* ``` | ||
*/ | ||
export const rxScheduleTask = ( | ||
work: (...args: any[]) => void, | ||
options?: { | ||
strategy?: keyof StrategiesPriorityRecord; | ||
delay?: number; | ||
ngZone?: NgZone; | ||
} | ||
) => { | ||
const task = scheduleCallback( | ||
strategiesPrio[options?.strategy || defaultStrategy], | ||
() => work(), | ||
{ | ||
delay: options?.delay, | ||
ngZone: options?.ngZone, | ||
} | ||
); | ||
return () => { | ||
cancelCallback(task); | ||
}; | ||
}; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the following syntax to initialize options?