@dammers/use-signalr-core 0.4.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/README.md +18 -28
- package/dist/calls.d.ts +48 -27
- package/dist/calls.js +45 -38
- package/dist/config.d.ts +8 -7
- package/dist/config.js +71 -17
- package/dist/connection-manager.d.ts +12 -10
- package/dist/connection-manager.js +188 -92
- package/dist/context.d.ts +35 -19
- package/dist/errors.d.ts +9 -0
- package/dist/errors.js +16 -0
- package/dist/hub-entry.d.ts +2 -5
- package/dist/index.d.ts +9 -11
- package/dist/index.js +4 -5
- package/dist/retry.d.ts +3 -29
- package/dist/retry.js +14 -33
- package/dist/session.d.ts +44 -0
- package/dist/session.js +202 -0
- package/dist/status-store.d.ts +3 -1
- package/dist/types.d.ts +110 -49
- package/package.json +23 -7
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { StatusStore } from "./status-store.js";
|
|
2
|
+
import type { HubConnectionStatus, HubString, ResolvedHubConfig, SignalRContract, SignalRErrorInfo } from "./types.js";
|
|
3
|
+
import type { SignalRContextValueBase, SignalRPublicContext } from "./context.js";
|
|
4
|
+
export interface SignalRSessionDeps<T extends SignalRContract, TStore extends StatusStore<keyof T & HubString> = StatusStore<keyof T & HubString>> {
|
|
5
|
+
/**
|
|
6
|
+
* The hub paths that the session manages.
|
|
7
|
+
* @remarks `Array<keyof T & HubString>`
|
|
8
|
+
*/
|
|
9
|
+
hubs: Array<keyof T & HubString>;
|
|
10
|
+
/** Returns the resolved config of one hub, the result of `resolveHubConfig`. */
|
|
11
|
+
resolve: (hub: keyof T & HubString) => ResolvedHubConfig;
|
|
12
|
+
/** The store that receives every status change. */
|
|
13
|
+
statusStore: TStore;
|
|
14
|
+
/** Read on every negotiate and every auto-reconnect attempt, so a token change needs no rebuild. */
|
|
15
|
+
getAccessToken: () => string | Promise<string>;
|
|
16
|
+
/** Called after the store changes. */
|
|
17
|
+
onStatusChange?: (hub: keyof T & HubString, status: HubConnectionStatus) => void;
|
|
18
|
+
/** Called for connection errors and for errors that user callbacks throw. */
|
|
19
|
+
onError?: (hub: keyof T & HubString, error: unknown, info: SignalRErrorInfo) => void;
|
|
20
|
+
}
|
|
21
|
+
/** The values that a provider applies. */
|
|
22
|
+
export interface SignalRSessionValues {
|
|
23
|
+
/** The base URL of the server. An empty value disables the session. */
|
|
24
|
+
baseUrl?: string;
|
|
25
|
+
/**
|
|
26
|
+
* When `false`, the session stops and every hub is `idle`.
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
/** A different value rebuilds the connections. */
|
|
31
|
+
connectionKey?: string | number;
|
|
32
|
+
}
|
|
33
|
+
export interface SignalRSession<T extends SignalRContract, TStore extends StatusStore<keyof T & HubString> = StatusStore<keyof T & HubString>> {
|
|
34
|
+
/** Disposes the live generation, sets every hub to `"idle"`, and forgets which hubs connected before. Calling it again does nothing. */
|
|
35
|
+
stop: () => void;
|
|
36
|
+
/** Does nothing if the values equal the last applied values. Otherwise it disposes the live generation and, if `enabled` is true and `baseUrl` is not empty, builds a new one. A rebuild keeps which hubs connected, so their reconnect callbacks run. */
|
|
37
|
+
update: (values: SignalRSessionValues) => void;
|
|
38
|
+
/** The value that an adapter puts into its framework context. Its identity is stable. */
|
|
39
|
+
context: SignalRContextValueBase<T, TStore>;
|
|
40
|
+
/** The frozen object with `getConnection`, `getStatus`, and `waitForConnection`. Its identity is stable. */
|
|
41
|
+
publicContext: SignalRPublicContext<T>;
|
|
42
|
+
}
|
|
43
|
+
/** Owns the manager generation, lazy ref-counts, reconnect listeners, and the context value. Adapters add only framework binding. */
|
|
44
|
+
export declare function createSignalRSession<T extends SignalRContract, TStore extends StatusStore<keyof T & HubString> = StatusStore<keyof T & HubString>>(deps: SignalRSessionDeps<T, TStore>): SignalRSession<T, TStore>;
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { HubConnectionState } from "@microsoft/signalr";
|
|
2
|
+
import { createConnectionManager, runUserCallback } from "./connection-manager.js";
|
|
3
|
+
import { SIGNALR_ERROR_NAMES, namedError } from "./errors.js";
|
|
4
|
+
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
5
|
+
/** Owns the manager generation, lazy ref-counts, reconnect listeners, and the context value. Adapters add only framework binding. */
|
|
6
|
+
export function createSignalRSession(deps) {
|
|
7
|
+
const { hubs, resolve, statusStore, getAccessToken, onStatusChange, onError } = deps;
|
|
8
|
+
const reportCallbackError = (hub, err) => onError?.(hub, err, { source: "callback" });
|
|
9
|
+
// Lazy ref-counts, stop timers, reconnect listeners, connect history, and event handlers persist
|
|
10
|
+
// across rebuilds. The live manager is swapped per generation.
|
|
11
|
+
const refCounts = new Map();
|
|
12
|
+
const stopTimers = new Map();
|
|
13
|
+
const reconnectListeners = new Map();
|
|
14
|
+
const connectedBefore = new Set();
|
|
15
|
+
const handlers = new Map();
|
|
16
|
+
const waiters = new Map();
|
|
17
|
+
// Hubs that stopped after a non-retriable error, with the error. A wait on one can never succeed.
|
|
18
|
+
const failed = new Map();
|
|
19
|
+
let disabled = false;
|
|
20
|
+
let current = null;
|
|
21
|
+
let applied = null;
|
|
22
|
+
const dispatch = (hub, event, args) => {
|
|
23
|
+
const set = handlers.get(hub)?.get(event);
|
|
24
|
+
if (!set)
|
|
25
|
+
return;
|
|
26
|
+
[...set].forEach((handler) => runUserCallback(hub, () => handler(...args), reportCallbackError));
|
|
27
|
+
};
|
|
28
|
+
const flushWaiters = (hub) => {
|
|
29
|
+
const connection = current?.getConnection(hub);
|
|
30
|
+
const set = waiters.get(hub);
|
|
31
|
+
if (!connection || !set)
|
|
32
|
+
return;
|
|
33
|
+
[...set].forEach((waiter) => waiter.resolve(connection));
|
|
34
|
+
};
|
|
35
|
+
const rejectWaiters = (hub, error) => {
|
|
36
|
+
const set = waiters.get(hub);
|
|
37
|
+
if (set)
|
|
38
|
+
[...set].forEach((waiter) => waiter.reject(error));
|
|
39
|
+
};
|
|
40
|
+
const disabledError = (hub) => namedError(SIGNALR_ERROR_NAMES.disabled, `SignalR is disabled: ${hub}`);
|
|
41
|
+
const failHub = (hub, cause) => {
|
|
42
|
+
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
43
|
+
const error = namedError(SIGNALR_ERROR_NAMES.disconnected, `SignalR hub ${hub} is disconnected: ${reason}`);
|
|
44
|
+
failed.set(hub, error);
|
|
45
|
+
rejectWaiters(hub, error);
|
|
46
|
+
};
|
|
47
|
+
const waitForConnection = (hub, timeoutMs, signal) => new Promise((resolvePromise, reject) => {
|
|
48
|
+
if (signal?.aborted)
|
|
49
|
+
return reject(signal.reason);
|
|
50
|
+
const live = current?.getConnection(hub);
|
|
51
|
+
if (live?.state === HubConnectionState.Connected)
|
|
52
|
+
return resolvePromise(live);
|
|
53
|
+
if (disabled)
|
|
54
|
+
return reject(disabledError(hub));
|
|
55
|
+
const failure = failed.get(hub);
|
|
56
|
+
if (failure)
|
|
57
|
+
return reject(failure);
|
|
58
|
+
let set = waiters.get(hub);
|
|
59
|
+
if (!set)
|
|
60
|
+
waiters.set(hub, (set = new Set()));
|
|
61
|
+
const settle = () => {
|
|
62
|
+
if (timer !== undefined)
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
signal?.removeEventListener("abort", onAbort);
|
|
65
|
+
set.delete(waiter);
|
|
66
|
+
};
|
|
67
|
+
const waiter = {
|
|
68
|
+
resolve: (connection) => {
|
|
69
|
+
settle();
|
|
70
|
+
resolvePromise(connection);
|
|
71
|
+
},
|
|
72
|
+
reject: (error) => {
|
|
73
|
+
settle();
|
|
74
|
+
reject(error);
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
const onAbort = () => {
|
|
78
|
+
settle();
|
|
79
|
+
reject(signal?.reason);
|
|
80
|
+
};
|
|
81
|
+
const timer = Number.isFinite(timeoutMs)
|
|
82
|
+
? setTimeout(() => {
|
|
83
|
+
settle();
|
|
84
|
+
reject(namedError(SIGNALR_ERROR_NAMES.timeout, `Timeout waiting for SignalR connection to ${hub} (${timeoutMs}ms)`));
|
|
85
|
+
}, Math.min(timeoutMs, MAX_TIMER_MS))
|
|
86
|
+
: undefined;
|
|
87
|
+
set.add(waiter);
|
|
88
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
89
|
+
});
|
|
90
|
+
const start = (baseUrl) => {
|
|
91
|
+
if (current)
|
|
92
|
+
teardown();
|
|
93
|
+
const manager = createConnectionManager({
|
|
94
|
+
baseUrl,
|
|
95
|
+
hubs,
|
|
96
|
+
resolve,
|
|
97
|
+
getAccessToken,
|
|
98
|
+
refCounts,
|
|
99
|
+
stopTimers,
|
|
100
|
+
reconnectListeners,
|
|
101
|
+
connectedBefore,
|
|
102
|
+
dispatch,
|
|
103
|
+
onStatus: (hub, status, error) => {
|
|
104
|
+
if (current === manager) {
|
|
105
|
+
if (status === "connected")
|
|
106
|
+
flushWaiters(hub);
|
|
107
|
+
if (status === "disconnected")
|
|
108
|
+
failHub(hub, error);
|
|
109
|
+
else
|
|
110
|
+
failed.delete(hub);
|
|
111
|
+
}
|
|
112
|
+
runUserCallback(hub, () => statusStore.set(hub, status), reportCallbackError);
|
|
113
|
+
runUserCallback(hub, () => onStatusChange?.(hub, status), reportCallbackError);
|
|
114
|
+
},
|
|
115
|
+
onError: (hub, err, info) => onError?.(hub, err, info),
|
|
116
|
+
isCurrent: () => current === manager,
|
|
117
|
+
});
|
|
118
|
+
current = manager;
|
|
119
|
+
manager.reconcile(); // build eager hubs plus already-referenced lazy hubs
|
|
120
|
+
};
|
|
121
|
+
const teardown = () => {
|
|
122
|
+
// Publish before dispose, so listeners still see the live connection.
|
|
123
|
+
hubs.forEach((h) => {
|
|
124
|
+
const wasIdle = statusStore.get(h) === "idle";
|
|
125
|
+
runUserCallback(h, () => statusStore.set(h, "idle"), reportCallbackError);
|
|
126
|
+
if (wasIdle)
|
|
127
|
+
return;
|
|
128
|
+
runUserCallback(h, () => onStatusChange?.(h, "idle"), reportCallbackError);
|
|
129
|
+
});
|
|
130
|
+
current?.dispose();
|
|
131
|
+
hubs.forEach((h) => {
|
|
132
|
+
if (resolve(h).lazy && !refCounts.get(h))
|
|
133
|
+
connectedBefore.delete(h); // a lazy hub with no consumer starts fresh
|
|
134
|
+
});
|
|
135
|
+
current = null;
|
|
136
|
+
applied = null;
|
|
137
|
+
failed.clear();
|
|
138
|
+
};
|
|
139
|
+
const stop = () => {
|
|
140
|
+
teardown();
|
|
141
|
+
connectedBefore.clear();
|
|
142
|
+
};
|
|
143
|
+
const update = ({ baseUrl, enabled, connectionKey }) => {
|
|
144
|
+
const next = { baseUrl, enabled: enabled !== false, connectionKey };
|
|
145
|
+
if (applied &&
|
|
146
|
+
Object.is(applied.baseUrl, next.baseUrl) &&
|
|
147
|
+
applied.enabled === next.enabled &&
|
|
148
|
+
Object.is(applied.connectionKey, next.connectionKey))
|
|
149
|
+
return;
|
|
150
|
+
teardown();
|
|
151
|
+
applied = next; // before start(), so a nested update() from a status callback wins
|
|
152
|
+
disabled = !(next.enabled && baseUrl);
|
|
153
|
+
if (disabled)
|
|
154
|
+
hubs.forEach((hub) => rejectWaiters(hub, disabledError(hub)));
|
|
155
|
+
else
|
|
156
|
+
start(baseUrl);
|
|
157
|
+
};
|
|
158
|
+
const getConnection = (hub) => current?.getConnection(hub) ?? null;
|
|
159
|
+
const getStatus = (hub) => statusStore.get(hub);
|
|
160
|
+
const publicContext = Object.freeze({
|
|
161
|
+
getConnection,
|
|
162
|
+
getStatus,
|
|
163
|
+
waitForConnection,
|
|
164
|
+
});
|
|
165
|
+
const context = {
|
|
166
|
+
getConnection,
|
|
167
|
+
getStatus,
|
|
168
|
+
publicContext,
|
|
169
|
+
statusStore,
|
|
170
|
+
waitForConnection,
|
|
171
|
+
acquire: (hub) => {
|
|
172
|
+
const next = (refCounts.get(hub) ?? 0) + 1;
|
|
173
|
+
refCounts.set(hub, next);
|
|
174
|
+
if (next === 1)
|
|
175
|
+
current?.reconcile();
|
|
176
|
+
},
|
|
177
|
+
release: (hub) => {
|
|
178
|
+
const next = Math.max(0, (refCounts.get(hub) ?? 0) - 1);
|
|
179
|
+
refCounts.set(hub, next);
|
|
180
|
+
if (next === 0)
|
|
181
|
+
current?.reconcile();
|
|
182
|
+
},
|
|
183
|
+
registerReconnect: (hub, cb) => {
|
|
184
|
+
let set = reconnectListeners.get(hub);
|
|
185
|
+
if (!set)
|
|
186
|
+
reconnectListeners.set(hub, (set = new Set()));
|
|
187
|
+
set.add(cb);
|
|
188
|
+
return () => set.delete(cb);
|
|
189
|
+
},
|
|
190
|
+
subscribe: (hub, event, handler) => {
|
|
191
|
+
let byEvent = handlers.get(hub);
|
|
192
|
+
if (!byEvent)
|
|
193
|
+
handlers.set(hub, (byEvent = new Map()));
|
|
194
|
+
let set = byEvent.get(event);
|
|
195
|
+
if (!set)
|
|
196
|
+
byEvent.set(event, (set = new Set()));
|
|
197
|
+
set.add(handler);
|
|
198
|
+
return () => set.delete(handler);
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
return { stop, update, context, publicContext };
|
|
202
|
+
}
|
package/dist/status-store.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { HubConnectionStatus } from "./types";
|
|
1
|
+
import type { HubConnectionStatus } from "./types.js";
|
|
2
2
|
/** Per-hub status store: point reads and writes keyed by hub. */
|
|
3
3
|
export interface StatusStore<H extends string> {
|
|
4
|
+
/** Returns the status of a hub. Must return `"idle"` for a hub without a value. */
|
|
4
5
|
get: (hub: H) => HubConnectionStatus;
|
|
6
|
+
/** Stores the status of a hub. */
|
|
5
7
|
set: (hub: H, status: HubConnectionStatus) => void;
|
|
6
8
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HubConnectionBuilder, IHttpConnectionOptions, IHubProtocol, IRetryPolicy, LogLevel } from "@microsoft/signalr";
|
|
2
2
|
/** A hub path, for example `/hubs/chat`. */
|
|
3
3
|
export type HubString = `/${string}`;
|
|
4
4
|
/**
|
|
@@ -56,88 +56,149 @@ type Methods<T, H extends keyof T> = NonNullable<T[H] extends {
|
|
|
56
56
|
export type EventName<T, H extends keyof T> = keyof Events<T, H> & string;
|
|
57
57
|
export type MethodName<T, H extends keyof T> = keyof Methods<T, H> & string;
|
|
58
58
|
export type EventArgs<T, H extends keyof T, E extends EventName<T, H>> = Events<T, H>[E] extends (...args: infer A) => any ? A : never;
|
|
59
|
-
export type MethodArgs<T, H extends keyof T, M extends MethodName<T, H>> = Methods<T, H>[M] extends (...args: infer A) => any ? A :
|
|
59
|
+
export type MethodArgs<T, H extends keyof T, M extends MethodName<T, H>> = Methods<T, H>[M] extends (...args: infer A) => any ? A : never;
|
|
60
60
|
export type MethodReturn<T, H extends keyof T, M extends MethodName<T, H>> = Methods<T, H>[M] extends (...args: any[]) => Promise<infer R> ? R : unknown;
|
|
61
|
-
export type HubConnectionStatus =
|
|
62
|
-
/**
|
|
63
|
-
| "
|
|
61
|
+
export type HubConnectionStatus =
|
|
62
|
+
/** No connection is wanted: not started, disabled, stopped, or a lazy hub with no consumer. */
|
|
63
|
+
"idle" | "connecting" | "connected" | "reconnecting"
|
|
64
|
+
/** The connection is wanted but down after a non-retriable error. */
|
|
65
|
+
| "disconnected";
|
|
66
|
+
/** Says where an `onError` error came from. */
|
|
67
|
+
export interface SignalRErrorInfo {
|
|
68
|
+
/** `"connection"`: connect, negotiate, build, close, or protocol errors.
|
|
69
|
+
* `"callback"`: an error that your event handler, reconnect callback, or
|
|
70
|
+
* `onStatusChange` throws or rejects with. */
|
|
71
|
+
source: "connection" | "callback";
|
|
72
|
+
}
|
|
64
73
|
/** Reconnect strategy: `true` for the library default, `false` for none, an
|
|
65
|
-
* array of retry delays in ms, or a custom policy.
|
|
74
|
+
* array of retry delays in ms, or a custom policy. `false` turns off SignalR
|
|
75
|
+
* auto-reconnect only. The library still rebuilds a lost connection with its own delays. */
|
|
66
76
|
export type ReconnectConfig = boolean | number[] | IRetryPolicy;
|
|
67
|
-
/**
|
|
77
|
+
/** Options for the SignalR HTTP connection. The library owns `accessTokenFactory`. */
|
|
78
|
+
export type HttpOptions = Omit<IHttpConnectionOptions, "accessTokenFactory">;
|
|
79
|
+
/** A hub protocol, or a factory that the library calls once per connection build. */
|
|
80
|
+
export type HubProtocolConfig = IHubProtocol | (() => IHubProtocol);
|
|
81
|
+
/** Context for {@link ConfigureBuilder}. */
|
|
82
|
+
export interface BuilderContext {
|
|
83
|
+
hub: HubString;
|
|
84
|
+
baseUrl: string;
|
|
85
|
+
}
|
|
86
|
+
/** Changes the connection builder. It must return the builder to use. */
|
|
87
|
+
export type ConfigureBuilder = (builder: HubConnectionBuilder, ctx: BuilderContext) => HubConnectionBuilder;
|
|
88
|
+
/** Per-hub overrides. The global value is the fallback for each option that you omit. */
|
|
68
89
|
export interface PerHubConfig {
|
|
69
|
-
/**
|
|
70
|
-
*
|
|
90
|
+
/**
|
|
91
|
+
* Connect this hub when its first consumer appears, and disconnect when the last consumer leaves. Falls back to the global `lazy`.
|
|
92
|
+
*/
|
|
71
93
|
lazy?: boolean;
|
|
72
|
-
/**
|
|
73
|
-
*
|
|
94
|
+
/**
|
|
95
|
+
* Time in ms that a lazy hub stays connected after its last consumer leaves. This option exists only per hub.
|
|
96
|
+
* @default 0
|
|
97
|
+
*/
|
|
74
98
|
graceMs?: number;
|
|
75
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* The reconnect strategy of SignalR. `true` uses the SignalR default delays of 0, 2, 10, and 30 seconds. `false` turns off SignalR auto-reconnect only. The library still rebuilds a lost connection. Falls back to the global `reconnect`, else `true`.
|
|
101
|
+
*/
|
|
76
102
|
reconnect?: ReconnectConfig;
|
|
77
|
-
/**
|
|
78
|
-
|
|
103
|
+
/**
|
|
104
|
+
* The log level of the SignalR client for this hub. Falls back to the global `logLevel`, else `LogLevel.Information`.
|
|
105
|
+
*/
|
|
79
106
|
logLevel?: LogLevel;
|
|
80
|
-
|
|
81
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Merged over the global `httpOptions`. The per-hub key wins. `headers` merge by name.
|
|
109
|
+
*/
|
|
110
|
+
httpOptions?: HttpOptions;
|
|
111
|
+
/**
|
|
112
|
+
* The hub protocol, for example `() => new MessagePackHubProtocol()`. The per-hub value wins. Falls back to the global `hubProtocol`, else JSON.
|
|
113
|
+
*/
|
|
114
|
+
hubProtocol?: HubProtocolConfig;
|
|
115
|
+
/** Runs on every connection build, after the library's own builder calls. The global hook runs first, then this hook. */
|
|
116
|
+
configureBuilder?: ConfigureBuilder;
|
|
82
117
|
}
|
|
83
|
-
/**
|
|
84
|
-
|
|
85
|
-
|
|
118
|
+
/** Maps every key that a hub definition does not declare to `never`. */
|
|
119
|
+
type NoUnknownHubKeys<H> = {
|
|
120
|
+
[P in keyof H]: {
|
|
121
|
+
[K in Exclude<keyof H[P], keyof HubDef>]: never;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
/** Config that you pass to `createSignalRClient`. These are the global defaults for all hubs. */
|
|
86
125
|
export interface SignalRClientConfig<H extends Record<HubString, HubDef>> {
|
|
87
|
-
/**
|
|
88
|
-
*
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
126
|
+
/**
|
|
127
|
+
* One entry per hub. The key is the hub path, such as `/hubs/rooms`. The value holds the hub config and its `events` and `methods`. Unknown keys are a type error.
|
|
128
|
+
* @remarks `Record<HubString, HubDef>`
|
|
129
|
+
*/
|
|
130
|
+
hubs: H & NoUnknownHubKeys<H>;
|
|
131
|
+
/**
|
|
132
|
+
* Connect hubs only when a consumer first uses them. By default, all configured hubs connect at the start.
|
|
133
|
+
* @default false
|
|
134
|
+
*/
|
|
92
135
|
lazy?: boolean;
|
|
93
|
-
/**
|
|
136
|
+
/**
|
|
137
|
+
* The reconnect strategy of SignalR. `true` uses the SignalR default delays of 0, 2, 10, and 30 seconds. `false` turns off SignalR auto-reconnect only. The library still rebuilds a lost connection.
|
|
138
|
+
* @default true
|
|
139
|
+
*/
|
|
94
140
|
reconnect?: ReconnectConfig;
|
|
95
|
-
/**
|
|
96
|
-
|
|
141
|
+
/**
|
|
142
|
+
* The log level of the SignalR client.
|
|
143
|
+
* @default LogLevel.Information
|
|
144
|
+
*/
|
|
97
145
|
logLevel?: LogLevel;
|
|
146
|
+
/**
|
|
147
|
+
* Options for the HTTP connection, such as `headers` and `transport`. The library sets `accessTokenFactory`, so it is not part of the type.
|
|
148
|
+
* @default {}
|
|
149
|
+
*/
|
|
150
|
+
httpOptions?: HttpOptions;
|
|
151
|
+
/**
|
|
152
|
+
* The hub protocol, for example `() => new MessagePackHubProtocol()`. A factory runs once for each connection build.
|
|
153
|
+
* @default new JsonHubProtocol()
|
|
154
|
+
*/
|
|
155
|
+
hubProtocol?: HubProtocolConfig;
|
|
156
|
+
/** Runs on every connection build, after the library's own builder calls. Return the builder to use. */
|
|
157
|
+
configureBuilder?: ConfigureBuilder;
|
|
98
158
|
}
|
|
99
|
-
/** Per-hub config with all defaults resolved. */
|
|
159
|
+
/** Per-hub config with all defaults resolved. `resolve` returns the result of `resolveHubConfig`. The shape can grow. */
|
|
100
160
|
export interface ResolvedHubConfig {
|
|
101
161
|
lazy: boolean;
|
|
102
162
|
graceMs: number;
|
|
103
163
|
reconnect: ReconnectConfig;
|
|
104
|
-
maxConnectRetries: number;
|
|
105
164
|
logLevel: LogLevel;
|
|
106
|
-
|
|
107
|
-
|
|
165
|
+
httpOptions: HttpOptions;
|
|
166
|
+
hubProtocol: HubProtocolConfig | undefined;
|
|
167
|
+
/** The global hook first, then the per-hub hook. */
|
|
168
|
+
configureBuilders: ConfigureBuilder[];
|
|
108
169
|
events: string[];
|
|
109
170
|
}
|
|
110
171
|
/** Options for an invoke call. */
|
|
111
172
|
export interface InvokeOptions {
|
|
112
173
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* call, but before its completion reaches the client, re-runs the call.
|
|
116
|
-
* Set this above 0 only for IDEMPOTENT methods.
|
|
174
|
+
* Number of retries for retriable failures. A retry can run the call twice, so use it only for idempotent methods.
|
|
175
|
+
* @default 0
|
|
117
176
|
*/
|
|
118
177
|
retries?: number;
|
|
119
|
-
/**
|
|
178
|
+
/**
|
|
179
|
+
* Maximum wait in ms for the connection, for each attempt. It does not limit the invoke itself. `Infinity` waits without a limit. A value above 2^31-1 is clamped to 2^31-1.
|
|
180
|
+
* @default 10000
|
|
181
|
+
*/
|
|
120
182
|
timeout?: number;
|
|
121
|
-
/**
|
|
122
|
-
*
|
|
183
|
+
/**
|
|
184
|
+
* The delay in ms before each retry. The last array value repeats. A function gets the attempt number, starting at 0. The cap is 30 seconds, and the delay has a jitter of 50 to 100 percent.
|
|
185
|
+
* @default [250, 1000, 3000, 5000]
|
|
186
|
+
*/
|
|
123
187
|
backoff?: number[] | ((attempt: number) => number);
|
|
124
|
-
/**
|
|
125
|
-
* the default rule. */
|
|
188
|
+
/** `true` retries, `false` throws at once, `undefined` uses the default rule. */
|
|
126
189
|
isRetriable?: (error: unknown) => boolean | undefined;
|
|
127
190
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* correct for query-like reads. Set true for a method invoked in an effect
|
|
131
|
-
* cleanup, so it still reaches the server. The detached call survives
|
|
132
|
-
* unmount, but a still-pending retry loop is NOT cancelled. For the
|
|
133
|
-
* connecting-race and lazy-hub case, use `useSignalRTeardown` instead.
|
|
191
|
+
* If `true`, a teardown does not abort the wait or the retry delays, and the call holds a lazy hub open until it settles. If `false`, a teardown aborts them with an `AbortError`. A call that was already sent finishes in both cases.
|
|
192
|
+
* @default false
|
|
134
193
|
*/
|
|
135
194
|
keepAliveOnUnmount?: boolean;
|
|
136
195
|
}
|
|
137
|
-
/** Options for a
|
|
196
|
+
/** Options for a teardown send. */
|
|
138
197
|
export interface TeardownOptions {
|
|
139
|
-
/**
|
|
140
|
-
*
|
|
198
|
+
/**
|
|
199
|
+
* Maximum time in ms to wait for the hub to connect before the library gives up the send.
|
|
200
|
+
* @default 10000
|
|
201
|
+
*/
|
|
141
202
|
timeout?: number;
|
|
142
203
|
}
|
|
143
204
|
export {};
|
package/package.json
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dammers/use-signalr-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Framework-free typed multi-hub SignalR core: connection lifecycle, contracts, retry.",
|
|
5
5
|
"author": "Dammers",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"repository":
|
|
8
|
-
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/DammersCode/use-signalr.git",
|
|
10
|
+
"directory": "packages/core"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://use-signalr.vercel.app/docs/core",
|
|
9
13
|
"bugs": "https://github.com/DammersCode/use-signalr/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"signalr",
|
|
16
|
+
"typescript",
|
|
17
|
+
"realtime",
|
|
18
|
+
"websocket"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20.19"
|
|
22
|
+
},
|
|
10
23
|
"type": "module",
|
|
11
24
|
"sideEffects": false,
|
|
12
25
|
"main": "./dist/index.js",
|
|
@@ -14,8 +27,10 @@
|
|
|
14
27
|
"exports": {
|
|
15
28
|
".": {
|
|
16
29
|
"types": "./dist/index.d.ts",
|
|
17
|
-
"import": "./dist/index.js"
|
|
18
|
-
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
19
34
|
},
|
|
20
35
|
"files": [
|
|
21
36
|
"dist"
|
|
@@ -24,14 +39,15 @@
|
|
|
24
39
|
"access": "public"
|
|
25
40
|
},
|
|
26
41
|
"scripts": {
|
|
42
|
+
"prebuild": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
27
43
|
"build": "tsc -p tsconfig.build.json",
|
|
28
44
|
"typecheck": "tsc --noEmit",
|
|
29
45
|
"test": "vitest run"
|
|
30
46
|
},
|
|
31
47
|
"peerDependencies": {
|
|
32
|
-
"@microsoft/signalr": "
|
|
48
|
+
"@microsoft/signalr": "^8.0.0 || ^9.0.0 || ^10.0.0"
|
|
33
49
|
},
|
|
34
50
|
"devDependencies": {
|
|
35
|
-
"@microsoft/signalr": "
|
|
51
|
+
"@microsoft/signalr": "^8.0.0 || ^9.0.0 || ^10.0.0"
|
|
36
52
|
}
|
|
37
53
|
}
|