Skip to content

Commit defc5b7

Browse files
authored
Merge pull request #78 from InhiblabCore/dev
feat: new package useWorker
2 parents ed2e91d + 4310c84 commit defc5b7

21 files changed

+497
-2
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ jobs:
2121
- name: Test
2222
run: |
2323
pnpm run test
24-
- name: use-immer Build
24+
- name: Build use-immer
2525
run: |
26-
cd packages/use-immer
26+
cd packages/use-immer
27+
pnpm build
28+
- name: Build use-worker
29+
run: |
30+
cd packages/use-worker
2731
pnpm build
2832
- name: Docs-Build
2933
run: |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Thanks for all their contributions!
9393
This project is heavily inspired by the following awesome projects.
9494

9595
- [ahooks](https://ahooks.js.org/)
96+
- [@koale/useworker](https://github.com/alewin/useWorker)
9697

9798
## 📄 License
9899

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ npm run docs:dev
9393
这个项目的灵感主要来自于以下这些很棒的项目。
9494

9595
- [ahooks](https://ahooks.js.org/)
96+
- [@koale/useworker](https://github.com/alewin/useWorker)
9697

9798
## 📄 证书
9899

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@vue-hooks-plus/vitepress": "1.2.3",
3737
"@vue-hooks-plus/vitepress-demo-block": "^1.3.1",
3838
"@vue-hooks-plus/use-immer":"workspace:^1.0.0",
39+
"@vue-hooks-plus/use-worker":"workspace:^1.0.0",
3940
"@vue/test-utils": "^2.1.0",
4041
"@vitest/coverage-c8":"^0.25.7",
4142
"eslint": "^8.20.0",

packages/hooks/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Thanks for all their contributions!
9393
This project is heavily inspired by the following awesome projects.
9494

9595
- [ahooks](https://ahooks.js.org/)
96+
- [@koale/useworker](https://github.com/alewin/useWorker)
9697

9798
## 📄 License
9899

packages/hooks/docs/.vitepress/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const Router = [
2929
{ text: 'useUpdate', link: '/useUpdate/' },
3030
{ text: 'useInterval', link: '/useInterval/' },
3131
{ text: 'useTimeout', link: '/useTimeout/' },
32+
{ text: 'useWorker', link: '/useWorker/' },
3233
],
3334
},
3435
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<template>
2+
<p
3+
>Current Time: <b>{{ computedTime }}</b></p
4+
>
5+
<!-- <p>Result: {{ JSON.stringify(result) }}</p> -->
6+
<div>
7+
<vhp-button @click="runSort()">Run Sort</vhp-button>
8+
<vhp-button @click="runWorkerSort()" style="margin-left: 8px;">Run Worker Sort</vhp-button>
9+
</div>
10+
</template>
11+
12+
<script lang="ts" setup>
13+
import { useInterval } from 'vue-hooks-plus'
14+
import { ref } from 'vue'
15+
import { useWorker } from '@vue-hooks-plus/use-worker'
16+
17+
const numbers = [...Array(5000000)].map(e => ~~(Math.random() * 1000000))
18+
const sortNumbers = (nums: any[]) => nums.sort((a, b) => a - b)
19+
20+
const result = ref<any[]>([])
21+
22+
const computedTime = ref(new Date().getTime())
23+
useInterval(() => {
24+
computedTime.value = new Date().getTime()
25+
}, 10)
26+
27+
const [sortWorker] = useWorker(sortNumbers)
28+
const runWorkerSort = async () => {
29+
// non-blocking UI
30+
result.value = await sortWorker(numbers)
31+
console.log(result.value)
32+
}
33+
34+
const runSort = async () => {
35+
result.value = sortNumbers(numbers)
36+
console.log(result.value)
37+
}
38+
</script>
39+
40+
<style scoped lang="less"></style>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /useWorker
5+
---
6+
7+
# useWorker
8+
9+
`useWorker` is a js library (with typescript support) that allows you to use the [Web Worker Web API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers), through Vue Hook. This library allows you to run the expensive function without blocking the user interface, using a simple syntax that makes use of [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise?retiredLocale=it)
10+
11+
## Code demonstration
12+
13+
<demo src="./demo/demo.vue"
14+
language="vue"
15+
title="basic usage"
16+
desc="Normal sorting will block UI bleeding, while worker sorting will not"> </demo>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /useWorker
5+
---
6+
7+
# useWorker
8+
9+
`useWorker` 允许您通过 Vue Hook 使用 [Web Worker Web API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)。该库允许您使用使用 [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise?retiredLocale=it) 的简单语法来运行昂贵的函数而不会阻塞用户界面
10+
11+
## 代码演示
12+
13+
<demo src="./demo/demo.vue"
14+
language="vue"
15+
title="基本用法"
16+
desc="正常排序会阻塞UI渲染,而worker排序则不会"> </demo>

packages/use-worker/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 YongGit
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)