@react-spring/rafz
react-spring's fork of rafz one frameloop to rule them all
Last updated 2 years ago by tdfka_rick .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @react-spring/rafz 
SYNC missed versions from official npm registry.

@react-spring/rafz

Coordinate requestAnimationFrame calls across your app and/or libraries.

  • < 700 bytes min+gzip
  • Timeout support
  • Batching support (eg: ReactDOM.unstable_batchedUpdates)
  • Uncaught errors are isolated
  • Runs continuously (to reduce frame skips)

 

API

import { raf } from '@react-spring/rafz'

// Schedule an update
raf(dt => {})

// Start an update loop
raf(dt => true)

// Cancel an update
raf.cancel(fn)

// Schedule a mutation
raf.write(() => {})

// Before any updates
raf.onStart(() => {})

// Before any mutations
raf.onFrame(() => {})

// After any mutations
raf.onFinish(() => {})

// Set a timeout that runs on nearest frame
raf.setTimeout(() => {}, 1000)

// Use a polyfill
raf.use(require('@essentials/raf').raf)

// Get the current time
raf.now() // => number

// Set how you want to control raf firing
raf.frameLoop = 'demand' | 'always'

 

Notes

  • Functions can only be scheduled once per queue per frame.
  • Thus, trying to schedule a function twice is a no-op.
  • The update phase is for updating JS state (eg: advancing an animation).
  • The write phase is for updating native state (eg: mutating the DOM).
  • Reading is allowed any time before the write phase.
  • Writing is allowed any time after the onFrame phase.
  • Timeout handlers run first on each frame.
  • Any handler (except timeouts) can return true to run again next frame.
  • The raf.cancel function only works with raf handlers.
  • Use raf.sync to disable scheduling in its callback.
  • Override raf.batchedUpdates to avoid excessive re-rendering in React.

 

raf.throttle

Wrap a function to limit its execution to once per frame. If called more than once in a single frame, the last arguments are used.

let log = raf.throttle(console.log)

log(1)
log(2) // nothing logged yet

raf.onStart(() => {
  // "2" is logged by now
})

// Cancel a pending call.
log.cancel()

// Access the wrapped function.
log.handler

Prior art

Current Tags

  • 9.4.0-alpha.0                                ...           alpha (4 years ago)
  • 10.0.3-beta.0                                ...           beta (7 months ago)
  • 10.0.3                                ...           latest (7 months ago)

73 Versions

  • 0.0.0-nightly-52cc710-20230626001840                                ...           3 years ago
  • 0.0.0-65fc02cc281573bc28d90720983c8e9df9035059-20230625170003                                ...           3 years ago
  • 0.0.0-d80cafe1f0ccd7ce193b8f558285515ede3b8afc-20230625160448                                ...           3 years ago
  • 0.0.0-nightly-bc3ce76-20230407001611                                ...           3 years ago
  • 0.0.0-nightly-bc3ce76-20230406001620                                ...           3 years ago
  • 0.0.0-nightly-bc3ce76-20230405001601                                ...           3 years ago
  • 0.0.0-nightly-bc3ce76-20230404001701                                ...           3 years ago
  • 0.0.0-nightly-bc3ce76-20230403001702                                ...           3 years ago
  • 0.0.0-nightly-bc3ce76-20230402001810                                ...           3 years ago
  • 0.0.0-nightly-bc3ce76-20230401001858                                ...           3 years ago
  • 0.0.0-nightly-789adf2-20230331070515                                ...           3 years ago
  • 0.0.0-nightly-f50b673-20230330070501                                ...           3 years ago
  • 0.0.0-40ac9039be0c657954e31ea7588bee4abbf1df1f-20230227081728                                ...           3 years ago
  • 0.0.0-fb0ddfbbce1fd1b524534d96188b7bdb923c7bc7-20230227080834                                ...           3 years ago
  • 0.0.0-b13152991101c4b7adc41eba4f10e8d155511bbe-20230227075103                                ...           3 years ago
  • 0.0.0-aefd1e584376be38b1f6ca1ca74e061c4ce9b9d8-20230225174818                                ...           3 years ago
  • 0.0.0-d0cc422bcf5dc2109d1d80e6ea065b4d8861d323-20230225174329                                ...           3 years ago
  • 0.0.0-1f54fd5554e133fd353fcf3bd85edeac3562a1a0-20230225150642                                ...           3 years ago
  • 0.0.0-00758660b56704e0949d99f4251c275d738376fe-20230225114436                                ...           3 years ago
  • 10.0.3                                ...           7 months ago
  • 10.0.3-beta.0                                ...           7 months ago
  • 10.0.2                                ...           7 months ago
  • 10.0.1                                ...           a year ago
  • 10.0.0                                ...           a year ago
  • 10.0.0-beta.0                                ...           a year ago
  • 9.7.5                                ...           2 years ago
  • 9.7.4                                ...           2 years ago
  • 9.7.3                                ...           3 years ago
  • 9.7.2                                ...           3 years ago
  • 9.7.1                                ...           3 years ago
  • 9.7.0                                ...           3 years ago
  • 9.7.0-beta.0                                ...           3 years ago
  • 9.6.1                                ...           3 years ago
  • 9.6.0                                ...           3 years ago
  • 9.5.5                                ...           4 years ago
  • 9.5.5-beta.0                                ...           4 years ago
  • 9.5.4                                ...           4 years ago
  • 9.5.3 [deprecated]           ...           4 years ago
  • 9.5.2                                ...           4 years ago
  • 9.5.1                                ...           4 years ago
  • 9.5.0                                ...           4 years ago
  • 9.4.5                                ...           4 years ago
  • 9.4.5-beta.2                                ...           4 years ago
  • 9.4.5-beta.1                                ...           4 years ago
  • 9.4.5-beta.0                                ...           4 years ago
  • 9.4.4                                ...           4 years ago
  • 9.4.3                                ...           4 years ago
  • 9.4.3-beta.1                                ...           4 years ago
  • 9.4.3-beta.0                                ...           4 years ago
  • 9.4.2                                ...           4 years ago
  • 9.4.1                                ...           4 years ago
  • 9.4.0                                ...           4 years ago
  • 9.4.0-beta.6                                ...           4 years ago
  • 9.3.2                                ...           4 years ago
  • 9.4.0-beta.4                                ...           4 years ago
  • 9.4.0-beta.3                                ...           4 years ago
  • 9.4.0-beta.2                                ...           4 years ago
  • 9.4.0-beta.1                                ...           4 years ago
  • 9.4.0-beta.0                                ...           4 years ago
  • 9.3.1                                ...           4 years ago
  • 9.4.0-alpha.0                                ...           4 years ago
  • 9.3.0                                ...           5 years ago
  • 9.2.6                                ...           5 years ago
  • 9.2.6-beta.0                                ...           5 years ago
  • 9.2.5                                ...           5 years ago
  • 9.2.5-beta.0                                ...           5 years ago
  • 9.2.4                                ...           5 years ago
  • 10.0.0-alpha.1                                ...           5 years ago
  • 10.0.0-alpha.0                                ...           5 years ago
  • 9.2.3                                ...           5 years ago
  • 9.2.2                                ...           5 years ago
  • 9.2.1                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None

Copyright 2013 - present © cnpmjs.org | Home |