Turn InAppPurchase into an EventEmitter

This commit is contained in:
Cheng Zhao 2018-01-10 16:37:05 +09:00
parent 400bfb3c5a
commit ac6f895f64
8 changed files with 123 additions and 90 deletions

View file

@ -1,39 +1,10 @@
'use strict'
const binding = process.atomBinding('in_app_purchase')
const v8Util = process.atomBinding('v8_util')
const {EventEmitter} = require('events')
const {inAppPurchase, InAppPurchase} = process.atomBinding('in_app_purchase')
module.exports = {
// inAppPurchase is an EventEmitter.
Object.setPrototypeOf(InAppPurchase.prototype, EventEmitter.prototype)
EventEmitter.call(inAppPurchase)
canMakePayments: function () {
return binding.canMakePayments()
},
getReceiptURL: function () {
return binding.getReceiptURL()
},
purchaseProduct: function (productID, quantity = 1, callback) {
if (typeof productID !== 'string') {
throw new TypeError('productID must be a string')
}
if (typeof quantity === 'function') {
quantity = 1
callback = quantity
}
return binding.purchaseProduct(productID, quantity, callback)
},
addTransactionListener: function (callback) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function')
}
return binding.addTransactionListener(callback)
}
}
// Mark standard asynchronous functions.
v8Util.setHiddenValue(
module.exports.purchaseProduct, 'asynchronous', true)
v8Util.setHiddenValue(
module.exports.addTransactionListener, 'asynchronous', true)
module.exports = inAppPurchase