Skip to content

Commit bd764fa

Browse files
authored
Merge pull request #132 from InhiblabCore/dev
feat: new plugin [useBroadcastChannelPlugin] at @vue-hooks-plus/use-request-plugins
2 parents b88c906 + 1da5273 commit bd764fa

File tree

23 files changed

+407
-93
lines changed

23 files changed

+407
-93
lines changed

commitlint.config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
// perf:性能, 体验优化 | Performance, experience optimization
77
// test:新增测试用例或是更新现有测试 | Add the new test cases or update the existing tests
88
// build:主要目的是修改项目构建系统(例如 glup,webpack,rollup 的配置等)的提交 | The main purpose is to modify the submission of the project building system (such as glup, webpack, rollup configuration, etc.)
9-
// ci:主要目的是修改项目继续集成流程(例如 Travis,Jenkins,GitLab CI,Circle等)的提交 | The main purpose is to modify the submission of project continued integration processes (e. g. Travis, Jenkins, GitLab CI, Circle, etc.)
9+
// workflow:主要目的是修改项目继续集成流程(例如 Travis,Jenkins,GitLab CI,Circle等)的提交 | The main purpose is to modify the submission of project continued integration processes (e. g. Travis, Jenkins, GitLab CI, Circle, etc.)
1010
// chore:不属于以上类型的其他类,比如构建流程, 依赖管理 | Other classes that do not belong to the above types, such as building processes, dependency management
1111
// revert:回滚某个更早之前的提交 | Roll back some earlier previous submission
12-
// workflow: 工作流 | workflow
1312
// version: 改变package.json 版本 | Change the package.json version
1413

1514
module.exports = {
@@ -34,7 +33,7 @@ module.exports = {
3433
'test',
3534
'refactor',
3635
'build',
37-
'ci',
36+
'type',
3837
'chore',
3938
'revert',
4039
'workflow',

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@
7373
"vue-tsc": "1.0.9",
7474
"vue-typical": "^2.1.0",
7575
"pinia":"^2.0.34",
76+
"broadcast-channel":"^5.1.0",
7677
"three":"0.147.0",
78+
"axios": "^0.24.0",
7779
"vue-demi": "^0.13.11"
7880
},
7981
"lint-staged": {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ const useRequestPlugins = [
170170
text: '全局请求状态管理',
171171
link: '/useRequest/plugins/fetchsing/',
172172
},
173+
{
174+
text: "同源跨窗口广播",
175+
link: "/useRequest/plugins/broadcastChannel/"
176+
}
173177
],
174178
},
175179
]
@@ -179,9 +183,13 @@ const useRequestPluginsEN = [
179183
text: 'UseRequest External Plugins',
180184
items: [
181185
{
182-
text: 'Global Fetching',
186+
text: 'global Fetching',
183187
link: '/en/useRequest/plugins/fetchsing/',
184188
},
189+
{
190+
text: "broadcastChannel",
191+
link: "/en/useRequest/plugins/broadcastChannel/"
192+
}
185193
],
186194
},
187195
]

packages/hooks/docs/.vitepress/theme/home/DemoEditor.vue

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
import TypeIt from 'typeit'
1414
import { El } from 'typeit/dist/types'
1515
import { ref, onMounted, watch } from 'vue'
16-
17-
import useInViewport from '../../../../src/useInViewport'
16+
import { useInViewport } from 'vue-hooks-plus'
1817
1918
const domRef = ref(null)
2019
const isMount = ref(false)
@@ -27,12 +26,10 @@
2726
if (curr?.[0] && curr?.[1] && isFirst.value === true) {
2827
isFirst.value = false
2928
if (block.value)
29+
// @ts-ignore
3030
new TypeIt(block.value, {
3131
speed: 50,
3232
startDelay: 900,
33-
// afterStep: () => {
34-
// console.log(JSON.parse(JSON.stringify(block.value!.innerText.replace('|', ''))))
35-
// },
3633
})
3734
.type('<br><h1 style="opacity: 0.5;">Welcome use VueHooks Plus!</h1><br /><br />', {
3835
delay: 100,

packages/hooks/docs/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"compilerOptions": {
44
"lib": ["ES2016", "dom"],
55
"baseUrl": "./",
6-
"rootDir": "./",
6+
"rootDir": "/",
77
"paths": {
88
"vue-hooks-plus": ["../src/index.ts"]
99
}

packages/hooks/src/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import useRequest, {
2-
clearUseRequestCache,
3-
createUseRequest,
4-
useRequestProvider,
5-
} from './useRequest'
1+
import useRequest, { clearUseRequestCache, useRequestProvider } from './useRequest'
62
import useAsyncOrder from './useAsyncOrder'
73
import useBoolean from './useBoolean'
84
import useCookieState from './useCookieState'
@@ -55,7 +51,6 @@ export {
5551
useRequest,
5652
clearUseRequestCache,
5753
useRequestProvider,
58-
createUseRequest,
5954
useAsyncOrder,
6055
useBoolean,
6156
useCookieState,

packages/hooks/src/useRequest/createUseRequest.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/hooks/src/useRequest/docs/basic/demo/demo.vue

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
</template>
55

66
<script lang="ts" setup>
7-
import { useRequest } from 'vue-hooks-plus'
7+
import { useRequest } from 'vue-hooks-plus'
88
9-
function getUsername(params: { desc: string }): Promise<string> {
10-
return new Promise(resolve => {
11-
setTimeout(() => {
12-
resolve(`vue-hooks-plus ${params.desc}`)
13-
}, 1000)
14-
})
15-
}
9+
function getUsername(params: { desc: string }): Promise<string> {
10+
return new Promise(resolve => {
11+
setTimeout(() => {
12+
resolve(`vue-hooks-plus ${params.desc}`)
13+
}, 1000)
14+
})
15+
}
1616
17-
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }))
17+
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }))
1818
</script>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<template>
2+
<div>Current: {{ loading ? 'loading..' : data }}</div>
3+
4+
<div>
5+
<vhp-button @click="refresh()">Refresh</vhp-button>
6+
<div style="margin-top: 8px;">
7+
<h4>Desc</h4>
8+
<input v-model="input">
9+
<vhp-button style="margin-left: 4px;" @click="submit()">Send</vhp-button>
10+
</div>
11+
</div>
12+
<div>
13+
<h3>Channel History:</h3>
14+
<div v-for="(item, index) in other" :key="index">{{ item }}</div>
15+
</div>
16+
</template>
17+
18+
<script lang="ts" setup>
19+
import { ref } from 'vue'
20+
import { useRequest } from 'vue-hooks-plus'
21+
import { useBroadcastChannelPlugin } from '@vue-hooks-plus/use-request-plugins'
22+
23+
function getUsername(params: { desc: string }): Promise<string> {
24+
return new Promise(resolve => {
25+
setTimeout(() => {
26+
resolve(`${params.desc}-${String(Date.now())}`)
27+
}, 1000)
28+
})
29+
}
30+
31+
const other = ref<string[]>([])
32+
const input = ref('')
33+
const desc = ref('good')
34+
35+
const submit = () => {
36+
desc.value = `send-other-${input.value}`
37+
}
38+
39+
const { data, loading, refresh } = useRequest(
40+
() => getUsername({ desc: desc.value }),
41+
{
42+
refreshDeps: true,
43+
broadcastChannel: 'nice-broadcastChannel',
44+
onBroadcastChannel: (message: any, channel: any) => {
45+
other.value.push(message.data)
46+
},
47+
},
48+
[useBroadcastChannelPlugin],
49+
)
50+
</script>
51+
52+
<style scoped lang="less"></style>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /useRequest/plugins/broadcastChannel
5+
source:
6+
path: https://github.com/InhiblabCore/vue-hooks-plus/blob/master/packages/use-request-plugins/src/useBroadcastChannelPlugin/index.ts
7+
demoPath: https://github.com/InhiblabCore/vue-hooks-plus/blob/master/packages/hooks/src/useRequest/docs/plugins/broadcastChannel/demo/demo.vue
8+
---
9+
10+
# BroadcastChannel(Experimental)
11+
12+
> VERY IMPORTANT: This utility is currently in an experimental stage. This means that breaking changes will happen in minor AND patch releases. Use at your own risk. If you choose to rely on this in production in an experimental stage, please lock your version to a patch-level version to avoid unexpected breakages.
13+
14+
`useBroadcastChannelPlugin` is a utility for broadcasting and syncing the state of your useRequest State between browser tabs/windows with the same origin.
15+
16+
## Feature
17+
18+
- Act as the intermediate state for all requests, where users can operate on the collected request results.
19+
20+
## Install
21+
22+
```bash
23+
24+
# It is necessary to ensure that the application contains `broadcast-channel` .
25+
26+
1. npm i broadcast-channel
27+
28+
2. npm i @vue-hooks-plus/use-request-plugins
29+
30+
```
31+
32+
## Demo
33+
34+
<demo src="./demo/demo.vue"
35+
language="vue"
36+
title="Same origin cross window communication"
37+
desc="Opening the same new window, sending and refreshing data will result in data being transmitted across windows."> </demo>
38+
39+
## API
40+
41+
```typescript
42+
import { useRequest } from 'vue-hooks-plus'
43+
import { useBroadcastChannelPlugin } from '@vue-hooks-plus/use-request-plugins'
44+
45+
useRequest(
46+
service,
47+
{
48+
broadcastChannel?: string
49+
broadcastChannelKey?: string
50+
broadcastChannelOptions?: BroadcastChannelOptions
51+
customPostMessage?: (postMessage: (msg?: any) => Promise<void>, channel?: BroadcastChannel) => void
52+
onBroadcastChannel?: (value?: MessageType, channel?: BroadcastChannel, setFetchState?: Fetch<any, []>['setFetchState']) => void
53+
},
54+
[useBroadcastChannelPlugin],
55+
)
56+
```
57+
58+
## Options
59+
60+
| Property | Description | Type |
61+
| --- | --- | --- |
62+
| broadcastChannel | Required, the name of the channel, which must be the same as the channel name to receive the same frequency data. | `string` |
63+
| broadcastChannelKey | Non mandatory identifier used to distinguish different application types. | `string` |
64+
| broadcastChannelOptions | The configuration item for `broadcast-channel` has a default configuration of `{webWorkerSupport: false}` | `BroadcastChannelOptions` |
65+
| customPostMessage | User defined collection and sending of data. The default sending method is `type`, `data`, `param`, `error` | `(potMessage: (msg?: any)=>Promise<void>, channel?: BroadcastChannel) => void` |
66+
| onBroadcastChannel | Monitor and collect the same frequency data sent by other applications (including multiple windows of the same application) | `(value?: MessageType, channel?: BroadcastChannel, setFetchState?: Fetch<any, []>['setFetchState']) => void` |

0 commit comments

Comments
 (0)