hiroki0525 / idle-task Goto Github PK
View Code? Open in Web Editor NEWImprove your website performance by executing JavaScript during a browser's idle periods.
License: MIT License
Improve your website performance by executing JavaScript during a browser's idle periods.
License: MIT License
You don't have to manage caches manually.
Here is an example.
const yourFunction = () => console.log('yourFunction');
// Before
// set `{ cache: false }` because the result isn't needed
setIdleTask(yourFunction, { cache: false });
// After
// this will remove the cache automatically
setIdleTask(yourFunction);
We must manage caches manually as belows.
// example1
configureIdleTask({ cache: false });
// example2
setIdleTask(yourFunction, { cache: false })
// example3
const taskId = setIdleTask(yourFunction);
const result1 = await forceRunIdleTask(taskId);
const result2 = await forceRunIdleTask(taskId, {
cache: false,
});
This disgust us.
Ideally, the cache would be deleted when it's no longer needed.
This is because idle-task
is using Map
to cache results.
// Map.set(taskId, result);
const taskId = setIdleTask(yourFunction);
// Map.delete(taskId, result);
const result = await forceRunIdleTask(taskId, {
cache: false,
});
idle-task
will use WeakMap
instead of Map
.
// taskKey is `Object` because the key of `WeakMap` must be `Object` .
// WeakMap.set(taskKey, result);
let taskKey = setIdleTask(yourFunction);
const result = await forceRunIdleTask(taskId);
// The cached result will be remove automatically because of garbage collection.
taskKey = null;
import { getResultFromIdleTask } from 'idle-task';
const result = await getResultFromIdleTask(hogeFunction, {
timeout: 1000,
timeoutStrategy: 'forceRun'
})
// When the task had time out, the result is undefined
console.log(result);
// => undefined
There are some common cases like
I'm looking a way to optimize some UI JS for those and found this together with
https://github.com/GoogleChromeLabs/idlize
Both are nice idea and exactly what I'm approaching. However in both cases, I can not see an interface API which allow to do these when CPU is idle while allow the initialization can be triggered immediately if users interact (and it was not initiated).
Do you have any idea? @hiroki0525 @yuchi @joeinnes @m5r
timeoutStrategy
is forceRun
as default.
Here is an example.
configureIdleTask({ timeout: 3000 });
const taskKey = setIdleTask(yourFunction);
// Before
// After 3000 ms, `idle-task` will throw Error if the task has not been run when the browser is idle.
try {
const result = await waitForIdleTask(taskKey);
} catch (e) {
// error handling
}
// After
// After 3000 ms, `idle-task` will execute the task if it has not been run when the browser is idle.
const result = await waitForIdleTask(taskKey);
I think that everyone sets forceRun
option in most cases.
Here is an example of React.lazy
.
import { getResultFromIdleTask, configureIdleTask } from 'idle-task';
import { lazy } from 'react';
configureIdleTask({ timeout: 3000 });
// Wrap `React.lazy`
const lazyWhenIdle = (factory: Parameters<typeof lazy>[0]) => {
const idleTaskPromise = getResultFromIdleTask(factory);
return lazy(() => idleTaskPromise);
}
// Problem: It maybe will throw Error.
const YourComponent = lazyWhenIdle(() => import('~/components/YourComponent'));
In this case, It will be solved like belows.
configureIdleTask({ timeout: 3000, timeoutStrategy: 'forceRun' });
// or
const idleTaskPromise = getResultFromIdleTask(factory, { timeoutStrategy: 'forceRun' });
But this disgust us.
Ideally, timeoutStrategy
is forceRun
as default.
This is because timeoutStrategy
is error
as default.
// `timeoutStrategy` is `error`
configureIdleTask({ timeout: 3000 });
timeoutStrategy
will be forceRun
instead of error
as default.
// `timeoutStrategy` is `forceRun`
configureIdleTask({ timeout: 3000 });
This project uses semantic-release to release, so it is import to add commit message correctly.
To know everyone this, I should add contributing guideline and ci for test that checks commit message.
Hi ๐
TLDR: Does it run in inactive tab?
I'm looking for a solution to the following problem:
Before closing a tab, I need to send an API request from the browser to server to the make a graceful teardown of resources allocated on the server side for the specific browser tab.
I used beforeunload
event for that, but this event doesn't emit when the user closes the tab when they are on another tab.
The solution that comes to my mind is to implement some kind of periodic heartbeat from browser to server.
My first thought was to implement it using Web Workers running in the background. After a bit more thinking on this option, it looks like it won't solve my problem.
I wonder, can this library help me to solve my task?
I'm not familiar with the requestIdleCallback
browser API.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.