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-state

v0.5.9

Published

React-like IPC backed state objects for Electron.

Downloads

219

Readme

Electron State

React-like IPC backed state objects for Electron.

Example

You can run this example yourself! Just run yarn start in this repo to play with this dummy login flow. The files live in this repo at /test/app.

UserState.ts

import { ElectronState, main, renderer, State } from 'electron-state';

// Extend the `ElectronState` base class to create a new IPC based shared memory model.
export default class UserState extends ElectronState {

  // Properties declared on your model define the interface of this state object.
  isLoggedIn: boolean = false;
  firstName: string | null = null;
  lastName: string | null = null;
  email: string | null = null;

  // The `@main` decorator forces async methods to run in Electron's main process.
  @main static async logIn(email: string, password: string): Promise<boolean> {
    // If you need code that can / should only execute in one process, make sure you import it only as needed.
    const { db } = await import('database');

    const user = await db.users.getByEmail(email);

    if (user?.password !== password) { return false; }

    // Use `ElectronState.setState()` to modify state objects.
    UserState.setState({
      isLoggedIn: true,
      firstName: user.firstName,
      lastName: user.lastName,
      email: user.email,
    });

    return true;
  }

  // The `@renderer` decorator forces async methods to run in Electron's renderer process.
  @renderer static async logOut(): Promise<void> {
    // Access state data at any time by calling `ElectronState.toJSON()`
    const data: State<UserState> = UserState.toJSON();

    if (!data.isLoggedIn) { return; }

    // Use `ElectronState.setState()` to modify state objects.
    UserState.setState({
      isLoggedIn: false,
      firstName: null,
      lastName: null,
      email: null,
    });

    alert("You've been logged out!");
  }
}

main.ts

import { app, BrowserWindow } from 'electron';

// You can import your custom `ElectronState` models into the main process.
import UserState from './UserState';

// The main process can listen for changes to the state object. It is passed a copy of the state data.
UserState.onChange((user) => {
  if (user.isLoggedIn) {
    console.log(`${user.firstName} ${user.lastName} has logged in. Auto logging out in 10s.`);

    // As defined in our UserState class, the `UserState.logOut()` method will always be run in the renderer process.
    setTimeout(() => UserState.logOut(), 10000);
  }
});

app.whenReady().then(() => {
  const win = new BrowserWindow({ width: 800, height: 600 });
  win.loadFile('/static/index.html'));
});

app.tsx

import * as React from 'react';
import * as ReactDOM from 'react-dom';

// ElectronState delivers a React hook for your convenience.
import { useElectronState } from 'electron-state/hooks';

// You can import your custom `ElectronState` models into the renderer process.
import UserState from './UserState';

async function handleSubmit(evt: React.FormEvent<HTMLFormElement>) {
  evt.preventDefault();
  const email = (evt.currentTarget.elements.namedItem('email') as HTMLInputElement)?.value || null;
  const password = (evt.currentTarget as HTMLFormElement.elements.namedItem('password') as HTMLInputElement)?.value || null;
  if (!email || !password) {
    alert('Please provide your email and password');
    return;
  }

  // As defined in our UserState class, the `UserState.logIn` method will always be run in the main process.
  const logInSuccess = await UserState.logIn(email, password);
  alert(logInSuccess ? 'Successfully logged in!' : 'Incorrect login information.');
}

function App() {
  // ElectronState delivers a React hook for your convenience.
  const [ user, setUser ] = useElectronState(UserState);

  if (user.isLoggedIn) {
    return <section>
      <h1>{user.firstName} {user.lastName} is Logged In</h1>
      <button onClick={() => setUser({ isLoggedIn: false })}>Log Out</button>
    </section>;
  }

  return <form onSubmit={handleSubmit}>
    <input type="email" name="email" />
    <input type="password" name="password" />
    <button type="submit">Log In</button>
  </form>;
}

ReactDOM.render(<App />, document.body);