diff --git a/base/package.json b/base/package.json index f6bba92..b57e8a4 100644 --- a/base/package.json +++ b/base/package.json @@ -1,15 +1,19 @@ { "name": "@bhoos/websocket-base", - "version": "0.1.1", + "version": "0.2.0", "description": "reliable websocket client", "repository": "https://github.com/Bhoos/websocket", "author": "Bibek Panthi ", "license": "MIT", - "main": "es6/index.js", + "main": "dist/index.js", + "module": "es6/index.js", "type": "module", + "types": "src/index.ts", + "react-native": "src/index.ts", "files": [ "es6", - "dist" + "dist", + "src" ], "scripts": { "build:cjs": "tsc", diff --git a/base/src/buffer.ts b/base/src/buffer.ts index b1e27e9..3649c43 100644 --- a/base/src/buffer.ts +++ b/base/src/buffer.ts @@ -5,7 +5,7 @@ export enum BufferType { export interface Buffer { size : number; // Total number of items that can be added to the buffer. - add(...items : T[]) : void; // Add an item. Maynot be added if there's no space, as per the rules of the buffer. + add(...items : T[]) : boolean; // Add items. Maynot be added if there's no space, as per the rules of the buffer. clear() : void; // Remove all items from buffer forEach(fn:(el:T) => void) :void; // Loop through all existing items. Earlier ones first. toArray(): T[]; // Return an array of all existing items with earlier ones in the beginning of array. @@ -42,8 +42,9 @@ export class RingBuffer implements Buffer { this.count = Math.min(this.count + 1 , this.size); } - add(...items : T[]) : void{ + add(...items : T[]): boolean { items.forEach(this.addItem.bind(this)); + return this.size >= items.length; } clear(): void { @@ -85,7 +86,7 @@ export class FixedQueueBuffer implements Buffer{ this.buffer = Array(size); } - add(...items : T[]) : void { + add(...items : T[]) : boolean { // Add items to buffer (starting from first) until the buffer is full. const freeSize = this.size - this.count; const len = Math.min(items.length, freeSize); @@ -93,6 +94,7 @@ export class FixedQueueBuffer implements Buffer{ this.buffer[this.count] = items[i]; this.count++; } + return freeSize >= items.length; } clear(): void { diff --git a/base/src/index.ts b/base/src/index.ts index ad50340..1e19858 100644 --- a/base/src/index.ts +++ b/base/src/index.ts @@ -40,7 +40,7 @@ export class ReliableWS< WSArgs > { private ws?: WebSocket; - private address: string | (() => string); + private address: string | (() => string | null); private config: Config; private wsOpen: boolean = false; @@ -62,7 +62,7 @@ export class ReliableWS< ondisconnect: ((event: CloseEv, tries: number) => void) | null = null; onreconnect: ((event: Ev) => void) | null = null; - constructor(address: string | (() => string), options: Config, wsargs?: WSArgs) { + constructor(address: string | (() => string | null), options: Config, wsargs?: WSArgs) { this.address = address; this.config = options; this.wsargs = wsargs; @@ -78,6 +78,14 @@ export class ReliableWS< address = this.address else address = this.address(); + + if (address === null) { + this.reconnectTimeout = setTimeout( + this.setupConnection.bind(this), + this.getReconnectionInterval(), + ); + return; + } //@ts-ignore this.ws = new WebSocket(address, this.wsargs); @@ -168,11 +176,12 @@ export class ReliableWS< : this.config.RECONNECT_INTERVAL(this.tries); } - send(msg: any) { + send(msg: any): boolean { if (this.wsOpen && this.ws) { this.ws.send(msg); + return true; } else { - this.msgBuffer.add(msg); + return this.msgBuffer.add(msg); } } @@ -185,5 +194,6 @@ export class ReliableWS< if (this.pingTimer) this.clearPingTimer(); if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout); if (this.ws && this.wsOpen) this.ws.close(); + this.msgBuffer.clear(); } } diff --git a/browser/package.json b/browser/package.json index 53a5aec..9e4dd77 100644 --- a/browser/package.json +++ b/browser/package.json @@ -1,10 +1,10 @@ { "name": "@bhoos/websocket", - "version": "0.1.1", + "version": "0.2.0", "main": "es6/index.js", "license": "MIT", "dependencies": { - "@bhoos/websocket-base": "^0.1.1" + "@bhoos/websocket-base": "^0.2.0" }, "type": "module", "files": [ diff --git a/node/package.json b/node/package.json index 658c0a1..404cccb 100644 --- a/node/package.json +++ b/node/package.json @@ -1,10 +1,10 @@ { "name": "@bhoos/websocket-node", - "version": "0.1.1", + "version": "0.2.0", "main": "es6/index.js", "license": "MIT", "dependencies": { - "@bhoos/websocket-base": "^0.1.1", + "@bhoos/websocket-base": "^0.2.0", "ws": "^8.8.1" }, "type": "module", diff --git a/package.json b/package.json index f972930..da1fd85 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@bhoos/websocket", + "name": "@bhoos/websocket-repo", "private": true, "workspaces": [ "base",