Created
June 4, 2019 06:49
-
-
Save tonis2/a689d9d55ceadc55a23158a102f9fb8e to your computer and use it in GitHub Desktop.
Async websocket calls with ES6
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 marker = () => { | |
return Math.random() | |
.toString(36) | |
.slice(2) | |
.padStart(10, "0") | |
} | |
export default class AsyncSocket { | |
constructor(url = null, config = {}) { | |
this.url = url | |
this.config = config | |
this.listeners = [] | |
this.promises = [] | |
this.server = null | |
this.waitFor = this.waitFor.bind(this) | |
this.resolveMessages = this.resolveMessages.bind(this) | |
this.sendAsync = this.sendAsync.bind(this) | |
} | |
listen(callback) { | |
if (!this.server) console.warn("No server found") | |
this.listeners.push(callback) | |
} | |
send(payload) { | |
let data = JSON.stringify(payload) | |
if (this.server.readyState === 1) this.server.send(data) | |
} | |
sendAsync(payload) { | |
if (!"postback" in payload) { | |
console.warn("Please provide postback for async calls") | |
return | |
} | |
return new Promise(resolve => { | |
this.server.send(JSON.stringify(payload)) | |
this.promises.push({ type: "wait_for_postback", action: payload.postback.action, promise: resolve, marker: marker() }) | |
}) | |
} | |
waitFor(action) { | |
return new Promise(resolve => { | |
this.promises.push({ type: "wait_for_postback", action, promise: resolve, marker: marker() }) | |
}) | |
} | |
resolveMessages(message) { | |
const data = JSON.parse(message.data) | |
this.listeners.forEach(x => x(data)) | |
this.promises.forEach(x => { | |
if (data.postback && data.postback.action === x.action && x.type === "wait_for_postback") { | |
x.promise(data) | |
this.promises = this.promises.filter(i => i.marker !== x.marker) | |
} | |
}) | |
} | |
close() { | |
if (this.server) this.server.close() | |
} | |
async open() { | |
return new Promise((resolve, reject) => { | |
this.server = new WebSocket(this.url) | |
this.server.onmessage = message => this.resolveMessages(message) | |
this.server.onopen = () => resolve(this) | |
this.server.onerror = err => reject(err) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment