promise-connection
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,2 +0,2 @@ | ||
const PC = require('promise-connection'); | ||
const Connection = require('./connection'); | ||
@@ -82,6 +82,8 @@ function WindowBroker(options) { | ||
WindowBroker.prototype.addWindow = function(win) { | ||
this.windows.set(win, { | ||
channels: {} | ||
}); | ||
} | ||
if (typeof win === 'object' && win !== null) { | ||
this.windows.set(win, { | ||
channels: {} | ||
}); | ||
} | ||
}; | ||
@@ -93,2 +95,3 @@ WindowBroker.prototype._createConnection = function(win, type, port) { | ||
} | ||
channelData.request = true; | ||
channelData.port = port; | ||
@@ -109,3 +112,3 @@ var broker = this; | ||
.then(function(localAPI) { | ||
return (new PC.Connection(port, localAPI, { debug: true })).connected; | ||
return (new Connection(port, localAPI, { debug: true })).connected; | ||
}) | ||
@@ -130,3 +133,3 @@ .then(channelData.gotChannel) | ||
if (!channelData.channel && create !== false) { | ||
channelData.channel = new Promise(function(resolve, reject) { | ||
channelData.channel = new Promise(function(resolve) { | ||
channelData.gotChannel = resolve; | ||
@@ -141,2 +144,12 @@ }); | ||
WindowBroker.prototype.await = function(win, type) { | ||
if (!this.factories[type]) { | ||
throw new Error('request for unknown factory: ' + type); | ||
} | ||
if (win === null || typeof win !== 'object') { | ||
throw new Error('window must be an object'); | ||
} | ||
return this._channelData(win, type).channel; | ||
}; | ||
WindowBroker.prototype.request = function(win, type) { | ||
@@ -146,9 +159,10 @@ if (!this.factories[type]) { | ||
} | ||
if (!this.windows.has(win)) { | ||
this.addWindow(win); | ||
if (win === null || typeof win !== 'object') { | ||
throw new Error('window must be an object'); | ||
} | ||
var channelData = this._channelData(win, type, false); | ||
if (channelData.channel) { | ||
if (channelData.request) { | ||
return channelData.channel; | ||
} | ||
channelData.request = true; | ||
win.postMessage({ _broker: 'request', type: type }, '*'); | ||
@@ -155,0 +169,0 @@ return this._channelData(win, type).channel; |
{ | ||
"name": "promise-connection", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Promise/A+ utilities for abstracting remote window requests on message ports.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
24488
653