web-repl 0.3 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/js/replConnection-0.4.min.js +1 -0
- data/js/replConnection.js +6 -12
- data/lib/web-repl.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16603912aa2b99c9c68ced749d4a3f6ef3a2ead1
|
4
|
+
data.tar.gz: 4b3974407e3c741068f24fc2ea152acf5da8ee1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a3e4c9ab844ffd58a8a174b6fad56f1e625b1d236b217268cda519dcd367c213a3cf0e69af0890f3d32570aec1db1506c22b4880e52983cd3b536558cdf280c
|
7
|
+
data.tar.gz: 9c6781d65d49a485ef741ed1bd1d4e13587108c22eed7906152b043a58110188aab06b1a927a5596ae3c057929846dd698eef9d5b43ed75a57134b73d165ab66
|
@@ -0,0 +1 @@
|
|
1
|
+
function ReplConnection(e,t,n){n=typeof n==="undefined"?{}:n;this.debug=!!n.debug;this.reconnect=!!n.reconnect;this.retryTime=n.retryTime||1e3;this.evalFunction=n.evalFunction;this.host=e;this.port=t;this.active=false;this.socket;this.supported="WebSocket"in window;if(this.supported){console.log("REPL: socket ok")}else{console.log("REPL: socket not supported")}}ReplConnection.prototype.eval=function(statement){options=typeof options==="undefined"?{}:options;response={};try{if(typeof this.evalFunction==="function"){response.value=this.evalFunction(statement)}else{response.value=eval(statement)}}catch(err){response.error=err.message}return response};ReplConnection.prototype.initSocket=function(e){if(this.socket!==undefined&&this.socket!==null){this.socket.close()}var t="ws://"+this.host+":"+this.port+"/echo";this.socket=new WebSocket(t);e()};ReplConnection.prototype.handleSocketOpen=function(){this.active=true;console.log("REPL: socket ready")};ReplConnection.prototype.tryConnection=function(){console.log("REPL: waiting for connection");if(this.reconnect){var e=this;window.setTimeout(function(){e.start()},this.retryTime)}};ReplConnection.prototype.handleSocketClose=function(e){if(!this.active){this.tryConnection()}else{console.log("REPL: socket closed");this.active=false;if(this.reconnect){this.tryConnection()}}};ReplConnection.prototype.handleMessageReceived=function(e){if(this.debug){console.log("REPL: message received");console.log(e)}var t=JSON.parse(e.data);var n=t.timestamp;t.timestamp=new Date(n);var r=this.eval(t.statement);var i=this.prepareJSONResponse(r);if(this.debug){console.log("REPL: replying ");console.log(i)}this.socket.send(i)};ReplConnection.prototype.prepareJSONResponse=function(e){var t;try{e.timestamp=(new Date).getTime();t=JSON.stringify(e)}catch(n){e.value=null;e.error=n.message;t=this.prepareJSONResponse(e)}return t};ReplConnection.prototype.initEventHandling=function(){var e=this;this.socket.onopen=function(){e.handleSocketOpen()};this.socket.onclose=function(t){e.handleSocketClose(t)};this.socket.onmessage=function(t){e.handleMessageReceived(t)}};ReplConnection.prototype.start=function(e){if(this.supported){var t=this;this.initSocket(function(){t.initEventHandling();if(e!==undefined){e(t)}})}}
|
data/js/replConnection.js
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
// A connection to the REPL server using Websocket
|
2
2
|
function ReplConnection(host, port, options) {
|
3
|
-
|
4
|
-
options = {};
|
5
|
-
}
|
3
|
+
options = (typeof options === "undefined") ? {} : options;
|
6
4
|
this.debug = !!options.debug;
|
7
5
|
this.reconnect = !!options.reconnect;
|
8
6
|
this.retryTime = options.retryTime || 1000;
|
9
|
-
this.
|
7
|
+
this.evalFunction = options.evalFunction;
|
10
8
|
this.host = host;
|
11
9
|
this.port = port;
|
12
10
|
this.active = false;
|
@@ -21,13 +19,12 @@ function ReplConnection(host, port, options) {
|
|
21
19
|
|
22
20
|
// This is the "eval" for the REPL
|
23
21
|
// statement: the statement to evaluate
|
24
|
-
|
25
|
-
ReplConnection.prototype.eval = function(statement, options) {
|
22
|
+
ReplConnection.prototype.eval = function(statement) {
|
26
23
|
options = (typeof options === "undefined") ? {} : options;
|
27
|
-
response = {}
|
24
|
+
response = {};
|
28
25
|
try {
|
29
|
-
if (typeof
|
30
|
-
response.value =
|
26
|
+
if (typeof this.evalFunction === "function") {
|
27
|
+
response.value = this.evalFunction(statement);
|
31
28
|
} else {
|
32
29
|
response.value = eval(statement);
|
33
30
|
}
|
@@ -88,9 +85,6 @@ ReplConnection.prototype.handleMessageReceived = function(event) {
|
|
88
85
|
var timestamp = message.timestamp;
|
89
86
|
message.timestamp = new Date(timestamp);
|
90
87
|
//
|
91
|
-
if (this.onReceive !== undefined) {
|
92
|
-
this.onReceive(message); // fire the custom callback
|
93
|
-
}
|
94
88
|
var response = this.eval(message.statement); // evaluate the statement
|
95
89
|
var json = this.prepareJSONResponse(response)
|
96
90
|
if (this.debug) {
|
data/lib/web-repl.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: web-repl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Russo
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- bin/web-repl
|
94
94
|
- js/replConnection-0.1.min.js
|
95
95
|
- js/replConnection-0.3.min.js
|
96
|
+
- js/replConnection-0.4.min.js
|
96
97
|
- js/replConnection.js
|
97
98
|
- lib/web-repl.rb
|
98
99
|
- lib/web-repl/messager.rb
|