@owlmeans/client-socket 0.1.18-rc.5 → 0.1.18-rc.50

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.
Files changed (65) hide show
  1. package/README.md +22 -16
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/client-socket/SKILL.md +173 -8
  4. package/build/consts.d.ts +8 -0
  5. package/build/consts.d.ts.map +1 -0
  6. package/build/consts.js +17 -0
  7. package/build/consts.js.map +1 -0
  8. package/build/consts.local.d.ts +3 -0
  9. package/build/consts.local.d.ts.map +1 -0
  10. package/build/consts.local.js +2 -0
  11. package/build/consts.local.js.map +1 -0
  12. package/build/helper/types.d.ts +22 -0
  13. package/build/helper/types.d.ts.map +1 -0
  14. package/build/helper/types.js +2 -0
  15. package/build/helper/types.js.map +1 -0
  16. package/build/helper.d.ts +8 -4
  17. package/build/helper.d.ts.map +1 -1
  18. package/build/helper.js +79 -49
  19. package/build/helper.js.map +1 -1
  20. package/build/hooks.d.ts +18 -0
  21. package/build/hooks.d.ts.map +1 -0
  22. package/build/hooks.js +66 -0
  23. package/build/hooks.js.map +1 -0
  24. package/build/index.d.ts +4 -0
  25. package/build/index.d.ts.map +1 -1
  26. package/build/index.js +3 -0
  27. package/build/index.js.map +1 -1
  28. package/build/status.d.ts +30 -0
  29. package/build/status.d.ts.map +1 -0
  30. package/build/status.js +151 -0
  31. package/build/status.js.map +1 -0
  32. package/build/types.d.ts +75 -0
  33. package/build/types.d.ts.map +1 -1
  34. package/build/utils/connection.d.ts +12 -3
  35. package/build/utils/connection.d.ts.map +1 -1
  36. package/build/utils/connection.js +264 -28
  37. package/build/utils/connection.js.map +1 -1
  38. package/build/utils/consts.local.d.ts +6 -0
  39. package/build/utils/consts.local.d.ts.map +1 -0
  40. package/build/utils/consts.local.js +6 -0
  41. package/build/utils/consts.local.js.map +1 -0
  42. package/build/utils/index.d.ts +1 -0
  43. package/build/utils/index.d.ts.map +1 -1
  44. package/build/utils/types.d.ts +31 -0
  45. package/build/utils/types.d.ts.map +1 -0
  46. package/build/utils/types.js +2 -0
  47. package/build/utils/types.js.map +1 -0
  48. package/package.json +15 -11
  49. package/src/consts.local.ts +3 -0
  50. package/src/consts.ts +20 -0
  51. package/src/helper/types.ts +20 -0
  52. package/src/helper.ts +95 -53
  53. package/src/hooks.ts +75 -0
  54. package/src/index.ts +4 -0
  55. package/src/status.ts +162 -0
  56. package/src/types.ts +84 -1
  57. package/src/utils/connection.ts +256 -33
  58. package/src/utils/consts.local.ts +5 -0
  59. package/src/utils/index.ts +1 -0
  60. package/src/utils/types.ts +33 -0
  61. package/tests/context.ts +20 -0
  62. package/tests/reconnect.spec.ts +372 -0
  63. package/tests/tsconfig.json +12 -0
  64. package/tsconfig.json +1 -1
  65. package/build/.gitkeep +0 -0
package/build/hooks.js ADDED
@@ -0,0 +1,66 @@
1
+ import { clientRequestHelper } from '@owlmeans/client-entrypoint';
2
+ import { useContext, useValue } from '@owlmeans/client';
3
+ import { AUTH_QUERY } from '@owlmeans/auth';
4
+ import { useEffect, useMemo, useState } from 'react';
5
+ import { SOCKET_STATUS } from './consts.js';
6
+ import { socketClientHelper } from './helper.js';
7
+ /**
8
+ * Open a socket through a declared entrypoint protocol.
9
+ *
10
+ * String aliases remain an adapter input for dynamically addressed integrations, while
11
+ * application callers pass their immutable protocol declaration and receive its bound client
12
+ * entrypoint here at the transport boundary.
13
+ *
14
+ * Re-opens on its own after a network drop (see `ws()`). `null` until the first attempt opens,
15
+ * and again once the retry budget elapses without one ever opening — dialed afresh on the next
16
+ * `SocketStatusService.retry()`. A screen that must tell those two apart reads
17
+ * `useSocketStatus()` (from `./status.js`) alongside this.
18
+ */
19
+ export const useWs = (module, request, options) => {
20
+ const ctx = useContext();
21
+ const mod = useMemo(() => ctx.entrypoint(module), [module]);
22
+ const [failed, setFailed] = useState(false);
23
+ const [dial, setDial] = useState(0);
24
+ const connection = useValue(async (cancel) => {
25
+ const _request = clientRequestHelper.provideRequest(mod.alias, mod.path());
26
+ Object.assign(_request, request);
27
+ try {
28
+ const conn = await socketClientHelper.ws(mod, _request, options);
29
+ if (cancel?.current) {
30
+ // The component unmounted while the handshake (or its retries) were still in flight —
31
+ // there is no `useEffect` cleanup coming for this value, so close it here instead of
32
+ // leaving a live socket and its retry timers attached to nothing.
33
+ void conn.close();
34
+ return null;
35
+ }
36
+ return conn;
37
+ }
38
+ catch {
39
+ if (!cancel?.current)
40
+ setFailed(true);
41
+ return null;
42
+ }
43
+ }, [
44
+ mod.alias,
45
+ request?.query?.[AUTH_QUERY],
46
+ request?.params ? JSON.stringify(request.params) : undefined,
47
+ dial
48
+ ]);
49
+ useEffect(() => {
50
+ if (!failed || !ctx.hasService(SOCKET_STATUS))
51
+ return;
52
+ return ctx.socketStatus().onRetry(() => {
53
+ setFailed(false);
54
+ setDial(n => n + 1);
55
+ });
56
+ }, [failed]);
57
+ useEffect(() => {
58
+ if (connection != null) {
59
+ return () => {
60
+ void connection.close();
61
+ };
62
+ }
63
+ }, [connection]);
64
+ return connection;
65
+ };
66
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AAKxF,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAEhD;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CACnB,MAAoC,EAAE,OAAuC,EAAE,OAAmB,EAC/E,EAAE;IACrB,MA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C,CAAA;IAEF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC;YAAE,OAAM;QACrD,OAAQ,GAA4C,CAAC,YAAY,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE;YAC/E,SAAS,CAAC,KAAK,CAAC,CAAA;YAChB,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;QACrB,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;IAEZ,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,UAAU,IAAI,IAAI,EAAE,CAAC;YACvB,OAAO,GAAG,EAAE;gBACV,KAAK,UAAU,CAAC,KAAK,EAAE,CAAA;YACzB,CAAC,CAAA;QACH,CAAC;IACH,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAA;IAEhB,OAAO,UAAU,CAAA;AACnB,CAAC,CAAA"}
package/build/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
1
  export type * from './types.js';
2
+ export type * from './helper/types.js';
3
+ export * from './consts.js';
2
4
  export * from './helper.js';
5
+ export * from './hooks.js';
6
+ export * from './status.js';
3
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,aAAa,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,mBAAmB,YAAY,CAAA;AAC/B,mBAAmB,mBAAmB,CAAA;AACtC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA"}
package/build/index.js CHANGED
@@ -1,2 +1,5 @@
1
+ export * from './consts.js';
1
2
  export * from './helper.js';
3
+ export * from './hooks.js';
4
+ export * from './status.js';
2
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,aAAa,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA"}
@@ -0,0 +1,30 @@
1
+ import type { Config, Context, SocketConnectionState, SocketStatusService, SocketStatusServiceAppend } from './types.js';
2
+ /**
3
+ * Aggregates the coarse health of every `ws()`/`useWs()` connection an app has open into one
4
+ * worst-of-all state. A page with several sockets (thinking stream, file watch, notifications…)
5
+ * still shows one dialog, not one per socket — so the aggregate, not any single connection, is
6
+ * what a UI like `SocketReloadDialog` reads.
7
+ */
8
+ export declare const createSocketStatusService: (alias?: string) => SocketStatusService;
9
+ /**
10
+ * Register the shared socket-status aggregator. Idempotent, and cheap enough for a host package
11
+ * (`@owlmeans/web-panel`) to call unconditionally on every context: every `ws()`/`useWs()`
12
+ * connection reports itself here if — and only if — the context carries this service, so an app
13
+ * that never calls this behaves exactly as it did before reconnect support existed.
14
+ */
15
+ export declare const appendSocketStatus: <C extends Config = Config, T extends Context<C> = Context<C>>(ctx: T, alias?: string) => T & SocketStatusServiceAppend;
16
+ /**
17
+ * The worst state across every live `ws()`/`useWs()` connection, or `'online'` if the app never
18
+ * registered the status service (`appendSocketStatus`) or has not finished initializing yet.
19
+ */
20
+ export declare const useSocketStatus: () => SocketConnectionState;
21
+ /**
22
+ * `retry` revives every lost connection now (`SocketStatusService.retry()`); `retrying` is true
23
+ * from any retry — this one, or the service's own on tab activation — until the aggregate settles
24
+ * back on `'online'` or `'lost'`.
25
+ */
26
+ export declare const useSocketRetry: () => {
27
+ retry: () => void;
28
+ retrying: boolean;
29
+ };
30
+ //# sourceMappingURL=status.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"status.d.ts","sourceRoot":"","sources":["../src/status.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,MAAM,EAAE,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,yBAAyB,EACvF,MAAM,YAAY,CAAA;AAInB;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,WAAW,MAAM,KAAmB,mBA6DzE,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,OACxF,CAAC,UAAS,MAAM,KACpB,CAAC,GAAG,yBAON,CAAA;AAgCD;;;GAGG;AACH,eAAO,MAAM,eAAe,QAAO,qBAWlC,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAO;IAAE,KAAK,EAAE,MAAM,IAAI,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAcvE,CAAA"}
@@ -0,0 +1,151 @@
1
+ import { createService } from '@owlmeans/context';
2
+ import { useContext } from '@owlmeans/client';
3
+ import { useCallback, useEffect, useState } from 'react';
4
+ import { RANK } from './consts.local.js';
5
+ import { SOCKET_STATUS } from './consts.js';
6
+ /**
7
+ * Aggregates the coarse health of every `ws()`/`useWs()` connection an app has open into one
8
+ * worst-of-all state. A page with several sockets (thinking stream, file watch, notifications…)
9
+ * still shows one dialog, not one per socket — so the aggregate, not any single connection, is
10
+ * what a UI like `SocketReloadDialog` reads.
11
+ */
12
+ export const createSocketStatusService = (alias = SOCKET_STATUS) => {
13
+ const states = new Map();
14
+ const revivers = new Map();
15
+ const listeners = new Set();
16
+ const retryListeners = new Set();
17
+ let current = 'online';
18
+ const recompute = () => {
19
+ let worst = 'online';
20
+ for (const state of states.values()) {
21
+ if (RANK[state] > RANK[worst])
22
+ worst = state;
23
+ }
24
+ if (worst !== current) {
25
+ current = worst;
26
+ listeners.forEach(listener => listener(current));
27
+ }
28
+ };
29
+ // Connections still retrying are nudged too, so one whose older budget is about to run out does
30
+ // not report `'lost'` right after its lost siblings were revived.
31
+ const retry = () => {
32
+ const pending = [...states].filter(([, state]) => state !== 'online');
33
+ pending.forEach(([id]) => revivers.get(id)?.());
34
+ if (!pending.some(([, state]) => state === 'lost'))
35
+ return false;
36
+ retryListeners.forEach(listener => listener());
37
+ return true;
38
+ };
39
+ // A backgrounded tab is where sockets die unnoticed (throttled timers, a sleeping machine), so
40
+ // the moment it is looked at again is the moment to retry — before anyone reaches for a button.
41
+ const watchActivation = () => {
42
+ if (typeof document === 'undefined' || typeof window === 'undefined')
43
+ return;
44
+ document.addEventListener('visibilitychange', () => {
45
+ if (document.visibilityState === 'visible')
46
+ retry();
47
+ });
48
+ window.addEventListener('focus', () => { retry(); });
49
+ window.addEventListener('online', () => { retry(); });
50
+ };
51
+ return createService(alias, {
52
+ report: (id, state, revive) => {
53
+ states.set(id, state);
54
+ if (revive != null)
55
+ revivers.set(id, revive);
56
+ else
57
+ revivers.delete(id);
58
+ recompute();
59
+ },
60
+ release: (id) => { states.delete(id); revivers.delete(id); recompute(); },
61
+ state: () => current,
62
+ subscribe: listener => {
63
+ listeners.add(listener);
64
+ return () => { listeners.delete(listener); };
65
+ },
66
+ retry,
67
+ onRetry: listener => {
68
+ retryListeners.add(listener);
69
+ return () => { retryListeners.delete(listener); };
70
+ },
71
+ }, service => async () => {
72
+ watchActivation();
73
+ service.initialized = true;
74
+ });
75
+ };
76
+ /**
77
+ * Register the shared socket-status aggregator. Idempotent, and cheap enough for a host package
78
+ * (`@owlmeans/web-panel`) to call unconditionally on every context: every `ws()`/`useWs()`
79
+ * connection reports itself here if — and only if — the context carries this service, so an app
80
+ * that never calls this behaves exactly as it did before reconnect support existed.
81
+ */
82
+ export const appendSocketStatus = (ctx, alias = SOCKET_STATUS) => {
83
+ const _ctx = ctx;
84
+ if (!ctx.hasService(alias)) {
85
+ ctx.registerService(createSocketStatusService(alias));
86
+ }
87
+ _ctx.socketStatus = () => ctx.service(alias);
88
+ return _ctx;
89
+ };
90
+ const readSocketStatusService = (ctx) => {
91
+ try {
92
+ return typeof ctx.hasService === 'function' && ctx.hasService(SOCKET_STATUS)
93
+ ? ctx.socketStatus?.() ?? null : null;
94
+ }
95
+ catch {
96
+ // Registered but not yet run through the context's OWN `init()` — a component that renders
97
+ // as a sibling of the router (as `SocketReloadDialog` does) mounts before that finishes,
98
+ // since `context.configure().init()` runs from the router's effect, not before React's
99
+ // first commit. Treat it as "not ready yet" rather than crash the render.
100
+ return null;
101
+ }
102
+ };
103
+ /** The status service once the context can hand it out; `null` while it cannot, or never will. */
104
+ const useStatusService = () => {
105
+ const ctx = useContext();
106
+ const [service, setService] = useState(() => readSocketStatusService(ctx));
107
+ useEffect(() => {
108
+ if (service != null)
109
+ return;
110
+ let cancelled = false;
111
+ void ctx.waitForInitialized?.().then(() => { if (!cancelled)
112
+ setService(readSocketStatusService(ctx)); });
113
+ return () => { cancelled = true; };
114
+ }, [ctx, service]);
115
+ return service;
116
+ };
117
+ /**
118
+ * The worst state across every live `ws()`/`useWs()` connection, or `'online'` if the app never
119
+ * registered the status service (`appendSocketStatus`) or has not finished initializing yet.
120
+ */
121
+ export const useSocketStatus = () => {
122
+ const service = useStatusService();
123
+ const [state, setState] = useState(() => service?.state() ?? 'online');
124
+ useEffect(() => {
125
+ if (service == null)
126
+ return;
127
+ setState(service.state());
128
+ return service.subscribe(setState);
129
+ }, [service]);
130
+ return state;
131
+ };
132
+ /**
133
+ * `retry` revives every lost connection now (`SocketStatusService.retry()`); `retrying` is true
134
+ * from any retry — this one, or the service's own on tab activation — until the aggregate settles
135
+ * back on `'online'` or `'lost'`.
136
+ */
137
+ export const useSocketRetry = () => {
138
+ const service = useStatusService();
139
+ const [retrying, setRetrying] = useState(false);
140
+ useEffect(() => {
141
+ if (service == null)
142
+ return;
143
+ const stopRetry = service.onRetry(() => setRetrying(service.state() === 'reconnecting'));
144
+ const stopState = service.subscribe(state => { if (state !== 'reconnecting')
145
+ setRetrying(false); });
146
+ return () => { stopRetry(); stopState(); };
147
+ }, [service]);
148
+ const retry = useCallback(() => { service?.retry(); }, [service]);
149
+ return { retry, retrying };
150
+ };
151
+ //# sourceMappingURL=status.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"status.js","sourceRoot":"","sources":["../src/status.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAIxD,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAA;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE3C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,KAAK,GAAW,aAAa,EAAuB,EAAE;IAC9F,MAAM,MAAM,GAAG,IAAI,GAAG,EAAiC,CAAA;IACvD,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAsB,CAAA;IAC9C,MAAM,SAAS,GAAG,IAAI,GAAG,EAA0C,CAAA;IACnE,MAAM,cAAc,GAAG,IAAI,GAAG,EAAc,CAAA;IAC5C,IAAI,OAAO,GAA0B,QAAQ,CAAA;IAE7C,MAAM,SAAS,GAAG,GAAG,EAAE;QACrB,IAAI,KAAK,GAA0B,QAAQ,CAAA;QAC3C,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC;YACpC,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC;gBAAE,KAAK,GAAG,KAAK,CAAA;QAC9C,CAAC;QACD,IAAI,KAAK,KAAK,OAAO,EAAE,CAAC;YACtB,OAAO,GAAG,KAAK,CAAA;YACf,SAAS,CAAC,OAAO,C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,EAAE,CAAC,CAAC,SAAS,EAAE,CAAA,CAAC,CAAC,CAAA;IAC3C,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAA;IAEb,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,GAAG,OAAO,EAAE,KAAK,EAAE,CAAA,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAA;IAEhE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAA;AAC5B,CAAC,CAAA"}
package/build/types.d.ts CHANGED
@@ -1,7 +1,82 @@
1
1
  import type { ClientConfig } from '@owlmeans/client-context';
2
2
  import type { ClientContext } from '@owlmeans/client';
3
+ import type { AbstractRequest } from '@owlmeans/entrypoint';
4
+ import type { InitializedService } from '@owlmeans/context';
3
5
  export interface Config extends ClientConfig {
6
+ socket?: SocketClientSettings;
4
7
  }
5
8
  export interface Context<C extends Config = Config> extends ClientContext<C> {
6
9
  }
10
+ /**
11
+ * How aggressively a dropped socket is retried, and when the carrier gives up on it.
12
+ *
13
+ * The delay grows geometrically from `minDelay` to `maxDelay` (±`jitter`, a fraction of the
14
+ * computed delay) and stays there. `budget` is measured from the connection's FIRST failed
15
+ * attempt in an outage — a socket that opens and drops again right away does not get a fresh
16
+ * budget — and `stableAfter` is how long a reopened socket must stay up before the attempt
17
+ * counter and the outage clock reset, so a flapping link is never read as "recovered" between
18
+ * two drops a second apart.
19
+ */
20
+ export interface ReconnectPolicy {
21
+ /** First retry delay, in ms. Default 200. */
22
+ minDelay: number;
23
+ /** Delay ceiling, in ms — the backoff never grows past this. Default 3000. */
24
+ maxDelay: number;
25
+ /** Geometric growth factor applied to the delay after every failed attempt. Default 2. */
26
+ factor: number;
27
+ /** Randomization applied to each computed delay, as a fraction of it. Default 0.1 (±10%). */
28
+ jitter: number;
29
+ /** Total time, in ms, the carrier keeps retrying before it reports the socket lost. Default
30
+ * 600000 (10 minutes). */
31
+ budget: number;
32
+ /** Retry window, in ms, of an outage restarted on a lost socket by `SocketStatusService.retry()`
33
+ * (a "Try again" press, the tab coming back) — short, so a retry that cannot succeed reports
34
+ * `'lost'` again while someone is still looking. Default 15000. */
35
+ reviveBudget: number;
36
+ /** How long a reopened socket must stay open before the attempt count and the outage clock
37
+ * reset, in ms. Default 10000. */
38
+ stableAfter: number;
39
+ /** Heartbeat ping interval, in ms. Default 30000. */
40
+ heartbeat: number;
41
+ /** How long to wait for ANY inbound frame after a ping before treating the socket as dead and
42
+ * forcing it closed, in ms. Default 10000. */
43
+ pongTimeout: number;
44
+ }
45
+ export interface SocketClientSettings {
46
+ /** `false` disables reconnection outright — a dropped socket is reported gone and stays gone,
47
+ * the pre-reconnect-support behaviour. Anything else merges over the defaults. */
48
+ reconnect?: false | Partial<ReconnectPolicy>;
49
+ }
50
+ export interface ConnectOptions {
51
+ reconnect?: false | Partial<ReconnectPolicy>;
52
+ }
53
+ export interface WsOptions extends ConnectOptions {
54
+ /**
55
+ * Run before every connection attempt, the first one included, given the request that will be
56
+ * turned into the socket URL — where a caller refreshes a token or other query/param value
57
+ * that may have changed since the previous attempt.
58
+ */
59
+ beforeConnect?: (request: AbstractRequest<any>) => void | Promise<void>;
60
+ }
61
+ /** The coarse health of a socket connection — the worst of everything a status service tracks. */
62
+ export type SocketConnectionState = 'online' | 'reconnecting' | 'lost';
63
+ export interface SocketStatusService extends InitializedService {
64
+ /** Record this connection's current state under `id` — call again to update it. `revive` is
65
+ * how `retry()` brings it back (`'lost'`) or hurries it (`'reconnecting'`); every report
66
+ * replaces the previous one. */
67
+ report: (id: string, state: SocketConnectionState, revive?: () => void) => void;
68
+ /** Stop tracking `id` (the connection closed on purpose). */
69
+ release: (id: string) => void;
70
+ /** The worst state across every tracked connection; `'online'` when none are tracked. */
71
+ state: () => SocketConnectionState;
72
+ subscribe: (listener: (state: SocketConnectionState) => void) => () => void;
73
+ /** Revive every `'lost'` connection and hurry every `'reconnecting'` one, then tell `onRetry`
74
+ * listeners. `false` (and no listener call) when nothing was lost. Also fires by itself when a
75
+ * browser tab becomes visible, its window gains focus, or the network comes back online. */
76
+ retry: () => boolean;
77
+ onRetry: (listener: () => void) => () => void;
78
+ }
79
+ export interface SocketStatusServiceAppend {
80
+ socketStatus: () => SocketStatusService;
81
+ }
7
82
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAErD,MAAM,WAAW,MAAO,SAAQ,YAAY;CAAI;AAEhD,MAAM,WAAW,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;CAAI"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAE3D,MAAM,WAAW,MAAO,SAAQ,YAAY;IAC1C,MAAM,CAAC,EAAE,oBAAoB,CAAA;CAC9B;AAED,MAAM,WAAW,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;CAAI;AAEhF;;;;;;;;;GASG;AACH,MAAM,WAAW,eAAe;IAC9B,6CAA6C;IAC7C,QAAQ,EAAE,MAAM,CAAA;IAChB,8EAA8E;IAC9E,QAAQ,EAAE,MAAM,CAAA;IAChB,0FAA0F;IAC1F,MAAM,EAAE,MAAM,CAAA;IACd,6FAA6F;IAC7F,MAAM,EAAE,MAAM,CAAA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AA;CAC9C;AAED,MAAM,WAAW,yBAAyB;IACxC,YAAY,EAAE,MAAM,mBAAmB,CAAA;CACxC"}
@@ -1,4 +1,13 @@
1
- import type { Config, Context } from '../types.js';
2
- import type { Connection } from '@owlmeans/socket';
3
- export declare const makeConnection: <C extends Config = Config, T extends Context<C> = Context<C>>(conn: WebSocket, _context: T) => Connection;
1
+ import type { ManagedConnection, ManagedConnectionOptions } from './types.js';
2
+ /**
3
+ * Build a `Connection` whose transport survives a dropped WebSocket.
4
+ *
5
+ * The connection MODEL (`createBasicConnection()`) is created exactly once and never replaced —
6
+ * every `observe`/`listen` a caller registered keeps firing across a reconnect, because only the
7
+ * underlying `WebSocket` is swapped underneath it. A server's own subscriptions do NOT survive a
8
+ * new TCP connection, so a caller whose server sets up state only after an in-band
9
+ * `authenticated` frame (as this platform's handlers do) must replay that frame itself on
10
+ * `SocketSystemEvent.Reconnected` — this carrier only re-opens the pipe.
11
+ */
12
+ export declare const makeConnection: (opts: ManagedConnectionOptions) => ManagedConnection;
4
13
  //# sourceMappingURL=connection.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/utils/connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,KAAK,EAAE,UAAU,EAA4B,MAAM,kBAAkB,CAAA;AAG5E,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,QACnF,SAAS,YAAY,CAAC,KAC3B,UAkDF,CAAA"}
1
+ {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/utils/connection.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAA;AAI7E;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,SAAU,wBAAwB,KAAG,iBAqQ/D,CAAA"}