UNPKG

electron-sync-state

Version:

`electron-sync-state` 是一个用于在 Electron 主进程和渲染进程之间自动同步状态的库。它利用了 Vue 的响应式系统,以及 Vue 3.5 版本提供的新 API,确保状态在进程间保持一致。该库适用于 Vue 以及 @vue/reactivity 3.5 及以上版本。

51 lines (50 loc) 1.18 kB
import { reactive as S, watch as d } from "vue"; var t = /* @__PURE__ */ ((e) => (e.GET = "GET", e.SET = "SET", e))(t || {}), i = /* @__PURE__ */ ((e) => (e.SET = "SET", e))(i || {}); function T(e) { return { [t.GET]: `${e}-${t.GET}`, [t.SET]: `${e}-${t.SET}` }; } function f(e) { return { [i.SET]: `${e}-${i.SET}` }; } function p(e) { return JSON.parse(JSON.stringify(e)); } const m = (e, o) => { const c = e.debug !== void 0, r = S({ loading: !0, state: void 0 }), u = T(e.channelPrefix), E = f(e.channelPrefix); let n; const l = () => { n = d( () => r.state, (s) => { c && console.log("send change to main", s), o.send(u.SET, p(s)); }, { deep: !0 } ); }; return l(), o.on(E.SET, async (s, a) => { c && console.log("change from main", a), n && (n.stop(), n = void 0), console.log("ipcRenderer.on", a), r.state = a, l(), r.loading = !1; }), o.send(u.GET), { wrapperState: r, syncWatchHandle: { pause: () => { n == null || n.pause(); }, resume: () => { n == null || n.resume(); } } }; }; export { m as useSyncState };