disposable-frames v1.0.4
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-framesUsage
import {
setImmediate,
immediate
} from 'disposable-frames'setImmediate(func: Function, options: Object): number | Immediate
- func
FunctionThe function to call assetTimeout(func, 0) - options.tolerance
?number=0The miniseconds within which since thesetImmediatecalled that thefuncis allowed to execute. Ifoptions.toleranceis0, the default value, it indicates there is no restriction, which is silly because it is the only reason for this package to exist. - Returns
Immediateon nodenumberthe 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
FunctionThe function to call assetTimeout(func, 0) - options
?Object- tolerance
?number=0 - maxWait
?number=0The maximum timefuncis allowed to be abandoned before it's invoked. In the other words, after everymaxWaittime, one execution offuncis allowed despite of the limitation oftolerance. IfmaxWaitis 0, the feature is disabled. - leading
boolean=falseIftrue, the first execution is always allowed.
- tolerance
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 executeLicense
MIT