From 488f36311949116c4a6c1c81c7145a1ec9451890 Mon Sep 17 00:00:00 2001 From: Riordan Panayides Date: Thu, 13 Apr 2023 13:47:02 +0100 Subject: [PATCH] v0.1.4 - Use correct WebSocket implementation in browser - Pass reconnectionAttemptsExhausted to onDisconnect - Add disconnect function - Fix package types --- .eslintrc.json | 35 +++++++++++++++++++++++++++++++++ package.json | 2 +- ts/client/scripts/fills.ts | 2 +- ts/client/src/fills.ts | 26 +++++++++++++++++++------ tsconfig.cjs.json | 24 +++++++++++------------ tsconfig.esm.json | 16 +++++++-------- tsconfig.json | 40 +++++++++++++++++++------------------- tsconfig.types.json | 16 +++++++-------- 8 files changed, 105 insertions(+), 56 deletions(-) create mode 100644 .eslintrc.json diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..fd6a100 --- /dev/null +++ b/.eslintrc.json @@ -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" + } + } + \ No newline at end of file diff --git a/package.json b/package.json index e4cd040..054d10d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@blockworks-foundation/mango-feeds", - "version": "0.1.3", + "version": "0.1.4", "description": "Typescript Client for mango-feeds.", "repository": "https://github.com/blockworks-foundation/mango-feeds", "author": { diff --git a/ts/client/scripts/fills.ts b/ts/client/scripts/fills.ts index 8385c55..bed8589 100644 --- a/ts/client/scripts/fills.ts +++ b/ts/client/scripts/fills.ts @@ -5,7 +5,7 @@ const RECONNECT_ATTEMPTS_MAX = -1; // Subscribe on connection const fillsFeed = new FillsFeed('ws://localhost:8080', { - reconnectIntervalMs: RECONNECT_INTERVAL_MS, + reconnectionIntervalMs: RECONNECT_INTERVAL_MS, reconnectionMaxAttempts: RECONNECT_ATTEMPTS_MAX, subscriptions: { accountIds: ['9XJt2tvSZghsMAhWto1VuPBrwXsiimPtsTR8XwGgDxK2'], diff --git a/ts/client/src/fills.ts b/ts/client/src/fills.ts index 971e42b..e1cd7aa 100644 --- a/ts/client/src/fills.ts +++ b/ts/client/src/fills.ts @@ -1,8 +1,10 @@ -import WebSocket from 'ws'; +import ws from 'ws'; + +const WebSocket = global.WebSocket || ws; interface FillsFeedOptions { subscriptions?: FillsFeedSubscribeParams; - reconnectIntervalMs?: number; + reconnectionIntervalMs?: number; reconnectionMaxAttempts?: number; } @@ -74,7 +76,9 @@ export class FillsFeed { private _reconnectionMaxAttempts; 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 _onHead: ((update: HeadUpdate) => void) | null = null; private _onStatus: ((update: StatusMessage) => void) | null = null; @@ -82,7 +86,7 @@ export class FillsFeed { constructor(url: string, options?: FillsFeedOptions) { this._url = url; this._subscriptions = options?.subscriptions; - this._reconnectionIntervalMs = options?.reconnectIntervalMs ?? 5000; + this._reconnectionIntervalMs = options?.reconnectionIntervalMs ?? 5000; this._reconnectionAttempts = 0; this._reconnectionMaxAttempts = options?.reconnectionMaxAttempts ?? -1; @@ -99,7 +103,7 @@ export class FillsFeed { private _connect() { this._socket = new WebSocket(this._url); - this._socket.addEventListener('error', (err) => { + this._socket.addEventListener('error', (err: any) => { console.warn(`[FillsFeed] connection error: ${err.message}`); if (this._reconnectionAttemptsExhausted()) { console.error('[FillsFeed] fatal connection error'); @@ -124,7 +128,8 @@ export class FillsFeed { this._connect(); } }, this._reconnectionIntervalMs); - if (this._onDisconnect) this._onDisconnect(); + if (this._onDisconnect) + this._onDisconnect(this._reconnectionAttemptsExhausted()); }); 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 { return this._connected; } diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index c715e52..ef5ce42 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -1,14 +1,14 @@ { - "extends": "./tsconfig", - "compilerOptions": { - "declaration": true, - "declarationMap": false, - "module": "commonjs", - "outDir": "dist/cjs", - "sourceMap": false - }, - "include": [ - "ts/client/src", - "ts/client/scripts" - ] + "extends": "./tsconfig", + "compilerOptions": { + "declaration": true, + "declarationMap": true, + "module": "commonjs", + "outDir": "dist/cjs", + "sourceMap": false + }, + "include": [ + "ts/client/src", + "ts/client/scripts" + ] } \ No newline at end of file diff --git a/tsconfig.esm.json b/tsconfig.esm.json index b910cc3..fdcfc28 100644 --- a/tsconfig.esm.json +++ b/tsconfig.esm.json @@ -1,10 +1,10 @@ { - "extends": "./tsconfig", - "compilerOptions": { - "declaration": false, - "declarationMap": false, - "module": "esnext", - "outDir": "dist/esm", - "sourceMap": false, - } + "extends": "./tsconfig", + "compilerOptions": { + "declaration": true, + "declarationMap": true, + "module": "esnext", + "outDir": "dist/esm", + "sourceMap": false, + } } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 328aa93..4f982d6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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": { - "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": { - "module": "commonjs" - } - }, - "include": [ - "ts/client/src" - ] + "module": "commonjs" + } + }, + "include": [ + "ts/client/src" + ] } \ No newline at end of file diff --git a/tsconfig.types.json b/tsconfig.types.json index 22c9509..efba203 100644 --- a/tsconfig.types.json +++ b/tsconfig.types.json @@ -1,10 +1,10 @@ { - "extends": "./tsconfig", - "compilerOptions": { - "noEmit": false, - "outDir": "./dist/types", - "declaration": true, - "declarationMap": true, - "emitDeclarationOnly": true - } + "extends": "./tsconfig", + "compilerOptions": { + "noEmit": false, + "outDir": "./dist/types", + "declaration": true, + "declarationMap": true, + "emitDeclarationOnly": true + } } \ No newline at end of file