-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (44 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const fs = require('fs');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
app.use(bodyParser.json());
const ot = require('ot');
const bus = require('statebus/server')({
port: 8004,
file_store: false,
client: (client) => {},
});
const server = new ot.Server("// Write some JS!");
const ids = [];
bus('/document').to_fetch = (key) => {
return {
revision: server.operations.length,
text: server.document,
}
}
bus('/ops').to_fetch = (key) => {
return {
all: server.operations,
ids: ids,
}
}
bus('/op').to_save = (obj) => {
console.log(ot.TextOperation.fromJSON(obj.operation), server.document);
server.receiveOperation(obj.revision, ot.TextOperation.fromJSON(obj.operation));
ids.push(obj.id);
bus.dirty('/ops');
}
readFile = () => {
fs.readFile('FILE', 'utf8', (err, data) => {
if (err) throw err;
bus.save.fire({
key: '/code',
text: data,
})
});
};
readFile();
fs.watch('FILE', readFile);
app.use(express.static(__dirname + '/'));
app.listen(8000);