@looker/chatty
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -34,2 +34,3 @@ import { Callback, CallbackStore } from './types'; | ||
* Sets the default period of time a [[ChattyClientConnection.sendAndReceive]] message will wait. | ||
* Use a negative number to wait indefinitely. | ||
* The default is 30000ms | ||
@@ -36,0 +37,0 @@ * |
@@ -86,2 +86,3 @@ "use strict"; | ||
* Sets the default period of time a [[ChattyClientConnection.sendAndReceive]] message will wait. | ||
* Use a negative number to wait indefinitely. | ||
* The default is 30000ms | ||
@@ -88,0 +89,0 @@ * |
@@ -15,3 +15,5 @@ /** | ||
/** Response */ | ||
Response = 4 | ||
Response = 4, | ||
/** Asynchronous error response */ | ||
ResponseError = 5 | ||
} |
@@ -42,2 +42,4 @@ "use strict"; | ||
ChattyClientMessages[ChattyClientMessages["Response"] = 4] = "Response"; | ||
/** Asynchronous error response */ | ||
ChattyClientMessages[ChattyClientMessages["ResponseError"] = 5] = "ResponseError"; | ||
})(ChattyClientMessages = exports.ChattyClientMessages || (exports.ChattyClientMessages = {})); |
@@ -25,3 +25,4 @@ import { ChattyClientBuilder } from './client_builder'; | ||
/** | ||
* Send a message to the host via a message channel, and then await a response. | ||
* Send a message to the host via a message channel, and then await a response. The event listener in | ||
* the host returns a value or a promise that is resolved at some later point. | ||
* | ||
@@ -28,0 +29,0 @@ * @param eventName The name of the event to send to the host |
@@ -165,7 +165,10 @@ "use strict"; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this._receivers[sequence] = resolve; | ||
setTimeout(function () { | ||
delete _this._receivers[sequence]; | ||
reject(new Error('Timeout')); | ||
}, _this._defaultTimeout); | ||
var timeoutId; | ||
if (_this._defaultTimeout > -1) { | ||
timeoutId = setTimeout(function () { | ||
delete _this._receivers[sequence]; | ||
reject(new Error('Timeout')); | ||
}, _this._defaultTimeout); | ||
} | ||
_this._receivers[sequence] = { resolve: resolve, reject: reject, timeoutId: timeoutId }; | ||
})]; | ||
@@ -183,8 +186,16 @@ }); | ||
case host_messages_1.ChattyHostMessages.MessageWithResponse: | ||
var _a = evt.data.data, eventName = _a.eventName, payload_1 = _a.payload, sequence = _a.sequence; | ||
var results = []; | ||
if (_this._handlers[eventName]) { | ||
results = _this._handlers[eventName].map(function (fn) { return fn.apply(_this, payload_1); }); | ||
{ | ||
var _a = evt.data.data, eventName_1 = _a.eventName, payload_1 = _a.payload, sequence_1 = _a.sequence; | ||
var results = []; | ||
if (_this._handlers[eventName_1]) { | ||
results = _this._handlers[eventName_1].map(function (fn) { return fn.apply(_this, payload_1); }); | ||
} | ||
Promise.all(results) | ||
.then(function (resolvedResults) { | ||
_this.sendMsg(client_messages_1.ChattyClientMessages.Response, { eventName: eventName_1, payload: resolvedResults }, sequence_1); | ||
}) | ||
.catch(function (error) { | ||
_this.sendMsg(client_messages_1.ChattyClientMessages.ResponseError, { eventName: eventName_1, payload: error }, sequence_1); | ||
}); | ||
} | ||
_this.sendMsg(client_messages_1.ChattyClientMessages.Response, { eventName: eventName, payload: results }, sequence); | ||
break; | ||
@@ -195,4 +206,20 @@ case host_messages_1.ChattyHostMessages.Response: | ||
delete _this._receivers[evt.data.data.sequence]; | ||
receiver(evt.data.data.payload); | ||
if (receiver.timeoutId) { | ||
clearTimeout(receiver.timeoutId); | ||
} | ||
receiver.resolve(evt.data.data.payload); | ||
} | ||
break; | ||
case host_messages_1.ChattyHostMessages.ResponseError: | ||
{ | ||
var receiver_1 = _this._receivers[evt.data.data.sequence]; | ||
if (receiver_1) { | ||
delete _this._receivers[evt.data.data.sequence]; | ||
if (receiver_1.timeoutId) { | ||
clearTimeout(receiver_1.timeoutId); | ||
} | ||
receiver_1.reject(evt.data.data.payload); | ||
} | ||
} | ||
break; | ||
} | ||
@@ -199,0 +226,0 @@ }; |
@@ -50,4 +50,4 @@ import { Callback, CallbackStore } from './types'; | ||
/** | ||
* Sets the default period of time a [[ChattyHostConnection.sendAndReceive]] message will wait | ||
* for a response. The default is 30000ms | ||
* Sets the default period of time a [[ChattyHostConnection.sendAndReceive]] message will wait. | ||
* Use a negative number to wait indefinitely. | ||
* | ||
@@ -54,0 +54,0 @@ * @param timeout in milliseconds |
@@ -128,4 +128,4 @@ "use strict"; | ||
/** | ||
* Sets the default period of time a [[ChattyHostConnection.sendAndReceive]] message will wait | ||
* for a response. The default is 30000ms | ||
* Sets the default period of time a [[ChattyHostConnection.sendAndReceive]] message will wait. | ||
* Use a negative number to wait indefinitely. | ||
* | ||
@@ -132,0 +132,0 @@ * @param timeout in milliseconds |
@@ -13,3 +13,5 @@ /** | ||
/** Response */ | ||
Response = 3 | ||
Response = 3, | ||
/** Asynchronous error response */ | ||
ResponseError = 4 | ||
} |
@@ -40,2 +40,4 @@ "use strict"; | ||
ChattyHostMessages[ChattyHostMessages["Response"] = 3] = "Response"; | ||
/** Asynchronous error response */ | ||
ChattyHostMessages[ChattyHostMessages["ResponseError"] = 4] = "ResponseError"; | ||
})(ChattyHostMessages = exports.ChattyHostMessages || (exports.ChattyHostMessages = {})); |
@@ -25,3 +25,5 @@ import { ChattyHostBuilder } from './host_builder'; | ||
/** | ||
* Send a message to the client via a message channel, and then await a response. | ||
* Send a message to the client via a message channel and then await a response. | ||
* The event listener in the client returns a value or a promise that is resolved | ||
* at some later point. | ||
* | ||
@@ -28,0 +30,0 @@ * @param eventName The name of the event to send to the client |
@@ -184,7 +184,10 @@ "use strict"; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this._receivers[sequence] = resolve; | ||
setTimeout(function () { | ||
delete _this._receivers[sequence]; | ||
reject(new Error('Timeout')); | ||
}, _this._defaultTimeout); | ||
var timeoutId; | ||
if (_this._defaultTimeout > -1) { | ||
timeoutId = setTimeout(function () { | ||
delete _this._receivers[sequence]; | ||
reject(new Error('Timeout')); | ||
}, _this._defaultTimeout); | ||
} | ||
_this._receivers[sequence] = { resolve: resolve, reject: reject, timeoutId: timeoutId }; | ||
})]; | ||
@@ -202,15 +205,41 @@ }); | ||
case client_messages_1.ChattyClientMessages.MessageWithResponse: | ||
var _a = evt.data.data, eventName = _a.eventName, payload_1 = _a.payload, sequence = _a.sequence; | ||
var results = []; | ||
if (_this._handlers[eventName]) { | ||
results = _this._handlers[eventName].map(function (fn) { return fn.apply(_this, payload_1); }); | ||
{ | ||
var _a = evt.data.data, eventName_1 = _a.eventName, payload_1 = _a.payload, sequence_1 = _a.sequence; | ||
var results = []; | ||
if (_this._handlers[eventName_1]) { | ||
results = _this._handlers[eventName_1].map(function (fn) { return fn.apply(_this, payload_1); }); | ||
} | ||
Promise.all(results) | ||
.then(function (resolvedResults) { | ||
_this.sendMsg(host_messages_1.ChattyHostMessages.Response, { eventName: eventName_1, payload: resolvedResults }, sequence_1); | ||
}) | ||
.catch(function (error) { | ||
_this.sendMsg(host_messages_1.ChattyHostMessages.ResponseError, { eventName: eventName_1, payload: error }, sequence_1); | ||
}); | ||
} | ||
_this.sendMsg(host_messages_1.ChattyHostMessages.Response, { eventName: eventName, payload: results }, sequence); | ||
break; | ||
case client_messages_1.ChattyClientMessages.Response: | ||
var receiver = _this._receivers[evt.data.data.sequence]; | ||
if (receiver) { | ||
delete _this._receivers[evt.data.data.sequence]; | ||
receiver(evt.data.data.payload); | ||
{ | ||
var receiver = _this._receivers[evt.data.data.sequence]; | ||
if (receiver) { | ||
delete _this._receivers[evt.data.data.sequence]; | ||
if (receiver.timeoutId) { | ||
clearTimeout(receiver.timeoutId); | ||
} | ||
receiver.resolve(evt.data.data.payload); | ||
} | ||
} | ||
break; | ||
case client_messages_1.ChattyClientMessages.ResponseError: | ||
{ | ||
var receiver = _this._receivers[evt.data.data.sequence]; | ||
if (receiver) { | ||
delete _this._receivers[evt.data.data.sequence]; | ||
if (receiver.timeoutId) { | ||
clearTimeout(receiver.timeoutId); | ||
} | ||
receiver.reject(evt.data.data.payload); | ||
} | ||
} | ||
break; | ||
} | ||
@@ -217,0 +246,0 @@ }; |
@@ -34,3 +34,3 @@ import { ChattyClientBuilder } from './client_builder'; | ||
*/ | ||
static createHostFromSource(source?: string): ChattyHostBuilder; | ||
static createHostFromSource(source: string): ChattyHostBuilder; | ||
/** | ||
@@ -37,0 +37,0 @@ * Creates a [[ChattyClientBuilder]] object. The builder presents a set of methods to configure |
@@ -19,1 +19,6 @@ /** | ||
} | ||
export interface Receiver { | ||
resolve: (value?: any) => void; | ||
reject: (value?: any) => void; | ||
timeoutId: any; | ||
} |
{ | ||
"name": "@looker/chatty", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A simple postMessage host / client channel manager.", | ||
"main": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"files": ["lib/**/*"], | ||
"files": [ | ||
"lib/**/*" | ||
], | ||
"directories": { | ||
@@ -9,0 +11,0 @@ "test": "tests" |
@@ -33,2 +33,16 @@ # chatty | ||
The client iframe can also be created using source from the `createHostFromSource(source)` method. | ||
```typescript | ||
import { Chatty } from 'chatty' | ||
Chatty.createHostFromSource(` | ||
<html> | ||
<body> | ||
<script src='//example.com/client.js' type="application/javascript" /> | ||
</body> | ||
</html> | ||
`) | ||
``` | ||
The client `iframe` creates its client using `createClient()`. It also adds event listeners, builds the | ||
@@ -95,2 +109,44 @@ client and connects. Once connected, it can send messages to its host. | ||
## Sending and receiving asynchronous responses | ||
The `sendAndReceive` method can also be used for data that needs to be retrieved asynchronously. In this scenario | ||
the target function must return a Promise. | ||
In the following example, the host requests that the client return some data that is to be retrieved asynchronously. | ||
```typescript | ||
import { Chatty } from 'chatty' | ||
Chatty.createHost('//example.com/client.html') | ||
.build() | ||
.connect() | ||
.then(client => { | ||
document.querySelector('#get-title')!.addEventListener('click', () => { | ||
client.sendAndReceive(Actions.GET_TITLE, (payload: any[]) => { | ||
const title: Element = document.querySelector('#got-title')! | ||
title.innerHTML = payload[0] | ||
} | ||
}) | ||
}) | ||
.catch(console.error) | ||
``` | ||
The client message handler returns a `Promise`. | ||
```typescript | ||
import { Chatty } from 'chatty' | ||
Chatty.createClient() | ||
.on(Actions.GET_TITLE, () => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(document.querySelector('title')!.text) | ||
}, 200) | ||
}) | ||
}) | ||
.build() | ||
.connect() | ||
.catch(console.error) | ||
``` | ||
## Getting Started | ||
@@ -97,0 +153,0 @@ |
74506
1426
171