-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
63 lines (57 loc) · 1.28 KB
/
client.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
57
58
59
60
61
62
63
var loader = require("@loader");
var loadPromise = new Promise(function(resolve, reject){
function connect(){
var host = loader.liveReloadHost || window.document.location.host.replace(/:.*/, "");
var port = loader.liveReloadTestPort || 8015;
var ws = new WebSocket("ws://" + host + ":" + port);
ws.onopen = function(){
resolve(ws);
};
ws.onerror = function(e){
reject(e);
};
}
connect();
});
function send(ws, msg){
ws.send(JSON.stringify(msg));
}
function waitForMessage(ws, type){
return new Promise(function(resolve){
ws.addEventListener("message", function onevent(data){
var msg = JSON.parse(data.data);
if(msg[type]) {
ws.removeEventListener("message", onevent);
resolve();
}
});
});
}
exports.put = function(address, content){
return loadPromise.then(function(ws){
send(ws, {
address: address,
content: content,
type: "put"
});
return waitForMessage(ws, "put");
});
};
exports.reset = function(){
return loadPromise.then(function(ws){
send(ws, {
type: "reset"
});
return waitForMessage(ws, "reset");
});
};
exports.install = function(packageName, flags){
return loadPromise.then(function(ws){
send(ws, {
type: "install",
package: packageName,
flags: flags
});
return waitForMessage(ws, "install");
});
};