Skip to content

Commit

Permalink
Bugfixes for prod errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brain committed Apr 7, 2016
1 parent 28c3844 commit 5a36e94
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,54 +106,57 @@

function postMessageRequest(options) {

if (!options.window) {
throw new Error('Expected options.window');
}

if (!options.name) {
throw new Error('Expected options.name');
}

var promise;

if (!options.callback) {
if (window.Promise) {
promise = new window.Promise(function (resolve, reject) {
options.callback = function (err, result) {
return err ? reject(err) : resolve(result);
};
});
} else {
throw new Error('Expected callback or window.Promise');
}
}

options.respond = once(function(err, response) {
options.callback(err, response);
return promise;
});

if (!options.window) {
return options.respond(new Error('Expected options.window'));
}

if (mockMode) {
options.window = window;

} else if (typeof options.window === 'string') {
var el = document.getElementById(options.window);

if (!el) {
throw new Error('Expected options.window ' + options.window + ' to be a valid element id');
return options.respond(new Error('Expected options.window ' + options.window + ' to be a valid element id'));
}

if (el.tagName.toLowerCase() !== 'iframe') {
throw new Error('Expected options.window ' + options.window + ' to be an iframe');
return options.respond(new Error('Expected options.window ' + options.window + ' to be an iframe'));
}

options.window = el.contentWindow;
}

options.callback = once(options.callback);

var promise;

if (!options.callback && window.Promise) {
promise = new window.Promise(function(resolve, reject) {
options.callback = function(err, result) {
return err ? reject(err) : resolve(result);
};
});
if (!options.window) {
return options.respond(new Error('Expected options.window'));
}
}

var hash = options.name + '_' + Math.random().toString();
responseHandlers[hash] = options;

options.respond = once(function(err, response) {
return options.callback(err, response);
});

if (!options.window.postMessage) {
return options.respond(new Error('Target window does not have postMessage handler'));
}

if (options.window.closed) {
return options.respond(new Error('Target window is closed'));
}
Expand Down

0 comments on commit 5a36e94

Please # to comment.