-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (110 loc) · 3.85 KB
/
script.js
File metadata and controls
129 lines (110 loc) · 3.85 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(function () {
var roomName,
myName = prompt("What's your name?", "Son Goku"),
executeDelta = !1,
storeDeltas = !1,
sendDelta = !1,
deltas = [],
isNew = !1,
editor,
lobby;
if (location.hash.substr(1)) {
roomName = location.hash.substr(1);
}
else {
roomName = "" + +new Date;
isNew = !0;
}
location.hash = roomName;
console.log('Connecting...');
goinstant.connect('https://goinstant.net/ca346407cc6d/snippeRT', {room : roomName}, function (err, connection, room) {
var notifications = new goinstant.widgets.Notifications();
if (err) {
console.dir(err);
return;
}
console.log('Connected.');
lobby = room;
console.log('Subscribing to notifications...');
notifications.subscribe(room, function(err) {
console.log('Done subscribing');
if (err) {
throw err;
}
console.log('Publishing notifications...');
notifications.publish({
room: room,
type: 'success',
message: myName + ' has joined.'
}, function(err) {
console.log('Done publishing');
if (err) {
throw err;
}
});
});
room.key('delta').on('set', function (value, context) {
console.log("receiving");
console.dir(value);
if (executeDelta) {
sendDelta = !1;
editor.getSession().getDocument().applyDeltas([value]);
sendDelta = !0;
// root's job
if (storeDeltas) {
console.log("Saving deltas");
room.key('deltas').set(editor.getValue(), function (err) {
if (err) {
throw err;
}
});
}
}
else {
deltas.push(value);
}
});
console.log("Getting deltas");
room.key('deltas').get(function (err, value){
console.log("Deltas : ");
console.dir(value);
editor = ace.edit("editor")
editor.setTheme('ace/theme/twilight');
editor.getSession().setMode('ace/mode/javascript');
editor.focus();
if (value) {
sendDelta = !1;
editor.setValue(value);
editor.clearSelection();
}
else {
storeDeltas = !0;
}
editor.getSession().getDocument().applyDeltas(deltas);
executeDelta = !0;
sendDelta = !0;
editor.on("change", function (e) {
if (sendDelta) {
console.log("sending delta : ");
console.dir(e.data);
lobby.key('delta').set(e.data, function (err, value, context) {
if (err) {
throw err;
}
});
if (storeDeltas) {
console.log("Saving deltas");
room.key('deltas').set(editor.getValue(), function (err) {
if (err) {
throw err;
}
});
}
}
});
if (isNew) {
alert("You can now share your URL to other collaborators! Hoorah!");
}
});
});
} () );