Skip to content

Commit

Permalink
Fix anonymous functions in PromisedSockets
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 1, 2019
1 parent ee4e55f commit 20031d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
18 changes: 8 additions & 10 deletions gramjs/extensions/PromisedNetSockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,19 @@ class PromisedNetSockets {
this.resolveRead = resolve
})
this.closed = false
return new Promise(function(resolve, reject) {
this.client.connect(port, ip, function() {
return new Promise((resolve, reject) => {
this.client.connect(port, ip, () => {
this.receive()
resolve(this)
}.bind(this))
this.client.on('error', function(error) {
reject(error)
})
this.client.on('close', function() {
this.client.on('error', reject)
this.client.on('close', () => {
if (this.client.closed) {
this.resolveRead(false)
this.closed = true
}
}.bind(this))
}.bind(this))
})
})
}

write(data) {
Expand All @@ -85,11 +83,11 @@ class PromisedNetSockets {
}

async receive() {
this.client.on('data', async function(message) {
this.client.on('data', async (message) => {
const data = Buffer.from(message)
this.stream = Buffer.concat([this.stream, data])
this.resolveRead(true)
}.bind(this))
})
}
}

Expand Down
18 changes: 8 additions & 10 deletions gramjs/extensions/PromisedWebSockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,19 @@ class PromisedWebSockets {
this.closed = false
this.website = this.getWebSocketLink(ip, port)
this.client = new WebSocketClient(this.website, 'binary')
return new Promise(function(resolve, reject) {
this.client.onopen = function() {
return new Promise((resolve, reject) => {
this.client.onopen = () => {
this.receive()
resolve(this)
}.bind(this)
this.client.onerror = function(error) {
reject(error)
}
this.client.onclose = function() {
this.client.onerror = reject
this.client.onclose = () => {
if (this.client.closed) {
this.resolveRead(false)
this.closed = true
}
}.bind(this)
}.bind(this))
}
})
}

write(data) {
Expand All @@ -93,7 +91,7 @@ class PromisedWebSockets {
}

async receive() {
this.client.onmessage = async function(message) {
this.client.onmessage = async (message) => {
let data
if (this.isBrowser) {
data = Buffer.from(await new Response(message.data).arrayBuffer())
Expand All @@ -102,7 +100,7 @@ class PromisedWebSockets {
}
this.stream = Buffer.concat([this.stream, data])
this.resolveRead(true)
}.bind(this)
}
}
}

Expand Down

0 comments on commit 20031d5

Please sign in to comment.