UNPKG

electron-sync-state

Version:

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

2 lines (1 loc) 1 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("electron"),i=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 v=(e,t)=>{const S=t.debug!==void 0,a=i.ref(e),E=f(t.channelPrefix),T=p(t.channelPrefix);c.ipcMain.on(E.GET,r=>{r.reply(T.SET,u(a.value))});let n;const d=()=>{n=i.watch(()=>a.value,r=>{S&&console.log("send change to renderer",r),c.BrowserWindow.getAllWindows().forEach(s=>{s.webContents.send(T.SET,u(r))})},{deep:!0})};return d(),c.ipcMain.on(E.SET,(r,s)=>{S&&console.log("change from renderer",s),n&&(n.stop(),n=void 0),a.value=s,d()}),i.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=v;