Skip to content

Commit

Permalink
fix: reopen with delay
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Dec 20, 2019
1 parent 0012fdd commit a41ba70
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
3 changes: 0 additions & 3 deletions example/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import acornClassFields from "acorn-class-fields";

import dev from "rollup-plugin-dev";
import svelte from "rollup-plugin-svelte";
import resolve from "rollup-plugin-node-resolve";
import pkg from "../package.json";

const port = 5000;

Expand Down
5 changes: 2 additions & 3 deletions example/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import { websocketStore } from "../../src/index.mjs";
export const socket1 = websocketStore('wss://echo.websocket.org');
export const socket2 = websocketStore('wss://echo.websocket.org');
//export const socket2 = websocketStore('wss://echo.websocket.org');
</script>

<input bind:value={$socket1} placeholder="publish value">


<input bind:value={$socket2} placeholder="back from server">
<input bind:value={$socket1} placeholder="back from server">
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-svelte": "^5.1.1",
"semantic-release": "^15.13.31",
"svelte": "^3.16.4",
"testcafe": "^1.7.0"
"svelte": "^3.16.5",
"testcafe": "^1.7.1"
},
"engines": {
"node": ">=12.14.0"
Expand Down
41 changes: 24 additions & 17 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@

export function websocketStore(url) {
export function websocketStore(url, ...args) {
let socket;
const subscriptions = new Set();
let reopenTimeoutHandler;

function close() {
if (reopenTimeoutHandler) {
clearTimeout(reopenTimeoutHandler);
}

if (socket) {
socket.close();
socket = undefined;
socket = undefined;
}
}

function reopen() {
close();
if (subscriptions.size > 0) {
reopenTimeoutHandler = setTimeout(() => open(), 5000);
}
}

function open() {
if (reopenTimeoutHandler) {
clearTimeout(reopenTimeoutHandler);
reopenTimeoutHandler = undefined;
}

if (socket) {
return;
}

socket = new WebSocket(url);

socket.onopen = event => {
console.log(event);
};
socket = new WebSocket(url, ...args);

socket.onerror = event => {
console.log(event);
};

socket.onclose = event => {
close();
if(subscriptions.size > 0) {
open();
}
};
socket.onclose = event => reopen();

socket.onmessage = event =>
subscriptions.forEach(subscription => subscription(event.data));
subscriptions.forEach(subscription => subscription(event.data));
}

return {
Expand All @@ -46,10 +53,10 @@ export function websocketStore(url) {
subscriptions.add(subscription);
return () => {
subscriptions.delete(subscription);
if(subscriptions.size === 0) {
if (subscriptions.size === 0) {
close();
}
}
};
}
};
}

0 comments on commit a41ba70

Please sign in to comment.