- 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",
"version": "0.1.3",
"version": "0.1.4",
"description": "Typescript Client for mango-feeds.",
"repository": "https://github.com/blockworks-foundation/mango-feeds",
"author": {

View File

@ -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'],

View File

@ -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;
}

View File

@ -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"
]
}

View File

@ -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,
}
}

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": {
"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"
]
}

View File

@ -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
}
}