如何在JavaScript中构建Electron桌面应用程序:多线程,SQLite,本机模块和其他常见痛点...

本文介绍了如何在JavaScript中使用Electron开发桌面应用,涉及多线程、SQLite数据库的使用以及本机模块的打包。文章详细探讨了Web Workers、分叉新进程和渲染器进程作为工作线程的优缺点,以及在 Electron 应用中处理SQLite数据库的策略。此外,还讨论了打包时遇到的挑战,如文件路径问题和本机模块的兼容性问题。
摘要由CSDN通过智能技术生成

by Andrew Walsh

通过安德鲁·沃尔什(Andrew Walsh)

如何在JavaScript中构建Electron桌面应用程序:多线程,SQLite,本机模块和其他常见痛点 (How to Build an Electron Desktop App in JavaScript: Multithreading, SQLite, Native Modules, and other Common Pain Points)

As a framework for developing desktop applications, Electron has a lot to offer. It grants full access to Node’s API and ecosphere. It deploys on all major operating systems (with a single codebase). And with its web-based architecture, you can use the latest features of CSS to create advanced UIs.

作为开发桌面应用程序的框架,Electron提供了很多东西。 它授予对Node的API和ecosphere的完全访问权限。 它部署在所有主要操作系统上(具有单个代码库)。 借助其基于Web的体系结构,您可以使用CSS的最新功能来创建高级UI。

There are a lot of articles addressing getting up and running with Electron, but fewer dedicated to using SQLite or how to go about multithreading. We’ll look at how to use Electron to build applications that handle large amounts of data or run lots of tasks.

有很多关于如何使用Electron进行启动和运行的文章,但很少有专门用于使用SQLite或如何进行多线程的文章。 我们将研究如何使用Electron来构建处理大量数据或运行大量任务的应用程序。

In particular, we’ll cover:

特别是,我们将介绍:

  • How Electron works (in brief), and how its architecture affects what we can do

    电子的工作原理(简而言之),以及其架构如何影响我们的工作
  • Multithreading

    多线程
  • Using local databases such as SQLite, or writing to any file inside an Electron app

    使用SQLite等本地数据库,或写入Electron应用程序内的任何文件
  • Native modules

    本机模块
  • A few gotchas to be aware of

    一些需要注意的陷阱
  • Packaging an application using native modules

    使用本机模块打包应用程序

电子的工作原理-简略 (How Electron works — abridged)

It’s worth repeating the key principles behind Electron’s architecture. An Electron application consists of at least two processes. The main thread is the entryway to your application and does all the work necessary to show your renderer process (or processes) to your users. There can only ever be one instance of the main process.

值得重复Electron架构背后的关键原理。 电子应用程序至少包含两个过程。 主线程是应用程序的入口,它执行所有必要的工作以向用户显示渲染器进程。 主流程只能有一个实例。

Renderer processes use Chromium to render your app. Just as each tab runs in its own process, so too does each renderer. They are loaded using the BrowserWindow constructor’s loadURL method, which needs to point to a local or remote HTML file. That means that the only way to start up a renderer process is to use an HTML file as an entry.

渲染器进程使用Chromium渲染您的应用程序。 正如每个选项卡在其自己的进程中运行一样,每个渲染器也是如此。 它们是使用BrowserWindow构造函数的loadURL方法加载的,该方法需要指向本地或远程HTML文件。 这意味着启动渲染器进程的唯一方法是使用HTML文件作为条目。

电子架构的注意事项 (Caveats of Electron’s architecture)

The simplicity of Electron is one of its greatest assets. Your main process does any configuration necessary then passes a HTML file or URL to the renderer process. This file can do anything that a regular web application can — and you’re good to go from there.

电子的简单性是其最大的资产之一。 您的主进程进行必要的配置,然后将HTML文件或URL传递给渲染器进程。 该文件可以执行常规Web应用程序可以执行的任何操作-您可以从那里开始。

But the fact that there can only be one main process makes it unclear on how to implement multithreading. Electron’s documentation implies that renderer processes are strictly designed for the task of rendering UIs (which as we’ll see, isn’t true).

但是只有一个主进程的事实使得不清楚如何实现多线程。 Electron的文档表明,渲染器进程是严格设计用于渲染UI的任务(我们将看到,这是不正确的)。

It’s important to know that doing anything computationally intensive on the main process will slow down (or freeze) your renderer processes. It’s critical that any computationally intensive work is moved off the main thread. It’s

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值