wspromisify
Advanced tools
Comparing version 0.0.502 to 0.0.511
@@ -61,3 +61,7 @@ { | ||
"scripts": { | ||
"test": "npm run test", | ||
"ci": "npm run lint && npm run test", | ||
"lint": "./node_modules/.bin/tslint 'src/**/*.ts'", | ||
"test": "npm run test:compile && npm run test:run", | ||
"test:compile": "./node_modules/.bin/tsc -p ./test/", | ||
"test:run": "./node_modules/.bin/ava ./test/dist/test/src/index.js", | ||
"dev": "rollup --watch -c --config src/rollup.config.js --environment INCLUDE_DEPS,BUILD:development", | ||
@@ -68,5 +72,11 @@ "prod:cjs": "rollup -c --config src/rollup.config.js --environment INCLUDE_DEPS,BUILD:cjs", | ||
}, | ||
"version": "0.0.502", | ||
"version": "0.0.511", | ||
"devDependencies": { | ||
"@types/node": "^8.0.58", | ||
"@types/ramda": "^0.25.8", | ||
"@types/ws": "^3.2.1", | ||
"ava": "^0.24.0", | ||
"axios": "^0.17.1", | ||
"express": "^4.16.2", | ||
"ramda": "^0.25.0", | ||
"rollup": "^0.52.1", | ||
@@ -77,5 +87,7 @@ "rollup-plugin-commonjs": "^8.2.6", | ||
"rollup-plugin-typescript2": "^0.8.4", | ||
"typescript": "^2.6.2" | ||
"tslint": "^5.8.0", | ||
"typescript": "^2.6.2", | ||
"ws": "^3.3.2" | ||
}, | ||
"types": "./index.d.ts" | ||
} |
@@ -6,3 +6,4 @@ # WebsocketPromisify | ||
// If you detected some bug or so, please, fill an issue. | ||
// Large data support, streams and different server-side implementations are coming. | ||
Large data support, streams and different server-side implementations are coming. | ||
To see a Node.js server-side part, please, take a look on test/mock in github repo. | ||
@@ -24,3 +25,3 @@ | ||
- Rejects if sent into closed socket or after some timeout without response. | ||
- If something sent before connection is connection is estabilished, it sends when its ready. | ||
- If something sent before connection is estabilished, it sends when its ready. | ||
@@ -75,7 +76,9 @@ How it on Server Side ? | ||
// sends any type of message. | ||
// Returns Promise that connection is open. Works even if it already opened. | ||
ready() | ||
// sends any type of message and returns a Promise. | ||
send(message), | ||
// .addEventListener with optional predicate. | ||
on(event_name, handler, predicate = (WebSocketEvent) => true), | ||
// Closes the connection and free up memory. | ||
// Closes the connection and free up memory. Returnds Promise that it has been done. | ||
close() | ||
@@ -95,2 +98,3 @@ | ||
const ws = new WSP({ | ||
// Just a random config. log() is ok. | ||
url: `${somehost}/ws`, | ||
@@ -109,2 +113,4 @@ timeout: 2e3, | ||
try { | ||
// You can wait for ready by calling await ws.ready() or send it right now: | ||
// the messages will be sent as soon as the connection opened. | ||
const data = await ws.send({catSaid: 'Meow!'}) | ||
@@ -111,0 +117,0 @@ console.log({data}) |
@@ -41,2 +41,4 @@ | ||
private messages = [] | ||
private onReadyQueue = [] | ||
private onCloseQueue = [] | ||
private config = <types.Config>{} | ||
@@ -80,2 +82,4 @@ | ||
this.open = true | ||
this.onReadyQueue.forEach((fn) => fn()) | ||
this.onReadyQueue = [] | ||
const {id_key, data_key} = config.server | ||
@@ -92,2 +96,4 @@ // Send all pending messages. | ||
this.open = false | ||
this.onCloseQueue.forEach((fn) => fn()) | ||
this.onCloseQueue = [] | ||
// Auto reconnect. | ||
@@ -149,3 +155,13 @@ const reconnect = config.reconnect | ||
public on(event_name, handler, predicate) { | ||
public async ready() { | ||
return new Promise((ff, rj) => { | ||
if(this.open) { | ||
return true | ||
} else { | ||
this.onReadyQueue.push(ff) | ||
} | ||
}) | ||
} | ||
public on(event_name, handler, predicate?) { | ||
return add_event(this.ws, event_name, event => { | ||
@@ -158,9 +174,13 @@ if(!predicate || predicate(event)) { | ||
public close() { | ||
this.init_flush() | ||
this.open = null | ||
this.ws.close() | ||
this.ws = null | ||
this.forcibly_closed = true | ||
return null | ||
public async close() { | ||
return new Promise((ff, rj) => { | ||
this.open = null | ||
this.onCloseQueue.push(() => { | ||
this.init_flush() | ||
this.ws = null | ||
this.forcibly_closed = true | ||
ff() | ||
}) | ||
this.ws.close() | ||
}) | ||
} | ||
@@ -167,0 +187,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48408
27
1187
120
15