Created
June 20, 2017 13:20
-
-
Save zetxx/779a71ebd15ce03dc0c7c1c455467063 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/jucocad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
const EventEmitter = require('events'); | |
class Client extends EventEmitter { | |
write(message) { | |
var msg = message.payload; | |
if (message.stop) { | |
return console.log(`stop here: ${msg}`); | |
} | |
if (msg.inc) { | |
msg = msg.data + msg.inc; | |
} | |
var msgId = message.id; | |
this.emit('read', {payload: msg, msgId}); | |
} | |
read(msg) { | |
this.emit('read', msg); | |
} | |
} | |
class Node { | |
constructor(client, incoming) { | |
this.client = client; | |
this.wentOut = {}; | |
this.outId = 0; | |
client.on('read', this.comesIn.bind(this)); | |
this.transformIncoming = incoming; | |
} | |
tagMsg(cb) { | |
var id = ++this.outId; | |
this.wentOut[id] = cb; | |
return id; | |
} | |
goesOut(message, cb) { | |
if (cb) { | |
message.id = this.tagMsg(cb); | |
} | |
this.client.write(message); | |
} | |
comesIn(message) { | |
var payload = message.payload; | |
var id = message.msgId; | |
if (this.wentOut[id]) { // response to smthing | |
let fn = this.wentOut[id]; | |
delete this.wentOut[id]; | |
fn(payload); | |
} else { // incoming message that is not a response of our request | |
this.goesOut(this.transformIncoming(message)); | |
} | |
} | |
} | |
var c = new Client(); | |
var n1 = new Node(c, (msg) => { | |
let payload = msg.payload; | |
let msgId = msg.msgId; | |
if (payload.inc) { | |
return {stop: true, msgId, payload: (payload.data + payload.inc)}; | |
} | |
}); | |
c.read({payload: {data: 8, inc: 1}, msgId: 1}); | |
case 1: packet comes in | |
n1.goesOut({payload: {data: 10, inc: 1}}, (r) => { | |
console.log(`{payload: {data: 10, inc: 1}} --> and the response is: ${r}`); | |
c.read({payload: {data: 8, inc: 1}, msgId: 1}); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const EventEmitter = require('events'); | |
class Client extends EventEmitter { | |
write(message) { | |
var msg = message.payload; | |
if (message.stop) { | |
return console.log(`stop here: ${msg}`); | |
} | |
if (msg.inc) { | |
msg = msg.data + msg.inc; | |
} | |
var msgId = message.id; | |
this.emit('read', {payload: msg, msgId}); | |
} | |
read(msg) { | |
this.emit('read', msg); | |
} | |
} | |
class Node { | |
constructor(client, incoming) { | |
this.client = client; | |
this.wentOut = {}; | |
this.outId = 0; | |
client.on('read', this.comesIn.bind(this)); | |
this.transformIncoming = incoming; | |
} | |
tagMsg(cb) { | |
var id = ++this.outId; | |
this.wentOut[id] = cb; | |
return id; | |
} | |
goesOut(message, cb) { | |
if (cb) { | |
message.id = this.tagMsg(cb); | |
} | |
this.client.write(message); | |
} | |
comesIn(message) { | |
var payload = message.payload; | |
var id = message.msgId; | |
if (this.wentOut[id]) { // response to smthing | |
let fn = this.wentOut[id]; | |
delete this.wentOut[id]; | |
fn(payload); | |
} else { // incoming message that is not a response of our request | |
this.goesOut(this.transformIncoming(message)); | |
} | |
} | |
} | |
var c = new Client(); | |
var n1 = new Node(c, (msg) => { | |
let payload = msg.payload; | |
let msgId = msg.msgId; | |
if (payload.inc) { | |
return {stop: true, msgId, payload: (payload.data + payload.inc)}; | |
} | |
}); | |
c.read({payload: {data: 8, inc: 1}, msgId: 1}); | |
case 1: packet comes in | |
n1.goesOut({payload: {data: 10, inc: 1}}, (r) => { | |
console.log(`{payload: {data: 10, inc: 1}} --> and the response is: ${r}`); | |
c.read({payload: {data: 8, inc: 1}, msgId: 1}); | |
}); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EventEmitter = require('events'); | |
class Client extends EventEmitter { | |
write(message) { | |
var msg = message.payload; | |
if (message.stop) { | |
return console.log(`stop here: ${msg}`); | |
} | |
if (msg.inc) { | |
msg = msg.data + msg.inc; | |
} | |
var msgId = message.id; | |
this.emit('read', {payload: msg, msgId}); | |
} | |
read(msg) { | |
this.emit('read', msg); | |
} | |
} | |
class Node { | |
constructor(client, incoming) { | |
this.client = client; | |
this.wentOut = {}; | |
this.outId = 0; | |
client.on('read', this.comesIn.bind(this)); | |
this.transformIncoming = incoming; | |
} | |
tagMsg(cb) { | |
var id = ++this.outId; | |
this.wentOut[id] = cb; | |
return id; | |
} | |
goesOut(message, cb) { | |
if (cb) { | |
message.id = this.tagMsg(cb); | |
} | |
this.client.write(message); | |
} | |
comesIn(message) { | |
var payload = message.payload; | |
var id = message.msgId; | |
if (this.wentOut[id]) { // response to smthing | |
let fn = this.wentOut[id]; | |
delete this.wentOut[id]; | |
fn(payload); | |
} else { // incoming message that is not a response of our request | |
this.goesOut(this.transformIncoming(message)); | |
} | |
} | |
} | |
var c = new Client(); | |
var n1 = new Node(c, (msg) => { | |
let payload = msg.payload; | |
let msgId = msg.msgId; | |
if (payload.inc) { | |
return {stop: true, msgId, payload: (payload.data + payload.inc)}; | |
} | |
}); | |
c.read({payload: {data: 8, inc: 1}, msgId: 1}); | |
case 1: packet comes in | |
n1.goesOut({payload: {data: 10, inc: 1}}, (r) => { | |
console.log(`{payload: {data: 10, inc: 1}} --> and the response is: ${r}`); | |
c.read({payload: {data: 8, inc: 1}, msgId: 1}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment