Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fn: (param?: T) => void
): void => {
// TODO(dimond): closure typing seems broken because WebChannel does
// not implement goog.events.Listenable
channel.listen(type, (param: unknown) => {
try {
fn(param as T);
} catch (e) {
setTimeout(() => {
throw e;
}, 0);
}
});
};
unguardedEventListen(WebChannel.EventType.OPEN, () => {
if (!closed) {
log.debug(LOG_TAG, 'WebChannel transport opened.');
}
});
unguardedEventListen(WebChannel.EventType.CLOSE, () => {
if (!closed) {
closed = true;
log.debug(LOG_TAG, 'WebChannel transport closed');
streamBridge.callOnClose();
}
});
unguardedEventListen(WebChannel.EventType.ERROR, err => {
if (!closed) {
closed = true;
Code.UNAVAILABLE,
'The operation could not be completed'
)
);
}
});
// WebChannel delivers message events as array. If batching is not enabled
// (it's off by default) each message will be delivered alone, resulting in
// a single element array.
interface WebChannelResponse {
data: Resp[];
}
unguardedEventListen(
WebChannel.EventType.MESSAGE,
msg => {
if (!closed) {
const msgData = msg!.data[0];
assert(!!msgData, 'Got a webchannel message without data.');
// TODO(b/35143891): There is a bug in One Platform that caused errors
// (and only errors) to be wrapped in an extra array. To be forward
// compatible with the bug we need to check either condition. The latter
// can be removed once the fix has been rolled out.
// Use any because msgData.error is not typed.
const msgDataOrError: WebChannelError | object = msgData;
const error =
msgDataOrError.error ||
(msgDataOrError as WebChannelError[])[0]?.error;
if (error) {
log.debug(LOG_TAG, 'WebChannel received error:', error);
// error.status will be a string like 'OK' or 'NOT_FOUND'.