GithubHelp home page GithubHelp logo

State Sharing And You about desktop HOT 9 CLOSED

desktop avatar desktop commented on May 21, 2024
State Sharing And You

from desktop.

Comments (9)

zeke avatar zeke commented on May 21, 2024

I don't have an opinion but I love the breakdown of pros and cons. Looking forward to seeing what others think.

from desktop.

zcbenz avatar zcbenz commented on May 21, 2024

Maybe storing global state in localStorage? Which is shared in all renderer processes.

from desktop.

joshaber avatar joshaber commented on May 21, 2024

@zcbenz Good point! I'd be concerned that if N renderers own the state, then it's equivalent to no one owning the state. For example, if we change something and need to broadcast it out to all the renderers, or coordinate some change, we still need something central doing that.

from desktop.

zeke avatar zeke commented on May 21, 2024

I had some trouble using localStorage because it's not persisted to disk until the app exits cleanly. So if the app crashes or you're working in development and ctrlc out, your state changes are lost.

https://github.com/sindresorhus/electron-config looks interesting. I haven't used it yet.

from desktop.

kevinsawicki avatar kevinsawicki commented on May 21, 2024

So will desktop have multiple windows, like one per repository perhaps?

If that is the case, then is the state we are talking here mostly preferences that apply to all windows, like user preferences and a few global app-wide events (like auto update)?

from desktop.

joshaber avatar joshaber commented on May 21, 2024

So will desktop have multiple windows, like one per repository perhaps?

Right.

If that is the case, then is the state we are talking here mostly preferences that apply to all windows, like user preferences and a few global app-wide events (like auto update)?

Right, or the lists of logged in users, tracked repositories, etc.

from desktop.

kevinsawicki avatar kevinsawicki commented on May 21, 2024

Right, or the lists of logged in users, tracked repositories, etc.

I think the background process (3) approach might work best for this.

One invisible window syncs and dispatches app-wide state changes and can easily be shown when debugging/profiling is needed.

IPC would (I think?) be two-step. Renderer -> main process -> background process.

Yeah, the messages would have go to through the the main process but you could do it all from the background process window.

A window can use the remote module to communicate with other windows "directly", messages still go through the main process but the code is initiated from the background window process making it easier to debug.

backgroundWindow.js

const {getCurrentWindow, BrowserWindow} = require('electron').remote

console.log(`Starting refresh of repositories at ${Date()}`)

refreshRepos().then((repos) => {
  // Assign to global for debugging
  window.latestRepos = repos
  console.log(`Repos refreshed at ${Date()}`)

  // Notify all windows of new repo state
  BrowserWindow.getAllWindows().forEach((window) => {
    // Send IPC event directly to renderer process remotely
    window.webContents.send('repos-refreshed', repos)
  })
}).catch((error) => {
  console.error(`Repos refresh failed at ${Date()}`)
  console.error(error)
  if (process.env.NODE_ENV === 'development') {
    // Show background window on errors when developing
    getCurrentWindow().show()
    getCurrentWindow().openDevTools()
  }
})

window1.js, window2.js, ...

const {ipcRenderer} = require('electron')
ipcRenderer.on('repos-refreshed', (repos) => {
  reposView.setState(repos)
})

from desktop.

joshaber avatar joshaber commented on May 21, 2024

Yeah, the messages would have go to through the the main process but you could do it all from the background process window.

Ah yeah, great call πŸ‘

from desktop.

joshaber avatar joshaber commented on May 21, 2024

Thanks for the input everyone πŸ’–

Gonna continue this discussion around some Real Code in #97.

from desktop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.