Skip to content

Commit

Permalink
Add callback to status messages on sale
Browse files Browse the repository at this point in the history
  • Loading branch information
gdespirito committed Sep 29, 2020
1 parent 095f0a8 commit 2dd8599
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
11 changes: 10 additions & 1 deletion dist/pos.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pos.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "dist/pos.js",
"module": "src/pos.js",
"scripts": {
"start": "webpack --watch --config webpack.config.js",
"build": "webpack --config webpack.config.js",
"prepublish": "npm run build"
},
Expand Down
23 changes: 11 additions & 12 deletions src/pos.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class TransbankPOSWebSocket {
}
}

socket() {
return this.socket;
}

async connect(socketIoUrl = "http://localhost:8090") {
this.socket = io("http://localhost:8090")
this.isConnected = true
Expand All @@ -30,14 +34,6 @@ export class TransbankPOSWebSocket {
return true;
}

wait(time) {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, time)
})
}

send(method, params = "") {
return new Promise((resolve, reject) => {
if (!this.isConnected && this.socket!==null) {
Expand All @@ -56,8 +52,6 @@ export class TransbankPOSWebSocket {
} else {
reject(data.message)
}


})

this.socket.emit(method, params)
Expand Down Expand Up @@ -119,9 +113,14 @@ export class TransbankPOSWebSocket {
return await this.send("getPortStatus")
}

async doSale(amount, ticket) {
async doSale(amount, ticket, callback = null) {
let params = { amount: amount, ticket: ticket }
return await this.send("sale", params)
if (typeof callback === 'function') {
this.socket.on('sale_status.response', callback)
}
let response = await this.send("sale", params)
this.socket.off('sale_status.response', callback)
return response;
}
}

Expand Down

0 comments on commit 2dd8599

Please sign in to comment.