Commit fb95dd85 authored by Max's avatar Max

Inpage: Promise when undefined callback

parent e7b1289c
......@@ -23,7 +23,6 @@ function triggerCb(res) {
if (cbs[res.id] != undefined) {
cbs[res.id](res.result)
}
else console.log("[inpage] no callback for %i", res.id)
}
window.addEventListener("message", function(event) {
......@@ -37,7 +36,7 @@ window.addEventListener("message", function(event) {
triggerCb(obj.res)
}
else if (obj.req.id == -1) {
state_cb()
if (state_cb != undefined) state_cb();
}
else {
console.log("[inpage] recv message with no result") ;
......@@ -50,8 +49,37 @@ window.addEventListener("message", function(event) {
function post (method, cb, data) {
var uid = getUniqueId().toString() ;
var request = { id: uid, name: method, data: data } ;
addCb(uid, cb) ;
window.postMessage({ src: "inpage", req: request }, "*")
var p;
if (cb != undefined) {
addCb(uid, cb) ;
window.postMessage({ src: "inpage", req: request }, "*")
} else {
p = new Promise(function(resolve, reject) {
window.addEventListener("message", function(event) {
if (event.source != window) reject(event.data);
try {
var obj = event.data ;
if (obj.res != undefined && obj.res.id == uid) {
if (obj.src && (obj.src == "background" || obj.src == "popup")) {
if (obj.res != undefined && obj.res.result.ok) {
resolve(obj.res)
}
else if (obj.res != undefined) {
reject(obj.res)
}
else {
reject("[inpage] recv message with no result") ;
}
}
else reject("[inpage] wrong source")
}
}
catch(error) { reject(error) }
}, false);
window.postMessage({ src: "inpage", req: request }, "*")
});
}
return p;
}
function makeData (kind, data) {
......@@ -148,7 +176,7 @@ metal = {
* @param {boolCallback} cb - callback to handle the result
*/
isEnabled: function (cb) {
post("is_enabled", cb)
return post("is_enabled", cb)
},
/**
......@@ -158,7 +186,7 @@ metal = {
* @param {boolCallback} cb - callback to handle the result
*/
isUnlocked: function (cb) {
post("is_unlocked", cb)
return post("is_unlocked", cb)
},
/**
......@@ -169,7 +197,7 @@ metal = {
* @param {boolCallback} cb - callback to handle the result
*/
isApproved: function (cb) {
post("is_approved", cb)
return post("is_approved", cb)
},
/**
......@@ -178,7 +206,7 @@ metal = {
* @param {hashCallback} cb - callback to handle the result
*/
getAccount: function (cb) {
post("get_account", cb)
return post("get_account", cb)
},
......@@ -188,7 +216,7 @@ metal = {
* @param {networkCallback} cb - callback to handle the result
*/
getNetwork: function (cb) {
post("get_network", cb)
return post("get_network", cb)
},
/**
......@@ -208,7 +236,7 @@ metal = {
var data = makeData(
"transaction",
{dst, amount, fee, entrypoint, parameter, gas_limit, storage_limit});
post("send", cb, data)
return post("send", cb, data);
},
/**
......@@ -229,7 +257,7 @@ metal = {
var data = makeData(
"origination",
{balance, fee, gas_limit, storage_limit, sc_code_hash, sc_storage, sc_code});
post("originate", cb, data)
return post("originate", cb, data)
},
/**
......@@ -246,7 +274,7 @@ metal = {
var data = makeData(
"delegation",
{delegate, fee, gas_limit, storage_limit});
post("delegate", cb, data)
return post("delegate", cb, data)
},
......@@ -272,7 +300,7 @@ metal = {
batch: function({operations, cb}) {
function make(op) { return makeData(op.kind, op.operation) };
var data = operations.map(make);
post("batch", cb, data)
return post("batch", cb, data)
},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment