disposable-frames

1.0.4 • Public • Published

Build Status Coverage

disposable-frames

Disposable frame scheduler which abandons frames if CPU is high or the I/O event loop is blocking. disposable-frames is useful for web pages with heavy DOM manipulation.

disposable-frames detects the usage of CPU approximately by monitoring the hehavior of setTimeout(func, 0).

Install

$ npm i disposable-frames

Usage

import {
  setImmediate,
  immediate
} from 'disposable-frames'

setImmediate(func: Function, options: Object): number | Immediate

  • func Function The function to call as setTimeout(func, 0)
  • options.tolerance ?number=0 The miniseconds within which since the setImmediate called that the func is allowed to execute. If options.tolerance is 0, the default value, it indicates there is no restriction, which is silly because it is the only reason for this package to exist.
  • Returns
    • Immediate on node
    • number the timer id on browsers

Schedules the "immediate" execution of the func if the schedule doesn't take too long.

If we schedule a function foo with options.tolerance as 50(ms), and in the real world, the I/O event loops take too long to respond, which causes that the setImmediate callback is supposed to be scheduled after 100ms from the beginning. And then the execution of foo will be abandoned according to options.tolerance.

setImmediate(func, {
  tolerance: 10
})

immediate(func: Function, options: Object): Function

  • func Function The function to call as setTimeout(func, 0)
  • options ?Object
    • tolerance ?number=0
    • maxWait ?number=0 The maximum time func is allowed to be abandoned before it's invoked. In the other words, after every maxWait time, one execution of func is allowed despite of the limitation of tolerance. If maxWait is 0, the feature is disabled.
    • leading boolean=false If true, the first execution is always allowed.

Wrap the func as a new function which schedules func as well as setImmediate does every time the wrapper function invokes.

const wrapped = immediate(func, {
  maxWait: 500,
  tolerance: 50
})

Suppose that it takes precisely 100ms for func to execute every time then:

wrapped()                   // will execute
wrapped()                   // disposed
wrapped()                   // disposed
setTimeout(wrapped, 200)    // will execute

License

MIT

Package Sidebar

Install

npm i disposable-frames

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

9.56 kB

Total Files

6

Last publish

Collaborators

  • kael