@data-fair/lib-vue 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ws.js CHANGED
@@ -1,66 +1,62 @@
1
- import reconnectingWebSocketModule from 'reconnecting-websocket';
2
- import { ref, reactive, onScopeDispose } from 'vue';
3
- const ReconnectingWebSocket = reconnectingWebSocketModule;
1
+ import reconnectingWebSocketModule from 'reconnecting-websocket'
2
+ import { ref, reactive, onScopeDispose } from 'vue'
3
+ const ReconnectingWebSocket = reconnectingWebSocketModule
4
4
  const getWS = (path) => {
5
- if (!window.WebSocket)
6
- return;
7
- // @ts-ignore
8
- if (import.meta.env?.SSR)
9
- return;
10
- const url = (window.location.origin + path).replace('http:', 'ws:').replace('https:', 'wss:');
11
- const ws = new ReconnectingWebSocket(url);
12
- const subscriptions = reactive({});
13
- const opened = ref(false);
14
- ws.addEventListener('open', () => {
15
- opened.value = true;
16
- for (const channel of Object.keys(subscriptions)) {
17
- if (subscriptions[channel].length) {
18
- ws.send(JSON.stringify({ type: 'subscribe', channel }));
19
- }
20
- else {
21
- ws.send(JSON.stringify({ type: 'unsubscribe', channel }));
22
- delete subscriptions[channel];
23
- }
24
- }
25
- });
26
- ws.addEventListener('close', () => {
27
- opened.value = false;
28
- });
29
- ws.onmessage = (event) => {
30
- const body = JSON.parse(event.data);
31
- if (body.type === 'message') {
32
- if (subscriptions[body.channel]?.length) {
33
- for (const listener of subscriptions[body.channel]) {
34
- listener(body.data);
35
- }
36
- }
37
- }
38
- };
39
- function subscribe(channel, listener) {
40
- if (!subscriptions[channel]) {
41
- subscriptions[channel] = [];
42
- if (opened.value)
43
- ws.send(JSON.stringify({ type: 'subscribe', channel }));
44
- }
45
- subscriptions[channel].push(listener);
46
- onScopeDispose(() => {
47
- unsubscribe(channel, listener);
48
- });
5
+ if (!window.WebSocket) { return }
6
+ // @ts-ignore
7
+ if (import.meta.env?.SSR) { return }
8
+ const url = (window.location.origin + path).replace('http:', 'ws:').replace('https:', 'wss:')
9
+ const ws = new ReconnectingWebSocket(url)
10
+ const subscriptions = reactive({})
11
+ const opened = ref(false)
12
+ ws.addEventListener('open', () => {
13
+ opened.value = true
14
+ for (const channel of Object.keys(subscriptions)) {
15
+ if (subscriptions[channel].length) {
16
+ ws.send(JSON.stringify({ type: 'subscribe', channel }))
17
+ } else {
18
+ ws.send(JSON.stringify({ type: 'unsubscribe', channel }))
19
+ delete subscriptions[channel]
20
+ }
49
21
  }
50
- const unsubscribe = (channel, listener) => {
51
- if (subscriptions[channel]) {
52
- subscriptions[channel] = subscriptions[channel].filter(l => l !== listener);
53
- if (subscriptions[channel].length === 0 && opened.value) {
54
- ws.send(JSON.stringify({ type: 'unsubscribe', channel }));
55
- delete subscriptions[channel];
56
- }
22
+ })
23
+ ws.addEventListener('close', () => {
24
+ opened.value = false
25
+ })
26
+ ws.onmessage = (event) => {
27
+ const body = JSON.parse(event.data)
28
+ if (body.type === 'message') {
29
+ if (subscriptions[body.channel]?.length) {
30
+ for (const listener of subscriptions[body.channel]) {
31
+ listener(body.data)
57
32
  }
58
- };
59
- return { opened, ws, subscribe, unsubscribe };
60
- };
61
- const sockets = {};
62
- export function useWS(path) {
63
- sockets[path] = sockets[path] ?? getWS(path);
64
- return sockets[path];
33
+ }
34
+ }
35
+ }
36
+ function subscribe (channel, listener) {
37
+ if (!subscriptions[channel]) {
38
+ subscriptions[channel] = []
39
+ if (opened.value) { ws.send(JSON.stringify({ type: 'subscribe', channel })) }
40
+ }
41
+ subscriptions[channel].push(listener)
42
+ onScopeDispose(() => {
43
+ unsubscribe(channel, listener)
44
+ })
45
+ }
46
+ const unsubscribe = (channel, listener) => {
47
+ if (subscriptions[channel]) {
48
+ subscriptions[channel] = subscriptions[channel].filter(l => l !== listener)
49
+ if (subscriptions[channel].length === 0 && opened.value) {
50
+ ws.send(JSON.stringify({ type: 'unsubscribe', channel }))
51
+ delete subscriptions[channel]
52
+ }
53
+ }
54
+ }
55
+ return { opened, ws, subscribe, unsubscribe }
56
+ }
57
+ const sockets = {}
58
+ export function useWS (path) {
59
+ sockets[path] = sockets[path] ?? getWS(path)
60
+ return sockets[path]
65
61
  }
66
- export default useWS;
62
+ export default useWS