durablews 1.0.1 → 2.0.0-alpha.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.
@@ -1,120 +0,0 @@
1
- var c = /* @__PURE__ */ ((s) => (s.DISCONNECTED = "DISCONNECTED", s.CONNECTING = "CONNECTING", s.CONNECTED = "CONNECTED", s.CLOSING = "CLOSING", s.CLOSED = "CLOSED", s))(c || {});
2
- let l = null;
3
- async function b(s) {
4
- if (l)
5
- return l;
6
- let e = null;
7
- const S = s.maxQueueSize || 10, i = [];
8
- let d;
9
- const N = s.maxReconnectAttempts || 5, h = Array.from(
10
- { length: N },
11
- (o, n) => Math.pow(2, n) * 1e3
12
- ), m = s.idleTimeout || 5 * 60 * 1e3;
13
- let f;
14
- const u = () => {
15
- clearTimeout(f), f = setTimeout(() => {
16
- r.idle.forEach((o) => o());
17
- }, m);
18
- };
19
- window.addEventListener("mousemove", u), window.addEventListener("keydown", u), window.addEventListener("mousedown", u), u(), s.getToken ? d = await s.getToken() : !d && s.authTokenURL && (d = (await fetch(s.authTokenURL).then(
20
- (n) => n.json()
21
- )).token);
22
- const O = (o = 0) => {
23
- let n = s.url;
24
- d && (n += `?token=${encodeURIComponent(d)}`), e = new WebSocket(n), e.onopen = () => {
25
- let t;
26
- for (; i.length > 0; ) {
27
- t = i.shift();
28
- try {
29
- if (t && (e == null ? void 0 : e.readyState) === (WebSocket == null ? void 0 : WebSocket.OPEN))
30
- e.send(JSON.stringify(t));
31
- else
32
- throw new Error(
33
- "Message not sent due to WebSocket not being in OPEN state"
34
- );
35
- } catch (a) {
36
- console.error(a), i.unshift(t);
37
- break;
38
- }
39
- }
40
- r.open.forEach((a) => a());
41
- }, e.onmessage = (t) => {
42
- try {
43
- const a = JSON.parse(t.data);
44
- r.message.forEach((E) => E(a));
45
- } catch (a) {
46
- t != null && t.data && typeof (t == null ? void 0 : t.data) == "string" || (t == null ? void 0 : t.data) instanceof String ? r.message.forEach(
47
- (E) => E({ data: t == null ? void 0 : t.data })
48
- ) : (console.log(typeof t), console.log(t), console.error("Failed to parse message data:", a));
49
- }
50
- }, e.onclose = (t) => {
51
- e && e.readyState !== (WebSocket == null ? void 0 : WebSocket.CLOSED) && o < N - 1 && // Exclude normal closure from reconnection attempts
52
- t.code !== 1e3 && setTimeout(
53
- () => O(o + 1),
54
- h[o]
55
- ), r.close.forEach((a) => a(t));
56
- }, e.onerror = (t) => {
57
- r.error.forEach((a) => a(t));
58
- };
59
- }, p = (o) => {
60
- (e == null ? void 0 : e.readyState) === (WebSocket == null ? void 0 : WebSocket.OPEN) ? e.send(JSON.stringify(o)) : (e == null ? void 0 : e.readyState) !== (WebSocket == null ? void 0 : WebSocket.CLOSED) && (i.push(o), i.length > S && i.shift());
61
- }, C = () => {
62
- (!e || e.readyState === (WebSocket == null ? void 0 : WebSocket.CLOSED)) && O();
63
- }, T = () => {
64
- e && e.close();
65
- }, D = () => new Promise((o, n) => {
66
- (e == null ? void 0 : e.readyState) === (WebSocket == null ? void 0 : WebSocket.OPEN) ? o() : (e == null || e.addEventListener("open", () => o(), { once: !0 }), e == null || e.addEventListener("error", n, { once: !0 }));
67
- }), k = () => {
68
- if (!e)
69
- return c.DISCONNECTED;
70
- switch (e.readyState) {
71
- case WebSocket.CONNECTING:
72
- return c.CONNECTING;
73
- case WebSocket.OPEN:
74
- return c.CONNECTED;
75
- case WebSocket.CLOSING:
76
- return c.CLOSING;
77
- case WebSocket.CLOSED:
78
- return c.CLOSED;
79
- default:
80
- return c.DISCONNECTED;
81
- }
82
- }, r = {
83
- message: [],
84
- error: [],
85
- open: [],
86
- close: [],
87
- idle: []
88
- };
89
- return l = {
90
- send: p,
91
- // subscribe,
92
- onMessage: (o) => (r.message.push(o), () => {
93
- const n = r.message.indexOf(o);
94
- n !== -1 && r.message.splice(n, 1);
95
- }),
96
- onError: (o) => (r.error.push(o), () => {
97
- const n = r.error.indexOf(o);
98
- n !== -1 && r.error.splice(n, 1);
99
- }),
100
- onOpen: (o) => (r.open.push(o), () => {
101
- const n = r.open.indexOf(o);
102
- n !== -1 && r.open.splice(n, 1);
103
- }),
104
- onClose: (o) => (r.close.push(o), () => {
105
- const n = r.close.indexOf(o);
106
- n !== -1 && r.close.splice(n, 1);
107
- }),
108
- onIdle: (o) => (r.idle.push(o), () => {
109
- const n = r.idle.indexOf(o);
110
- n !== -1 && r.idle.splice(n, 1);
111
- }),
112
- connect: C,
113
- disconnect: T,
114
- isReady: D,
115
- getStatus: k
116
- }, s.autoConnect !== !1 && C(), l;
117
- }
118
- export {
119
- b as defineClient
120
- };
@@ -1 +0,0 @@
1
- (function(c,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(c=typeof globalThis<"u"?globalThis:c||self,a(c.durablews={}))})(this,function(c){"use strict";var a=(r=>(r.DISCONNECTED="DISCONNECTED",r.CONNECTING="CONNECTING",r.CONNECTED="CONNECTED",r.CLOSING="CLOSING",r.CLOSED="CLOSED",r))(a||{});let l=null;async function p(r){if(l)return l;let e=null;const h=r.maxQueueSize||10,d=[];let u;const N=r.maxReconnectAttempts||5,m=Array.from({length:N},(o,n)=>Math.pow(2,n)*1e3),T=r.idleTimeout||5*60*1e3;let O;const f=()=>{clearTimeout(O),O=setTimeout(()=>{s.idle.forEach(o=>o())},T)};window.addEventListener("mousemove",f),window.addEventListener("keydown",f),window.addEventListener("mousedown",f),f(),r.getToken?u=await r.getToken():!u&&r.authTokenURL&&(u=(await fetch(r.authTokenURL).then(n=>n.json())).token);const C=(o=0)=>{let n=r.url;u&&(n+=`?token=${encodeURIComponent(u)}`),e=new WebSocket(n),e.onopen=()=>{let t;for(;d.length>0;){t=d.shift();try{if(t&&(e==null?void 0:e.readyState)===(WebSocket==null?void 0:WebSocket.OPEN))e.send(JSON.stringify(t));else throw new Error("Message not sent due to WebSocket not being in OPEN state")}catch(i){console.error(i),d.unshift(t);break}}s.open.forEach(i=>i())},e.onmessage=t=>{try{const i=JSON.parse(t.data);s.message.forEach(E=>E(i))}catch(i){t!=null&&t.data&&typeof(t==null?void 0:t.data)=="string"||(t==null?void 0:t.data)instanceof String?s.message.forEach(E=>E({data:t==null?void 0:t.data})):(console.log(typeof t),console.log(t),console.error("Failed to parse message data:",i))}},e.onclose=t=>{e&&e.readyState!==(WebSocket==null?void 0:WebSocket.CLOSED)&&o<N-1&&t.code!==1e3&&setTimeout(()=>C(o+1),m[o]),s.close.forEach(i=>i(t))},e.onerror=t=>{s.error.forEach(i=>i(t))}},y=o=>{(e==null?void 0:e.readyState)===(WebSocket==null?void 0:WebSocket.OPEN)?e.send(JSON.stringify(o)):(e==null?void 0:e.readyState)!==(WebSocket==null?void 0:WebSocket.CLOSED)&&(d.push(o),d.length>h&&d.shift())},S=()=>{(!e||e.readyState===(WebSocket==null?void 0:WebSocket.CLOSED))&&C()},D=()=>{e&&e.close()},g=()=>new Promise((o,n)=>{(e==null?void 0:e.readyState)===(WebSocket==null?void 0:WebSocket.OPEN)?o():(e==null||e.addEventListener("open",()=>o(),{once:!0}),e==null||e.addEventListener("error",n,{once:!0}))}),k=()=>{if(!e)return a.DISCONNECTED;switch(e.readyState){case WebSocket.CONNECTING:return a.CONNECTING;case WebSocket.OPEN:return a.CONNECTED;case WebSocket.CLOSING:return a.CLOSING;case WebSocket.CLOSED:return a.CLOSED;default:return a.DISCONNECTED}},s={message:[],error:[],open:[],close:[],idle:[]};return l={send:y,onMessage:o=>(s.message.push(o),()=>{const n=s.message.indexOf(o);n!==-1&&s.message.splice(n,1)}),onError:o=>(s.error.push(o),()=>{const n=s.error.indexOf(o);n!==-1&&s.error.splice(n,1)}),onOpen:o=>(s.open.push(o),()=>{const n=s.open.indexOf(o);n!==-1&&s.open.splice(n,1)}),onClose:o=>(s.close.push(o),()=>{const n=s.close.indexOf(o);n!==-1&&s.close.splice(n,1)}),onIdle:o=>(s.idle.push(o),()=>{const n=s.idle.indexOf(o);n!==-1&&s.idle.splice(n,1)}),connect:S,disconnect:D,isReady:g,getStatus:k},r.autoConnect!==!1&&S(),l}c.defineClient=p,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});