Skip to content

Commit e6b3ec7

Browse files
committed
update
1 parent ba9402a commit e6b3ec7

File tree

9 files changed

+98
-72
lines changed

9 files changed

+98
-72
lines changed

resources/lib/js/sdk.mjs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,59 @@
1-
import { reactive as r, watch as p } from "vue";
2-
async function d(n) {
1+
import { reactive as t, watch as u } from "vue";
2+
async function p(n) {
33
return window.parent.electron.ipcRenderer.invoke("ipc-show-open-dialog", n);
44
}
5-
async function w(n) {
5+
async function d(n) {
66
return window.parent.electron.ipcRenderer.invoke("ipc-show-save-dialog", n);
77
}
8-
const o = r(window.$wujie?.props?.modelValue ?? {});
9-
p(o, (n) => {
10-
window.$wujie.bus.$emit("update:modelValue", {
11-
pluginId: window.$wujie?.props?.pluginId,
12-
id: window.$wujie?.props?.editIndex,
13-
data: n
14-
});
8+
let o = !1;
9+
const r = t(window.$wujie?.props?.dataStore ?? {});
10+
u(
11+
r,
12+
(n) => {
13+
if (o) {
14+
o = !1;
15+
return;
16+
}
17+
window.$wujie?.bus && (console.log("pluginEmit"), window.$wujie.bus.$emit("update:dataStore", n));
18+
},
19+
{ deep: !0 }
20+
);
21+
window.$wujie?.bus && window.$wujie.bus.$on("update:dataStore:fromMain", (n) => {
22+
o = !0, Object.assign(r, n || {});
1523
});
16-
function t() {
17-
return o;
24+
function a() {
25+
return r;
1826
}
1927
const i = window.parent.logBus;
20-
function l(n, ...e) {
28+
function s(n, ...e) {
2129
return window.parent.electron.ipcRenderer.invoke(
2230
"ipc-plugin-exec",
2331
{ pluginId: window.$wujie?.props?.pluginId, id: window.$wujie?.props?.editIndex },
2432
n,
2533
...e
2634
);
2735
}
28-
function a(n, e) {
36+
function l(n, e) {
2937
i.on(`pluginEvent.${window.$wujie?.props?.pluginId}.${n}`, e);
3038
}
31-
function s(n, e) {
39+
function c(n, e) {
3240
i.off(`pluginEvent.${window.$wujie?.props?.pluginId}.${n}`, e);
3341
}
34-
function c(n) {
42+
function g(n) {
3543
i.on(`pluginError.${window.$wujie?.props?.pluginId}`, n);
3644
}
37-
function g(n) {
45+
function f(n) {
3846
i.off(`*pluginError.${window.$wujie?.props?.pluginId}`, n);
3947
}
4048
export {
41-
c as addPluginErrorListen,
42-
a as addPluginEventListen,
43-
l as callServerMethod,
49+
g as addPluginErrorListen,
50+
l as addPluginEventListen,
51+
s as callServerMethod,
4452
i as eventBus,
45-
g as removePluginErrorListen,
46-
s as removePluginEventListen,
47-
d as showOpenDialog,
48-
w as showSaveDialog,
49-
t as useData
53+
f as removePluginErrorListen,
54+
c as removePluginEventListen,
55+
p as showOpenDialog,
56+
d as showSaveDialog,
57+
a as useData
5058
};
5159
//# sourceMappingURL=index.mjs.map

src/main/plugin-sdk/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ export function registerService<K extends keyof ServiceMap>(name: K, func: Servi
2929
export function emitEvent(name: string, data: any) {
3030
if (!isMainThread) {
3131
workerpool.workerEmit({
32-
event: name,
33-
data: data
32+
event: 'pluginEvent',
33+
data: {
34+
name,
35+
data
36+
}
3437
})
3538
}
3639
}

src/main/plugin-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ecubus-pro/main-plugin-sdk",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "EcuBus Pro main plugin SDK",
55
"type": "commonjs",
66
"main": "./dist/index.cjs",

src/main/pluginCilent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ export default class PluginClient {
3333
pluginPath,
3434
name
3535
)
36+
this.nodeItem.pool?.registerHandler('pluginEvent', this.eventHandler.bind(this))
3637
}
3738
}
3839

39-
eventHandler(payload: any) {
40-
const event = payload.event
40+
eventHandler(payload: { name: string; data: any }) {
41+
const name = payload.name
4142
const data = payload.data
42-
this.log.pluginEvent(event, data)
43+
this.log.pluginEvent(name, data)
4344
}
4445

4546
async exec(method: string, ...params: any[]): Promise<any> {

src/main/workerClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type HandlerMap = {
3131
linApi: (data: linApiStartSch | linApiStopSch) => void
3232
pwmApi: (data: pwmApiSetDuty) => void
3333
canApi: (data: any) => void
34+
pluginEvent: (data: { name: string; data: any }) => void
3435
}
3536
export type pwmApiSetDuty = {
3637
method: 'setDuty'

src/renderer/src/App.vue

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
<script setup lang="ts">
1818
import HeaderView from '@r/views/header/header.vue'
19-
import { onMounted, onUnmounted, ref, watch } from 'vue'
19+
import { onMounted, onUnmounted, ref, watch, nextTick } from 'vue'
2020
import { ElMessage, ElNotification } from 'element-plus'
21-
import { useDataStore } from './stores/data'
21+
import { DataSet, useDataStore } from './stores/data'
2222
import { useProjectStore } from './stores/project'
2323
import { useWindowSize } from '@vueuse/core'
2424
import { useGlobalStart } from './stores/runtime'
@@ -27,6 +27,7 @@ import { useDark } from '@vueuse/core'
2727
import { VxeUI } from 'vxe-table'
2828
import { bus } from 'wujie'
2929
import log from 'electron-log'
30+
import { cloneDeep } from 'lodash'
3031
const data = useDataStore()
3132
const project = useProjectStore()
3233
const pluginStore = usePluginStore()
@@ -35,29 +36,25 @@ const globalStart = useGlobalStart()
3536
const isDark = useDark()
3637
const params = ref<any>({})
3738
38-
bus.$on(
39-
'update:modelValue',
40-
({ pluginId, id, data: val }: { pluginId: string; id: string; data: any }) => {
41-
log.info('plugin data update', {
42-
pluginId,
43-
id,
44-
val
45-
})
46-
if (pluginId == id) {
47-
//single data
48-
data.pluginData[pluginId] = val
49-
} else {
50-
//multi data
51-
if (data.pluginData[pluginId]) {
52-
data.pluginData[pluginId][id] = val
53-
} else {
54-
data.pluginData[pluginId] = {
55-
[id]: val
56-
}
57-
}
58-
}
39+
let isExternalUpdate = false
40+
// 监听插件对 store 的修改,同步到主应用
41+
bus.$on('update:dataStore', (newStore: DataSet) => {
42+
// 使用 $patch 批量更新 store(Pinia 推荐方式)
43+
isExternalUpdate = true
44+
data.$patch(() => {
45+
return newStore
46+
})
47+
})
48+
49+
// 监听主应用 store 的变化,同步到插件
50+
data.$subscribe((mutation, state) => {
51+
if (isExternalUpdate) {
52+
isExternalUpdate = false
53+
return
5954
}
60-
)
55+
console.log('mainEmit')
56+
bus.$emit('update:dataStore:fromMain', cloneDeep(data.getData()))
57+
})
6158
6259
// Watch for dark theme changes
6360
watch(isDark, (value) => {

src/renderer/src/plugin-sdk/index.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { DataSet } from 'src/preload/data'
23
import { reactive, watch } from 'vue'
4+
import { isEqual } from 'lodash'
35
export { showOpenDialog, showSaveDialog } from './dialog'
46

5-
// ============ 插件数据和方法 ============
7+
// ============ 模拟 Pinia 的 useDataStore ============
8+
let isExternalUpdate = false
9+
// 创建响应式的 dataStore(模拟 Pinia store)
10+
const dataStore = reactive<DataSet>(window.$wujie?.props?.dataStore ?? {})
611

7-
const data = reactive<any>(window.$wujie?.props?.modelValue ?? {})
12+
// 监听插件对 store 的修改,同步到主应用
13+
watch(
14+
dataStore,
15+
(newVal: any) => {
16+
if (isExternalUpdate) {
17+
isExternalUpdate = false
18+
return
19+
}
20+
if (window.$wujie?.bus) {
21+
console.log('pluginEmit')
22+
window.$wujie.bus.$emit('update:dataStore', newVal)
23+
}
24+
},
25+
{ deep: true }
26+
)
827

9-
watch(data, (newVal: any) => {
10-
window.$wujie.bus.$emit('update:modelValue', {
11-
pluginId: window.$wujie?.props?.pluginId,
12-
id: window.$wujie?.props?.editIndex,
13-
data: newVal
28+
// 监听主应用发送的 store 更新事件
29+
if (window.$wujie?.bus) {
30+
window.$wujie.bus.$on('update:dataStore:fromMain', (newStore: any) => {
31+
// 使用 isEqual 比较,只有真正变化时才更新,避免不必要的 watch 触发
32+
isExternalUpdate = true
33+
Object.assign(dataStore, newStore || {})
1434
})
15-
})
35+
}
1636

37+
// 模拟 Pinia 的 useDataStore,返回整个 dataSet
1738
export function useData() {
18-
return data
39+
return dataStore
1940
}
41+
2042
export const eventBus = window.parent.logBus
2143

2244
export function callServerMethod(method: string, ...params: any[]): Promise<any> {

src/renderer/src/plugin-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ecubus-pro/renderer-plugin-sdk",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "EcuBus Pro renderer plugin SDK",
55
"type": "module",
66
"module": "./dist/index.mjs",

src/renderer/src/plugin/plugin.vue

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:url="entry"
77
:fetch="isDev ? undefined : customFetch"
88
:plugins="plugins"
9-
:props="{ ...props, modelValue: data, isDark: darkValue }"
9+
:props="{ ...props, isDark: darkValue, dataStore: cloneDeep(dataStore.getData()) }"
1010
:load-error="loadError"
1111
></WujieVue>
1212
</div>
@@ -31,12 +31,6 @@ const props = defineProps<{
3131
height: number
3232
}>()
3333
34-
const data = cloneDeep(
35-
props.editIndex == props.pluginId
36-
? dataStore.pluginData[props.pluginId]
37-
: dataStore.pluginData[props.pluginId][props.editIndex]
38-
)
39-
4034
const isDev = props.item.entry?.startsWith('http')
4135
4236
const entry = isDev ? props.item.entry : `file:///${props.item.entry}`

0 commit comments

Comments
 (0)