Files
api/packages/api-provider/src/ws/onOpen.ts
T
Jaco Greeff f5a9c207d0 Add TSLint (#106)
* Add TSLint

* Update dependencies

* Update dependencies
2018-06-27 12:22:52 +02:00

35 lines
896 B
TypeScript

// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { WsState } from './types';
import assert from '@polkadot/util/assert';
import isNull from '@polkadot/util/is/null';
export default function onOpen (self: WsState): () => boolean {
return (): boolean => {
assert(!isNull(self.websocket), 'WebSocket cannot be null in onOpen');
self.l.debug(() => ['connected to', self.endpoint]);
self.isConnected = true;
self.emitter.emit('connected');
Object.keys(self.queued).forEach((id) => {
try {
// @ts-ignore checked above
self.websocket.send(
self.queued[id]
);
delete self.queued[id];
} catch (error) {
self.l.error(error);
}
});
return true;
};
}