UNPKG

electron-sync-state

Version:

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

2 lines (1 loc) 1.04 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("electron"),c=require("@vue/reactivity");var o=(e=>(e.GET="GET",e.SET="SET",e))(o||{}),l=(e=>(e.SET="SET",e))(l||{});function f(e){return{[o.GET]:`${e}-${o.GET}`,[o.SET]:`${e}-${o.SET}`}}function p(e){return{[l.SET]:`${e}-${l.SET}`}}function u(e){return JSON.parse(JSON.stringify(e))}const g=(e,t)=>{const S=t.debug===void 0?!1:!!t.debug,a=c.ref(e),E=f(t.channelPrefix),d=p(t.channelPrefix);s.ipcMain.on(E.GET,r=>{r.reply(d.SET,u(a.value))});let n;const T=()=>{n=c.watch(()=>a.value,r=>{S&&console.log("send change to renderer",JSON.stringify(r)),s.BrowserWindow.getAllWindows().forEach(i=>{i.webContents.send(d.SET,u(r))})},{deep:!0})};return T(),s.ipcMain.on(E.SET,(r,i)=>{S&&console.log("change from renderer",JSON.stringify(i)),n&&(n.stop(),n=void 0),a.value=i,T()}),c.watch(()=>a.value,r=>{t.onChange&&t.onChange(u(r))}),{state:a,syncWatchHandle:{pause:()=>{n==null||n.pause()},resume:()=>{n==null||n.resume()}}}};exports.createRefSyncState=g;