parent
4677186d0a
commit
10b199b6c4
13
app/index.js
13
app/index.js
|
@ -1,4 +1,4 @@
|
||||||
const { readFile } = require('fs');
|
const { readFile, writeFile } = require('fs');
|
||||||
const { connect, createServer } = require('net');
|
const { connect, createServer } = require('net');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const { promisify } = require('util');
|
const { promisify } = require('util');
|
||||||
|
@ -38,13 +38,21 @@ function setTitle(t) {
|
||||||
document.querySelector('.title').innerText = t;
|
document.querySelector('.title').innerText = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let filename = '';
|
||||||
|
|
||||||
async function loadFile(f) {
|
async function loadFile(f) {
|
||||||
const contents = await promisify(readFile)(f, { encoding: 'utf8' });
|
const contents = await promisify(readFile)(f, { encoding: 'utf8' });
|
||||||
|
filename = f;
|
||||||
m.setValue(contents);
|
m.setValue(contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function save(e) {
|
async function save(e) {
|
||||||
|
if (filename.length > 0) {
|
||||||
setTitle('saving');
|
setTitle('saving');
|
||||||
|
await promisify(writeFile)(filename, m.getValue(), { encoding: 'utf8' });
|
||||||
|
setTitle('saved');
|
||||||
|
setTimeout(() => setTitle(filename), 1000);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function open(e) {
|
async function open(e) {
|
||||||
|
@ -238,7 +246,7 @@ async function attemptConnect() {
|
||||||
m.setValue('');
|
m.setValue('');
|
||||||
});
|
});
|
||||||
connection.on('error', () => {
|
connection.on('error', () => {
|
||||||
setTitle('connection error');
|
setTitle('connection error!');
|
||||||
});
|
});
|
||||||
|
|
||||||
// set up a parser to read incoming deltas
|
// set up a parser to read incoming deltas
|
||||||
|
@ -273,6 +281,7 @@ function attemptListen() {
|
||||||
|
|
||||||
server.on('connection', async (c) => {
|
server.on('connection', async (c) => {
|
||||||
setTitle('new client connected');
|
setTitle('new client connected');
|
||||||
|
setTimeout(() => setTitle(filename), 1000);
|
||||||
|
|
||||||
// maintain a list of clients to do broadcasting later
|
// maintain a list of clients to do broadcasting later
|
||||||
connections.push(c);
|
connections.push(c);
|
||||||
|
|
Loading…
Reference in New Issue