mirror of
https://github.com/aljazceru/pear-docs.git
synced 2025-12-17 22:44:21 +01:00
Update guides (#134)
* update desktop guide * update pear terminal guides * fixes * fix ctrl-c for example terminal app * use SIGINT on readline close --------- Co-authored-by: rafapaezbas <rafaelpaezbastida@gmail.com>
This commit is contained in:
@@ -14,7 +14,7 @@ Pear runs on [`Bare`](https://github.com/holepunchto/bare), a lightweight JavaSc
|
||||
|
||||
|
||||
```
|
||||
npm i bare-readline bare-tty hyperswarm b4a hypercore-crypto
|
||||
npm i bare-readline bare-tty bare-process hyperswarm b4a hypercore-crypto
|
||||
```
|
||||
|
||||
## Step 2. JavaScript
|
||||
@@ -28,9 +28,10 @@ import b4a from 'b4a' // Module for buffer-to-string and vice-ve
|
||||
import crypto from 'hypercore-crypto' // Cryptographic functions for generating the key in app
|
||||
import readline from 'bare-readline' // Module for reading user input in terminal
|
||||
import tty from 'bare-tty' // Module to control terminal behavior
|
||||
import process from 'bare-process' // Process control for Bare
|
||||
|
||||
|
||||
const { teardown, config } = Pear // Import configuration options and cleanup functions from Pear
|
||||
const { teardown, config, updates } = Pear // Import configuration options, updates and cleanup functions from Pear
|
||||
const key = config.args.pop() // Retrieve a potential chat room key from command-line arguments
|
||||
const shouldCreateSwarm = !key // Flag to determine if a new chat room should be created
|
||||
const swarm = new Hyperswarm()
|
||||
@@ -39,6 +40,10 @@ const swarm = new Hyperswarm()
|
||||
// (This is not a requirement, but it helps avoid DHT pollution)
|
||||
teardown(() => swarm.destroy())
|
||||
|
||||
// Enable automatic reloading for the app
|
||||
// This is optional but helpful during production
|
||||
updates(() => Pear.reload())
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: new tty.ReadStream(0),
|
||||
output: new tty.WriteStream(1)
|
||||
@@ -70,6 +75,10 @@ rl.on('data', line => {
|
||||
})
|
||||
rl.prompt()
|
||||
|
||||
rl.on('close', () => {
|
||||
process.kill(process.pid, 'SIGINT')
|
||||
})
|
||||
|
||||
async function createChatRoom () {
|
||||
// Generate a new random topic (32 byte string)
|
||||
const topicBuffer = crypto.randomBytes(32)
|
||||
@@ -86,7 +95,7 @@ async function joinChatRoom (topicStr) {
|
||||
}
|
||||
|
||||
async function joinSwarm (topicBuffer) {
|
||||
// Join the swarm with the topic. Setting both client/server to true means that this app can act as both.
|
||||
// Join the swarm with the topic. Setting both client/server to true means that this app can act as both.
|
||||
const discovery = swarm.join(topicBuffer, { client: true, server: true })
|
||||
await discovery.flushed()
|
||||
}
|
||||
@@ -105,7 +114,7 @@ function appendMessage ({ name, message }) {
|
||||
|
||||
## Step 3. Run in dev mode
|
||||
|
||||
To test this chat app, in one terminal run `pear dev .`
|
||||
To test this chat app, in one terminal run `pear run --dev .`
|
||||
|
||||
The app will output something similar to:
|
||||
|
||||
@@ -113,7 +122,7 @@ The app will output something similar to:
|
||||
[info] Created new chat room: a1b2c35fbeb452bc900c5a1c00306e52319a3159317312f54fe5a246d634f51a
|
||||
```
|
||||
|
||||
In another terminal use this key as input, `pear dev . a1b2c35fbeb452bc900c5a1c00306e52319a3159317312f54fe5a246d634f51a`
|
||||
In another terminal use this key as input, `pear run --dev . a1b2c35fbeb452bc900c5a1c00306e52319a3159317312f54fe5a246d634f51a`
|
||||
|
||||
The app will output:
|
||||
|
||||
@@ -128,4 +137,4 @@ Type something in one of the applications. Two Terminal Applications are now con
|
||||
|
||||
## Next
|
||||
|
||||
* [Sharing a Pear Application](./sharing-a-pear-app.md)
|
||||
* [Sharing a Pear Application](./sharing-a-pear-app.md)
|
||||
Reference in New Issue
Block a user