Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
chore: broadcast sync project data
Browse files Browse the repository at this point in the history
  • Loading branch information
hibig committed Aug 2, 2023
1 parent 2a56e54 commit dc7b833
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/components/menu/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { PROJECT } from '@/router/config';
import { defineComponent, ref, h, compile, computed } from 'vue';
import useCallCommon from '@/hooks/use-call-common';
import BC from '@/hooks/broadcast-channel';
import { RouteRecordRaw, RouteRecordNormalized } from 'vue-router';
import {
useAppVersion,
Expand Down Expand Up @@ -46,6 +47,15 @@
// listener to route change
execListenerRouteChange();
// sync the project data between tabs
BC.onmessage((message) => {
if (message.type === 'project') {
const { projectList } = message.data;
projectStore.setInfo({
projectList
});
}
});
const collapsed = computed({
get() {
if (appStore.device === 'desktop') return appStore.menuCollapse;
Expand Down Expand Up @@ -237,7 +247,6 @@
const isReplace: any = item.meta?.replace;
tabBarStore.clearTags();
if (item.name === PROJECT.List) {
await getProjectList();
goToProject(item);
return;
}
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/broadcast-channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const symbol = 'broadcast-channel';

const BC = new BroadcastChannel(symbol);

export default {
postMessage: (message: any) => {
BC.postMessage(message);
},
onmessage: (handler: (message: any) => void) => {
BC.onmessage = (event) => {
handler(event.data);
};
}
};
7 changes: 7 additions & 0 deletions src/store/modules/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { clone, cloneDeep } from 'lodash';
import { defineStore } from 'pinia';
import BC from '@/hooks/broadcast-channel';

const useProjectStore = defineStore('project', {
persist: {
Expand Down Expand Up @@ -30,6 +31,12 @@ const useProjectStore = defineStore('project', {
this.setInfo({
projectList
});
BC.postMessage({
type: 'project',
data: {
projectList
}
});
}
}
});
Expand Down

0 comments on commit dc7b833

Please sign in to comment.