39 lines
859 B
TypeScript
Raw Normal View History

2015-11-25 11:44:58 -06:00
declare var window;
declare var Promise;
2015-11-24 15:00:16 -06:00
export function get(obj, path) {
for (var i=0, path = path.split('.'), len = path.length; i < len; i++) {
obj = obj[path[i]];
}
return obj;
};
2015-11-25 11:44:58 -06:00
export const promisify = (pluginRef, methodName, successIndex, errorIndex) => {
return (...args) => {
return new Promise((resolve, reject) => {
args[successIndex] = resolve;
args[errorIndex] = reject;
get(window, pluginRef)[methodName].apply(this, args);
})
}
}
2015-11-28 16:17:04 -06:00
export const wrap = (pluginRef, methodName, successIndex=null, errorIndex=null) => {
return (...args) => {
return new Promise((resolve, reject) => {
if(successIndex) {
args[successIndex] = resolve;
}
if(errorIndex) {
args[errorIndex] = reject;
}
get(window, pluginRef)[methodName].apply(this, args);
})
}
}