Files
api/packages/api-provider/src/ws/onOpen.ts
T
Jaco Greeff 432f9a73a9 TypeScript conversion (#101)
* Auto-conversion

* ES6 import/export

* WIP...

* WIP...

* And... we are TS typed, some good, some bad, some better, some worse

* Tests passing
2018-06-23 13:39:31 +02:00

35 lines
897 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;
};
};