https://github.com/patrickjs/request-idle-callback
requestIdleCallback for Angular
https://github.com/patrickjs/request-idle-callback
requestidlecallback scheduling
Last synced: 5 months ago
JSON representation
requestIdleCallback for Angular
- Host: GitHub
- URL: https://github.com/patrickjs/request-idle-callback
- Owner: PatrickJS
- License: apache-2.0
- Created: 2016-06-18T15:39:38.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-10-10T20:18:41.000Z (over 2 years ago)
- Last Synced: 2025-10-02T06:58:38.713Z (6 months ago)
- Topics: requestidlecallback, scheduling
- Language: TypeScript
- Homepage:
- Size: 17.6 KB
- Stars: 11
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Angular Request Idle Callback
cross-platform supported `requestIdleCallback` for Angular used for Progressive Web App and Universal Apps
### Why should I use requestIdleCallback?
Scheduling non-essential work yourself is very difficult to do. It’s impossible to figure out exactly how much frame time remains because after `requestAnimationFrame` callbacks execute there are style calculations, layout, paint, and other browser internals that need to run. A home-rolled solution can’t account for any of those. In order to be sure that a user isn’t interacting in some way you would also need to attach listeners to every kind of interaction event `(scroll, touch, click)`, even if you don’t need them for functionality, just so that you can be absolutely sure that the user isn’t interacting. The browser, on the other hand, knows exactly how much time is available at the end of the frame, and if the user is interacting, and so through `requestIdleCallback` we gain an API that allows us to make use of any spare time in the most efficient way possible.

## links
* [Using requestIdleCallback](https://developers.google.com/web/updates/2015/08/using-requestidlecallback)
* [Cooperative Scheduling of Background Tasks](https://www.w3.org/TR/requestidlecallback/)
### Install
```typescript
bootstrap(App, [
...ANGULARCLASS_IDLE_PROVIDERS
]);
```
```typescript
import { Idle } from '@angularclass/request-idle-callback';
class App {
constructor(idle: Idle) {
idle.requestIdleCallback(() => {
prefetchComponent() // your api
});
}
}
```
prefetch data/async route after bootstrap via `providePrefetchIdleCallbacks`
```typescript
import { providePrefetchIdleCallbacks } from '@angularclass/request-idle-callback';
function callbackToPrefetch() {
}
var arrayOfCallbacks = [
callbackToPrefetch
]
bootstrap(App, [
providePrefetchIdleCallbacks(arrayOfCallbacks)
]);
```
___
enjoy — **PatrickJS**