ZeroOmega/omega-target-chromium-extension/src/chrome_api.coffee

20 lines
583 B
CoffeeScript
Raw Normal View History

2014-09-20 11:49:04 -04:00
OmegaTarget = require('omega-target')
Promise = OmegaTarget.Promise
chromeApiPromisifer = (originalMethod) ->
return (args...) ->
new Promise (resolve, reject) =>
callback = (callbackArgs...) ->
if chrome.runtime.lastError?
return reject(chrome.runtime.lastError)
if callbackArgs.length <= 1
resolve(callbackArgs[0])
else
resolve(callbackArgs)
args.push(callback)
originalMethod.apply(this, args)
module.exports = (obj) ->
Promise.promisifyAll(Object.create(obj), {promisifier: chromeApiPromisifer})