- Use correct WebSocket implementation in browser
- Pass reconnectionAttemptsExhausted to onDisconnect
- Add disconnect function
- Fix package types
This commit is contained in:
Riordan Panayides 2023-04-13 13:47:02 +01:00
parent 150e85cfeb
commit 488f363119
8 changed files with 105 additions and 56 deletions

35
.eslintrc.json Normal file
View File

@ -0,0 +1,35 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"linebreak-style": [
"error",
"unix"
],
"semi": [
"error",
"always"
],
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/explicit-function-return-type": "warn"
}
}

View File

@ -1,6 +1,6 @@
{ {
"name": "@blockworks-foundation/mango-feeds", "name": "@blockworks-foundation/mango-feeds",
"version": "0.1.3", "version": "0.1.4",
"description": "Typescript Client for mango-feeds.", "description": "Typescript Client for mango-feeds.",
"repository": "https://github.com/blockworks-foundation/mango-feeds", "repository": "https://github.com/blockworks-foundation/mango-feeds",
"author": { "author": {

View File

@ -5,7 +5,7 @@ const RECONNECT_ATTEMPTS_MAX = -1;
// Subscribe on connection // Subscribe on connection
const fillsFeed = new FillsFeed('ws://localhost:8080', { const fillsFeed = new FillsFeed('ws://localhost:8080', {
reconnectIntervalMs: RECONNECT_INTERVAL_MS, reconnectionIntervalMs: RECONNECT_INTERVAL_MS,
reconnectionMaxAttempts: RECONNECT_ATTEMPTS_MAX, reconnectionMaxAttempts: RECONNECT_ATTEMPTS_MAX,
subscriptions: { subscriptions: {
accountIds: ['9XJt2tvSZghsMAhWto1VuPBrwXsiimPtsTR8XwGgDxK2'], accountIds: ['9XJt2tvSZghsMAhWto1VuPBrwXsiimPtsTR8XwGgDxK2'],

View File

@ -1,8 +1,10 @@
import WebSocket from 'ws'; import ws from 'ws';
const WebSocket = global.WebSocket || ws;
interface FillsFeedOptions { interface FillsFeedOptions {
subscriptions?: FillsFeedSubscribeParams; subscriptions?: FillsFeedSubscribeParams;
reconnectIntervalMs?: number; reconnectionIntervalMs?: number;
reconnectionMaxAttempts?: number; reconnectionMaxAttempts?: number;
} }
@ -74,7 +76,9 @@ export class FillsFeed {
private _reconnectionMaxAttempts; private _reconnectionMaxAttempts;
private _onConnect: (() => void) | null = null; private _onConnect: (() => void) | null = null;
private _onDisconnect: (() => void) | null = null; private _onDisconnect:
| ((reconnectionAttemptsExhausted: boolean) => void)
| null = null;
private _onFill: ((update: FillEventUpdate) => void) | null = null; private _onFill: ((update: FillEventUpdate) => void) | null = null;
private _onHead: ((update: HeadUpdate) => void) | null = null; private _onHead: ((update: HeadUpdate) => void) | null = null;
private _onStatus: ((update: StatusMessage) => void) | null = null; private _onStatus: ((update: StatusMessage) => void) | null = null;
@ -82,7 +86,7 @@ export class FillsFeed {
constructor(url: string, options?: FillsFeedOptions) { constructor(url: string, options?: FillsFeedOptions) {
this._url = url; this._url = url;
this._subscriptions = options?.subscriptions; this._subscriptions = options?.subscriptions;
this._reconnectionIntervalMs = options?.reconnectIntervalMs ?? 5000; this._reconnectionIntervalMs = options?.reconnectionIntervalMs ?? 5000;
this._reconnectionAttempts = 0; this._reconnectionAttempts = 0;
this._reconnectionMaxAttempts = options?.reconnectionMaxAttempts ?? -1; this._reconnectionMaxAttempts = options?.reconnectionMaxAttempts ?? -1;
@ -99,7 +103,7 @@ export class FillsFeed {
private _connect() { private _connect() {
this._socket = new WebSocket(this._url); this._socket = new WebSocket(this._url);
this._socket.addEventListener('error', (err) => { this._socket.addEventListener('error', (err: any) => {
console.warn(`[FillsFeed] connection error: ${err.message}`); console.warn(`[FillsFeed] connection error: ${err.message}`);
if (this._reconnectionAttemptsExhausted()) { if (this._reconnectionAttemptsExhausted()) {
console.error('[FillsFeed] fatal connection error'); console.error('[FillsFeed] fatal connection error');
@ -124,7 +128,8 @@ export class FillsFeed {
this._connect(); this._connect();
} }
}, this._reconnectionIntervalMs); }, this._reconnectionIntervalMs);
if (this._onDisconnect) this._onDisconnect(); if (this._onDisconnect)
this._onDisconnect(this._reconnectionAttemptsExhausted());
}); });
this._socket.addEventListener('message', (msg: any) => { this._socket.addEventListener('message', (msg: any) => {
@ -169,6 +174,15 @@ export class FillsFeed {
} }
} }
public disconnect() {
if (this._connected) {
this._socket.close();
this._connected = false;
} else {
console.warn('[FillsFeed] attempt to disconnect when not connected');
}
}
public connected(): boolean { public connected(): boolean {
return this._connected; return this._connected;
} }

View File

@ -1,14 +1,14 @@
{ {
"extends": "./tsconfig", "extends": "./tsconfig",
"compilerOptions": { "compilerOptions": {
"declaration": true, "declaration": true,
"declarationMap": false, "declarationMap": true,
"module": "commonjs", "module": "commonjs",
"outDir": "dist/cjs", "outDir": "dist/cjs",
"sourceMap": false "sourceMap": false
}, },
"include": [ "include": [
"ts/client/src", "ts/client/src",
"ts/client/scripts" "ts/client/scripts"
] ]
} }

View File

@ -1,10 +1,10 @@
{ {
"extends": "./tsconfig", "extends": "./tsconfig",
"compilerOptions": { "compilerOptions": {
"declaration": false, "declaration": true,
"declarationMap": false, "declarationMap": true,
"module": "esnext", "module": "esnext",
"outDir": "dist/esm", "outDir": "dist/esm",
"sourceMap": false, "sourceMap": false,
} }
} }

View File

@ -1,23 +1,23 @@
{ {
"compilerOptions": {
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noImplicitAny": false,
"resolveJsonModule": true,
"skipLibCheck": true,
"strictNullChecks": true,
"target": "esnext",
},
"ts-node": {
// these options are overrides used only by ts-node
// same as the --compilerOptions flag and the TS_NODE_COMPILER_OPTIONS environment variable
"compilerOptions": { "compilerOptions": {
"esModuleInterop": true, "module": "commonjs"
"module": "esnext", }
"moduleResolution": "node", },
"noEmit": true, "include": [
"noImplicitAny": false, "ts/client/src"
"resolveJsonModule": true, ]
"skipLibCheck": true,
"strictNullChecks": true,
"target": "esnext",
},
"ts-node": {
// these options are overrides used only by ts-node
// same as the --compilerOptions flag and the TS_NODE_COMPILER_OPTIONS environment variable
"compilerOptions": {
"module": "commonjs"
}
},
"include": [
"ts/client/src"
]
} }

View File

@ -1,10 +1,10 @@
{ {
"extends": "./tsconfig", "extends": "./tsconfig",
"compilerOptions": { "compilerOptions": {
"noEmit": false, "noEmit": false,
"outDir": "./dist/types", "outDir": "./dist/types",
"declaration": true, "declaration": true,
"declarationMap": true, "declarationMap": true,
"emitDeclarationOnly": true "emitDeclarationOnly": true
} }
} }