
Research
/Security News
DuckDB npm Account Compromised in Continuing Supply Chain Attack
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
The idlefy
library is a collection of JavaScript tools designed to help you improve website performance by strategically scheduling tasks to run during the browser's idle periods. This adheres to the idle-until-urgent pattern.
Inspired by the idilize library, idlefy not only retains all the original features but also introduces enhanced TypeScript support, a reduced bundle size, and a more adaptable API. Moreover, it seamlessly serves as a drop-in replacement for idilize, passing all the tests from the original library.
npm install idlefy
// imports all helpers methods and classes from idlefy library
import { IdleQueue, IdleValue } from 'idlefy'
// import only methods and classes you need
import { IdleQueue } from 'idlefy/idleQueue.js'
// import methods from minified build
import { IdleQueue } from 'idlefy/min'
IdleQueue
It's useful for apps that want to split up their logic into a sequence of functions and schedule them to run idly.
This class offers a few benefits over the regular usage of requestIdleCallback()
:
import { IdleQueue } from 'idlefy/idleQueue.js'
const queue = new IdleQueue()
queue.pushTask(() => {
// Some expensive function that can run idly...
})
queue.pushTask(() => {
// Some other task that depends on the above
// expensive function having already run...
})
Name | Description |
---|---|
constructor(options) |
Parameters:
|
pushTask(task, options) |
Parameters:
Adds a task to the end of the queue and schedules the queue to be run when next idle (if not already scheduled). When the task is run, it's invoked with an object containing the following properties:
|
unshiftTask(task, options) |
Parameters:
Adds a task to the beginning of the queue and schedules the queue to be run when next idle (if not already scheduled). When the task is run, it's invoked with an object containing the following properties:
|
runTasksImmediately() |
Runs all queued tasks immediately (synchronously). |
hasPendingTasks() |
Returns: (boolean) True if the queue has any tasks not yet run. |
clearPendingTasks() |
Unschedules all pending tasks in the queue. |
getState() |
Returns: (Object)
|
IdleValue
It's useful when you want to initialize a value during an idle period but ensure it can be initialized immediately as soon as it's needed.
import { IdleValue } from 'idlefy/idleValue.js'
class MyClass {
constructor() {
// Create an IdleValue instance for `this.data`. It's value is
// initialized in an idle callback (or immediately as soon as
// `this.data.getValue()` is called).
this.data = new IdleValue(() => {
// Run expensive code and return the result...
})
}
}
Name | Description |
---|---|
constructor(init) |
Parameters:
The initialization function is scheduled to run in an idle callback as soon as the instance is created. |
getValue() |
Returns: (*) Returns the value returned by the initialization function passed to the constructor. If the initialization function has already been run, the value is returned immediately. If the initialization function is still scheduled for an idle callback, that callback is cancelled, the initialization function is run synchronously, and the result is returned. |
setValue(newValue) |
Parameters:
Assigns a new value. If the initialization function passed to the constructor has not yet run, it is cancelled. |
idle-callback-with-polyfills
Small polyfills that allow developers to use requestIdleCallback
and cancelIdleCallback()
in all browsers.
These are not full polyfills (since the native APIs cannot be fully polyfilled), but they offer the basic benefits of idle tasks via setTimeout()
and clearTimeout()
.
import { cIC, rIC } from 'idlefy/idleCbWithPolyfill.js'
// To run a task when idle.
const handle = rIC(() => {
// Do something here...
})
// To cancel the idle callback.
cIC(handle)
rIC
Uses the native requestIdleCallback()
function in browsers that support it, or a small polyfill (based on setTimeout()
) in browsers that don't.
See the requestIdleCallback()
docs on MDN for details.
cIC
Uses the native cancelIdleCallback()
function in browsers that support it, or a small polyfill (based on clearTimeout()
) in browsers that don't.
See the cancelIdleCallback()
docs on MDN for details.
defineIdleProperty
It's useful when you want to initialize a property value during an idle period but ensure it can be initialized immediately as soon as it's referenced.
import { defineIdleProperty } from 'idlefy/defineIdleProperty.js'
class MyClass {
constructor() {
// Define a getter for `this.data` whose value is initialized
// in an idle callback (or immediately if referenced).
defineIdleProperty(this, 'data', () => {
// Run expensive code and return the result...
})
}
}
defineIdleProperty(obj, prop, init)
Name | Type | Description |
---|---|---|
obj | Object | The object on which to define the property. |
prop | string | The name of the property. |
init | Function | An function (typically something expensive to compute) that returns a value. The function is scheduled to run in an idle callback as soon as the property is defined. If the property is referenced before the function can be run in an idle callback, the idle callback is canceled, the function is run immediately, and the return value of the function is set as the value of the property. |
defineIdleProperties
It's useful when you want to initialize one or more property values during an idle period but ensure they can be initialized immediately as soon as they're referenced.
import { defineIdleProperties } from 'idlefy/defineIdleProperties.js'
class MyClass {
constructor() {
// Define a getter for `this.data` whose value is initialized
// in an idle callback (or immediately if referenced).
defineIdleProperties(this, {
data: () => {
// Run expensive code and return the result...
},
})
}
}
defineIdleProperties(obj, props)
Name | Type | Description |
---|---|---|
obj | Object | The object on which to define the property. |
props | Object |
A dictionary of property names and initialization functions. See the defineIdleProperty documentation for prop and init .
|
FAQs
Defer non-critical tasks to run when the main thread is idle
We found that idlefy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.