Skip to content

ArcherGu/einf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6253d7d Β· Aug 29, 2022

History

27 Commits
Aug 16, 2022
Aug 16, 2022
Aug 29, 2022
Aug 23, 2022
Aug 16, 2022
Aug 16, 2022
Aug 16, 2022
Aug 16, 2022
Aug 16, 2022
Aug 23, 2022
Aug 27, 2022
Aug 27, 2022
Aug 16, 2022
Aug 16, 2022
Aug 16, 2022
Aug 16, 2022

Repository files navigation

Einf logo

A simple electron main process framework.

Description

Einf is a simple electron main process framework, which provides some decorators and automatic dependency injection to help you simplify the main process code.

Features

  • πŸ’‰β€ Support dependency injection powered by Typescript decorators.

  • πŸͺŸ Support custom items injection and window object injection.

  • πŸ”— Automatic ipc event binding to reduce duplication of code.

  • πŸ“¦ Tiny size, the whole framework is less than 10kb.

  • πŸ’‘ Simple to use, you can use it as a base framework to build your own framework.

Installation

npm i einf
# Or Yarn
yarn add einf
# Or PNPM
pnpm add einf

Usage

Entry point of your electron application like index.ts:

import { BrowserWindow, app } from 'electron'
import { createEinf } from 'einf'
import { AppController } from './app.controller'

async function bootstrap() {
  const window = new BrowserWindow()
  window.loadURL('https://github.com')

  await createEinf({
    // window to create
    window,
    // controllers will be automatically initialized
    controllers: [AppController],
    // custom items to inject
    injects: [{
      name: 'IS_DEV',
      inject: !app.isPackaged,
    }],
  })
}

bootstrap()

Provide at least one controller to start the application, app.controller.ts:

import type { BrowserWindow } from 'electron'
import { app } from 'electron'
import { Controller, Inject, IpcHandle, IpcSend, Window } from 'einf'
import type { AppService } from './app.service'

@Controller()
export class AppController {
  constructor(
    private appService: AppService,
    @Inject('IS_DEV') private isDev: boolean,
    @Window() private win: BrowserWindow,
  ) {}

  @IpcSend('reply-msg')
  public replyMsg(msg: string) {
    return msg
  }

  @IpcHandle('send-msg')
  public sendMsg(msg: string) {
    console.log(msg)
    return 'Get msg'
  }

  @IpcHandle('exit')
  public exit() {
    app.quit()
  }
}

You can also inject service via @Injectable decorator, app.service.ts:

import { Injectable } from 'einf'

@Injectable()
export class AppService {
  public createMsg(msg: string): string {
    return `"${msg}" is created by app service`
  }
}

License

MIT License Β© 2022 Archer Gu