npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-fusion

v1.0.19

Published

Tool for fusing data between the main and renderer processes

Downloads

27

Readme

Electron Fusion

A laconic solution for synchronizing data between the main and renderer processes of Electron

Powered by JS Proxy. Designed for TypeScript developers

Performance

This package was inspired by the reactivity of vue. Objects stored in memory are not proxies, but become proxies only when they are accessed

The package sends to the ipcRenderer only those data that have been changed, trying to minimize the information

Also, all sync commands are not sent immediately to ipcMain, but are collected in batching and sent to nextTick

Installation

yarn add electron-fusion

Structure

Code splited on 3 parts: Main, Renderer and Preload. Let's see how to use them

Main

electron-fusion/main containts methods for main process. Here you can declare the sync data that will be synchronized with the renderer

How to use

import { syncMain } from "electron-fusion/main"

class CounterData {
  value = 1
}

const counterData = syncMain(new CounterData(), [ "counter" ])
export { counterData, CounterData }

Also in main you should declare objects whose methods can be used from renderer

How to use

import { proxyMethods } from 'electron-fusion/main'
import { counterData } from '../data/counter'

class CounterService {
  
  constructor() {
    proxyMethods(this, "counter")
  }

  increment() {
    counterData.value++
  }
}

export const counterService = new CounterService()

Preload

For synchronization to be available, you must define the necessary fields in the preloader

Note: If you are not using a package builder like vite or webpack, you will not be able to import into preload. In this case, instead of importing, just copy the code from dist/preload.js into your preload.js

How to use

import { bridge, proxy } from 'electron-fusion/preload'

const electronBridge = {
  ...bridge,
  counter: counter: proxy("counter", {} as typeof counterService, [ "increment" ], [], []))
}

contextBridge.exposeInMainWorld('electron', electronBridge)

P.S. Sorry about the dummy second argument hack. It is necessary because typescript does not allow to make partial generic types

The beauty is that you can also use the electronBridge type from here, and drop the necessary types into renderer:

export type electronAPI = typeof electronBridge
import type { electronAPI } from '../../../main/preload'

declare global {
  interface Window { // eslint-disable-line no-unused-vars
    electron: electronAPI
  }
}

export {}

Renderer

As you noticed in the previous step, you now have a window.electron object with defined methods. You can use them as normal methods and not worry.

Now let's deal with the data. You need to get the synchronized data from the main. It doesn't matter how many windows are open - the data is synchronized in each window.

How to use

import { syncRenderer } from 'electron-fusion/renderer'
import { reactive } from 'vue'

const counterData = syncRenderer('counter', (obj: CounterData) => reactive(obj))

Now you have a reactive counterData object, which immediately changes following the changes in main

React + Mobx

If you use React and Mobx, you know that all observable mutations must take place in action. Here this is provided: to do this you need to call setActionWrapper

import { syncRenderer, setActionWrapper } from 'electron-fusion/renderer'
import { observable, action } from 'mobx'

setActionWrapper(action)

const counterData = syncRenderer('counter', (obj: CounterData) => observable(obj))

Goals

This package has only one goal - to get rid of a lot of unnecessary routine that developers have to write. Using this package, you will most likely forget the lines of code with ipcMain and ipcRenderer like a bad dream