Skip to content

Commit 4ca18ab

Browse files
authored
Merge pull request #1794 from SUI-Components/feat/performance
feat(packages/sui-performance): add performance package
2 parents faca9bc + bcf5250 commit 4ca18ab

File tree

6 files changed

+151
-1
lines changed

6 files changed

+151
-1
lines changed

package-lock.json

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# CHANGELOG

packages/sui-performance/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# @s-ui/performance
2+
3+
> Performance utilities to make your web application go fast ⚡️
4+
5+
## Installation
6+
7+
```sh
8+
npm install @s-ui/performance
9+
```
10+
11+
## Usage
12+
13+
### Delay code execution
14+
15+
Use this function to delay the execution of an expensive operation and prioritize user actions. Keep in mind that it only delays the response by a maximum of 1 frame, an average of 8ms, which is too little for a human to notice for the types of major actions where you’d use this function.
16+
17+
```jsx
18+
import {delayTask} from '@s-ui/performance';
19+
20+
export default function Example() {
21+
const [counter, setCounter] = useState(0)
22+
23+
const handleClick = async () => {
24+
setCounter(counter => counter + 1)
25+
26+
await delayTask()
27+
28+
work() // expensive work
29+
}
30+
31+
return <button onClick={handleClick}>{counter}</button>
32+
}
33+
```
34+
35+
### Delay code execution until urgent
36+
37+
Use this function to delay the execution of an expensive operation while the main thread is idle, using [requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback), and to prioritize user actions. This method ensures the execution is completed before the user leaves the page. It is especially useful for delaying tracking execution.
38+
39+
The `delayTaskUntilUrgent` function optionally receives an options object. The documentation can be found [here](https://github.com/redbus-labs/idlefy/tree/main?tab=readme-ov-file#methods) ([idlefy](https://github.com/redbus-labs/idlefy/tree/main) is used under the hood).
40+
41+
```jsx
42+
import {delayTaskUntilUrgent} from '@s-ui/performance';
43+
44+
export default function Example() {
45+
const [counter, setCounter] = useState(0)
46+
47+
const handleClick = async () => {
48+
setCounter(counter => counter + 1)
49+
50+
await delayTaskUntilUrgent()
51+
52+
track() // expensive work
53+
}
54+
55+
return <button onClick={handleClick}>{counter}</button>
56+
}
57+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@s-ui/performance",
3+
"version": "1.0.0",
4+
"description": "",
5+
"type": "module",
6+
"main": "lib/index.js",
7+
"scripts": {
8+
"lib": "babel --presets sui ./src --out-dir ./lib",
9+
"prepublishOnly": "npm run lib",
10+
"test:browser:watch": "NODE_ENV=test npm run test:browser -- --watch",
11+
"test:browser": "NODE_ENV=test sui-test browser",
12+
"test:server:watch": "npm run test:server -- --watch",
13+
"test:server": "NODE_ENV=test sui-test server",
14+
"test": "npm run test:server && npm run test:browser"
15+
},
16+
"keywords": [],
17+
"author": "",
18+
"license": "MIT",
19+
"dependencies": {
20+
"idlefy": "1.1.1"
21+
},
22+
"peerDependencies": {},
23+
"devDependencies": {
24+
"@s-ui/test": "8"
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {IdleQueue} from 'idlefy'
2+
3+
const queue = new IdleQueue({ensureTasksRun: true})
4+
5+
export function delayTask() {
6+
return new Promise(resolve => {
7+
setTimeout(resolve, 100)
8+
requestAnimationFrame(() => {
9+
setTimeout(resolve, 0)
10+
})
11+
})
12+
}
13+
14+
export function delayTaskUntilUrgent(options) {
15+
return new Promise(resolve => {
16+
queue.pushTask(resolve, options)
17+
})
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {expect} from 'chai'
2+
import sinon from 'sinon'
3+
4+
import {waitFor} from '@testing-library/react'
5+
6+
import {delayTask, delayTaskUntilUrgent} from '../../src/index.js'
7+
8+
describe('delayTask', () => {
9+
it('should execute function', async () => {
10+
const callback = sinon.spy()
11+
12+
delayTask().then(callback)
13+
14+
await waitFor(() => expect(callback.called).to.be.true)
15+
})
16+
})
17+
18+
describe('delayTaskUntilUrgent', () => {
19+
it('should execute function', async () => {
20+
const callback = sinon.spy()
21+
22+
delayTaskUntilUrgent().then(callback)
23+
24+
await waitFor(() => expect(callback.called).to.be.true)
25+
})
26+
})

0 commit comments

Comments
 (0)