+13
-2
@@ -13,3 +13,3 @@ "use strict"; | ||
| var result = function result() { | ||
| var wrapperFn = function wrapperFn() { | ||
| for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -33,3 +33,14 @@ args[_key] = arguments[_key]; | ||
| return result; | ||
| wrapperFn.cancel = function () { | ||
| if (!frameId) { | ||
| return; | ||
| } | ||
| cancelAnimationFrame(frameId); | ||
| frameId = null; | ||
| }; | ||
| var resultFn = wrapperFn; | ||
| return resultFn; | ||
| }; |
+5
-5
| { | ||
| "name": "raf-schd", | ||
| "version": "2.0.2", | ||
| "version": "2.1.0", | ||
| "description": "A simple scheduler based on requestAnimationFrame", | ||
@@ -23,7 +23,7 @@ "main": "lib/index.js", | ||
| "babel-preset-flow": "6.23.0", | ||
| "eslint": "4.10.0", | ||
| "eslint-plugin-jest": "21.2.0", | ||
| "flow-bin": "0.58.0", | ||
| "eslint": "4.11.0", | ||
| "eslint-plugin-jest": "21.3.2", | ||
| "flow-bin": "0.59.0", | ||
| "jest": "21.2.1", | ||
| "prettier": "1.8.1", | ||
| "prettier": "1.8.2", | ||
| "raf-stub": "2.0.1" | ||
@@ -30,0 +30,0 @@ }, |
+29
-6
@@ -79,10 +79,16 @@ # raf-schd | ||
| type ResultFn = (...arg: any[]) => number; | ||
| // Adding a .cancel property to the WrapperFn | ||
| type WrapperFn = (...arg: any[]) => number; | ||
| type CancelFn = {| | ||
| cancel: () => void, | ||
| |}; | ||
| type ResultFn = WrapperFn & CancelFn; | ||
| ``` | ||
| At the top level `raf-schd` accepts any function a returns a new `ResultFn` (a function that wraps your original function). When executed, the `ResultFn` returns a `number`. This number is the animation frame id. You can use this frame id to cancel the scheduled frame using `cancelAnimationFrame(id)`; | ||
| At the top level `raf-schd` accepts any function a returns a new `ResultFn` (a function that wraps your original function). When executed, the `ResultFn` returns a `number`. This number is the animation frame id. You can cancel a frame using the `.cancel()` property on the `ResultFn`. | ||
| The `ResultFn` will execute your function with the **latest arguments** provided to it on the next animation frame. | ||
| **Throttled with latest argument** | ||
| ### Throttled with latest argument | ||
@@ -105,8 +111,25 @@ ```js | ||
| ### Cancelling a frame | ||
| **Cancelling a frame** | ||
| #### `.cancel` | ||
| `raf-schd` adds a `.cancel` property to the `ResultFn` so that it can be easily cancelled. The frame will only be cancelled if it has not yet executed. | ||
| ```js | ||
| const scheduled = rafSchedule(doSomething); | ||
| schedule('foo'); | ||
| scheduled.cancel(); | ||
| // now doSomething will not be executed in the next animation frame | ||
| ``` | ||
| #### `cancelAnimationFrame` | ||
| You can use [`cancelAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/Window/cancelAnimationFrame) directly to cancel a frame if you like. You can do this because you have the `frameId`. | ||
| ```js | ||
| const scheduled = rafSchedule(doSomething); | ||
| const frameId = schedule('foo'); | ||
@@ -125,3 +148,3 @@ | ||
| **Similiarities to `throttle`** | ||
| ### Similarities to `throttle` | ||
@@ -133,3 +156,3 @@ - It batches multiple calls into a single event | ||
| **Differences to `throttle`** | ||
| ### Differences to `throttle` | ||
@@ -136,0 +159,0 @@ - Rather than throttling based on time (such as `200ms`, this library throttles based on `requestAnimationFrame`. This allows the browser to control how many frames to provide per second to optimise rendering. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7083
13.66%31
34.78%187
14.02%0
-100%