electron-sync-state
Version:
`electron-sync-state` 是一个用于在 Electron 主进程和渲染进程之间自动同步状态的库。它利用了 Vue 的响应式系统,以及 Vue 3.5 版本提供的新 API,确保状态在进程间保持一致。该库适用于 Vue 以及 @vue/reactivity 3.5 及以上版本。
56 lines (55 loc) • 1.34 kB
JavaScript
import { ipcMain as T, BrowserWindow as f } from "electron";
import { ref as p, watch as d } from "@vue/reactivity";
var a = /* @__PURE__ */ ((e) => (e.GET = "GET", e.SET = "SET", e))(a || {}), i = /* @__PURE__ */ ((e) => (e.SET = "SET", e))(i || {});
function h(e) {
return {
[a.GET]: `${e}-${a.GET}`,
[a.SET]: `${e}-${a.SET}`
};
}
function m(e) {
return {
[i.SET]: `${e}-${i.SET}`
};
}
function u(e) {
return JSON.parse(JSON.stringify(e));
}
const $ = (e, t) => {
const l = t.debug !== void 0, o = p(e), E = h(t.channelPrefix), c = m(t.channelPrefix);
T.on(E.GET, (r) => {
r.reply(c.SET, u(o.value));
});
let n;
const S = () => {
n = d(
() => o.value,
(r) => {
l && console.log("send change to renderer", r), f.getAllWindows().forEach((s) => {
s.webContents.send(c.SET, u(r));
});
},
{
deep: !0
}
);
};
return S(), T.on(E.SET, (r, s) => {
l && console.log("change from renderer", s), n && (n.stop(), n = void 0), o.value = s, S();
}), d(() => o.value, (r) => {
t.onChange && t.onChange(u(r));
}), {
state: o,
syncWatchHandle: {
pause: () => {
n == null || n.pause();
},
resume: () => {
n == null || n.resume();
}
}
};
};
export {
$ as createRefSyncState
};