You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/async-sqlite/README.md
+4-11Lines changed: 4 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ The Async SQLite example implements a simple database in Rust with a JavaScript
5
5
## Usage
6
6
7
7
```js
8
-
constDatabase=require(".");
8
+
const{ Database }=require(".");
9
9
10
10
(async () => {
11
11
constdb=newDatabase();
@@ -31,25 +31,18 @@ Since SQLite is naturally single threaded, our application does not benefit from
31
31
32
32
Once the database thread is spawned, the JavaScript main thread needs a way to communicate with it. A [multi-producer, single-consumer (mpsc)][mpsc] channel is created. The receiving end is owned by the database thread and the sender is held by JavaScript.
33
33
34
-
#### `JsBox`
34
+
#### `#[neon::export(class)]`
35
35
36
-
Rust data cannot be directly held by JavaScript. The [`JsBox`][jsbox] provides a mechanism for allowing JavaScript to hold a reference to Rust data and later access it again from Rust.
36
+
The Rust sender side of the channel is held in a JavaScript[class][class]. It can be referenced later in methods.
37
37
38
38
#### Rust and Neon Channels
39
39
40
40
The mpsc channel provides a way for the JavaScript main thread to communicate with the database thread, but it is one-way. In order to complete the callback, the database thread must be able to communicate with the JavaScript main thread. [`neon::event::Channel`][channel] provides a channel for sending these events back.
41
41
42
-
#### `Root`
43
-
44
-
The last issue to solve is sending a reference to the JavaScript callback to the database thread and back again before finally calling it. [Handles][handle] to JavaScript values are not `Send`; they cannot escape the scope that created them. The reason they cannot be passed to other threads is because when control is returned back to the JavaScript engine, the garbage collector may determine they are no longer used and free the value.
45
-
46
-
A [`Root`][root] is a special handle to a JavaScript value that prevents the value from being freed as long as the `Root` has not been dropped. By placing the callback in a `Root`, it can be safely sent across threads and finally accessed and called when back on the JavaScript main thread.
0 commit comments