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

@briosheje/ipc-reactive-state-electron

v0.0.3

Published

This library can be used in conjuction with a compatible frontend adapter (check the last section of this readme) to implement a one-way or two-way synced state between the electron main process and a react/vue application hosted in any renderer process(e

Downloads

3

Readme

ipc-reactive-state-electron

This library can be used in conjuction with a compatible frontend adapter (check the last section of this readme) to implement a one-way or two-way synced state between the electron main process and a react/vue application hosted in any renderer process(es).

Installation

Install the library using:

npm i @briosheje/ipc-reactive-state-electron --save

Usage & Getting started

There are two steps needed to use the library:

  • Mounting the preload event handlers and DOM augmentation.
  • Creating a state (or more than one state).

Mounting on preload

Add this in your preload.js or preload.ts file:

import { mountReactiveStateListeners } from "@briosheje/ipc-reactive-state-electron"
// or const { mountReactiveStateListeners } = require("@briosheje/ipc-reactive-state-electron")

mountReactiveStateListeners();

Create a reactive state in the main process

Example with typescript

import { createReactiveIpcState } from '@briosheje/ipc-reactive-state-electron';

interface MyState = {
  name: string;
  lastName: string;
  age: number;
}

const stateKey = 'my-state';

const initialState: MyState = {
  name: 'Hello',
  lastName: 'World',
  age: 30
};
const { state } = createReactiveIpcState<MyState>(
  stateKey, initialState
);

Then, whenever you will change a property to the state object, like:

state.name = 'Helloooooo';

The change will be automatically propagated in electron after 50 milliseconds. You can change the amount of milliseconds needed to sync the state with the third option of the createReactiveIpcState function, by providing an object and specifying the desired throttle.

The throttle was implemented so that sequential changes are batched. As an example, if you write this:

state.name = 'Helloooooo';
state.lastName = 'wooooorld';
state.age = 10

There will only be one IPC message sent from the main to the renderer processes.

How to listen to state changes

The createReactiveIpcState function returns the state and an EventEmitter. Such EventEmitter is typed out of box for typescript users and allow to intercept changes to the state from the client side.

Example:

import { createReactiveIpcState } from '@briosheje/ipc-reactive-state-electron';

interface MyState = {
  name: string;
  lastName: string;
  age: number;
}

const stateKey = 'my-state';

const initialState: MyState = {
  name: 'Hello',
  lastName: 'World',
  age: 30
};
const { state, events } = createReactiveIpcState<MyState>(
  stateKey, initialState
);

events.on('onStateUpdateRequestReceived', (s) => {
  console.log('client requested to update the state.')
})

events.on('onStateUpdateSentToClient', (s) => {
  console.log('A change propagated in the main process was sent to renderer processes.')
})

events.on('onStateUpdated', (s) => {
  console.log('A change requested to update the state was applied to the state.')
})

How do I use the library on the client side?

You can use one of the adapters available:

  • Vanilla: coming soon
  • Vue: @briosheje/ipc-reactive-state-vue
  • React: @briosheje/ipc-reactive-state-angular
  • Solid JS: coming soon
  • Svelte: coming soon
  • Angular: coming soon
  • Qwik: coming soon