v0.1.4
- Use correct WebSocket implementation in browser - Pass reconnectionAttemptsExhausted to onDisconnect - Add disconnect function - Fix package types
This commit is contained in:
parent
150e85cfeb
commit
488f363119
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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": {
|
||||||
|
|
|
@ -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'],
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"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
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
{
|
{
|
||||||
"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,
|
||||||
|
|
Loading…
Reference in New Issue