Skip to content

Commit

Permalink
improves gpf.interfaces.promisify (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBuchholz committed Aug 16, 2018
1 parent d809ae2 commit f12feaf
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/interfaces/promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
*/
/*#ifndef(UMD)*/
"use strict";
/*global _gpfInterfaceQuery*/ // gpf.interfaces.query
/*global _gpfObjectForEach*/ // Similar to [].forEach but for objects
/*global _gpfPromisify*/ // Converts any value into a Promise
/*#endif*/

function _gpfInterfacesWrap (object, interfaceSpecifier, promise) {
function _gpfInterfacesWrap (iInterfaceImpl, interfaceSpecifier, promise) {
var fThen = promise.then;
promise.then = function () {
return _gpfInterfacesWrap(iInterfaceImpl, interfaceSpecifier, fThen.apply(promise, arguments));
};
_gpfObjectForEach(interfaceSpecifier.prototype, function (referenceMethod, name) {
promise[name] = function () {
var args = arguments;
return _gpfInterfacesWrap(object, interfaceSpecifier, promise.then(function () {
return _gpfPromisify(object[name].apply(object, args));
return _gpfInterfacesWrap(iInterfaceImpl, interfaceSpecifier, promise.then(function () {
return _gpfPromisify(iInterfaceImpl[name].apply(iInterfaceImpl, args));
}));
};
});
Expand All @@ -29,7 +34,13 @@ function _gpfInterfacesWrap (object, interfaceSpecifier, promise) {
*/
function _gpfInterfacesPromisify (interfaceSpecifier) {
return function (object) {
return _gpfInterfacesWrap(object, interfaceSpecifier, Promise.resolve());
var iInterfaceImpl = _gpfInterfaceQuery(interfaceSpecifier, object);
if (!iInterfaceImpl) {
gpf.Error.interfaceExpected({
name: interfaceSpecifier.compatibleName()
});
}
return _gpfInterfacesWrap(iInterfaceImpl, interfaceSpecifier, Promise.resolve());
};
}

Expand Down

0 comments on commit f12feaf

Please sign in to comment.