Skip to content

Commit

Permalink
Merge 4012ba6 into dcb0076
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom910 committed Jun 2, 2019
2 parents dcb0076 + 4012ba6 commit ddd1bb6
Show file tree
Hide file tree
Showing 10 changed files with 7,951 additions and 4,473 deletions.
50 changes: 31 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# Frame Scheduling

[![Build Status](https://travis-ci.org/Tom910/frame-scheduling.svg?branch=master)](https://travis-ci.org/Tom910/frame-scheduling)
[![Coverage Status](https://coveralls.io/repos/github/Tom910/frame-scheduling/badge.svg?branch=master)](https://coveralls.io/github/Tom910/frame-scheduling?branch=master)

# Frame Scheduling
A tiny module which allows run a non-blocking layout many tasks.

* **Fast.** Contains low overhead and optimized for running lots of tasks without drop fps
* **Small.** 930 B (minified and gzipped). No dependencies. It uses [Size Limit](https://github.com/ai/size-limit) to control size.
* **Priority** Separate tasks into different priorities. Try to complete priority tasks as quickly as possible.
* **Isomorphic.** work in browser and node js.


```js
import frameScheduling, { P_IMPORTANT } from 'frame-scheduling';

frameScheduling(() => { Action() }, { priority: P_IMPORTANT });
frameScheduling(() => { console.log('async task') });
```
[Demo](https://codesandbox.io/s/admiring-ride-jdoq0)

Asynchronous running tasks in JavaScript based on requestAnimationFrame. Supports priority and interrupt execution every 16 milliseconds, to achieve 60fps.

Expand All @@ -21,38 +29,42 @@ yarn add frame-scheduling
npm install --save frame-scheduling
```

## Example
### Priority

## Priority
```js
import frameScheduling, { P_IMPORTANT, P_LOW } from 'frame-scheduling';
const result = [];

frameScheduling(() => { result.push('Ember') }, { priority: P_LOW })
frameScheduling(() => { result.push('Angular') })
frameScheduling(() => { result.push('React') }, { priority: P_IMPORTANT })
frameScheduling(() => { result.push('A') }, { priority: P_LOW })
frameScheduling(() => { result.push('B') })
frameScheduling(() => { result.push('C') }, { priority: P_IMPORTANT })
frameScheduling(() => { result.push('D') }, { priority: 1000 })

console.log(result) // > ['React', 'Angular', 'Ember']
// after doing
console.log(result) // > ['D', 'C', 'B', 'A']
```
perform priority tasks first

### framing
```js
import frameScheduling from 'frame-scheduling';

frameScheduling(() => 'function 1') // light < 1ms exec
frameScheduling(() => 'function 2') // heavy > 17ms exec
frameScheduling(() => 'function 3') // heavy > 17ms exec
frameScheduling(() => 'function 4') // light < 1ms exec
frameScheduling(() => 'function 5') // light < 1ms exec
frameScheduling(() => lightFunction()) // light < 1ms exec
frameScheduling(() => heavyFunction()) // heavy > 17ms exec
frameScheduling(() => heavyFunction2()) // heavy > 17ms exec
frameScheduling(() => lightFunction2()) // light < 1ms exec
frameScheduling(() => lightFunction3()) // light < 1ms exec

/*
Runs in frame
| function 1
| function 2
| function 3
| function 4
| function 5
| lightFunction
| heavyFunction
| heavyFunction2
| lightFunction2
| lightFunction3
*/
```
frame-scheduling aims to achieve 60 fps

## Options
#### priority: number = 5
Expand Down
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ module.exports = {
"js"
],
"transform": {
"^.+\\.ts$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.ts$": "ts-jest"
},
"timers": "fake",
"mapCoverage": true,
"testMatch": [
"<rootDir>/tests/**/*.(ts|js)"
]
Expand Down

0 comments on commit ddd1bb6

Please sign in to comment.